Recycle partially filled buffers on the next read

pull/3483/head
Craig Tiller 10 years ago
parent fc193e1ff4
commit 649deebf7f
  1. 2
      include/grpc/support/slice_buffer.h
  2. 9
      src/core/iomgr/tcp_posix.c
  3. 9
      src/core/support/slice_buffer.c

@ -87,7 +87,7 @@ void gpr_slice_buffer_swap(gpr_slice_buffer *a, gpr_slice_buffer *b);
/* move all of the elements of src into dst */ /* move all of the elements of src into dst */
void gpr_slice_buffer_move_into(gpr_slice_buffer *src, gpr_slice_buffer *dst); void gpr_slice_buffer_move_into(gpr_slice_buffer *src, gpr_slice_buffer *dst);
/* remove n bytes from the end of a slice buffer */ /* remove n bytes from the end of a slice buffer */
void gpr_slice_buffer_trim_end(gpr_slice_buffer *src, size_t n); void gpr_slice_buffer_trim_end(gpr_slice_buffer *src, size_t n, gpr_slice_buffer *garbage);
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -78,6 +78,9 @@ typedef struct {
size_t slice_size; size_t slice_size;
gpr_refcount refcount; gpr_refcount refcount;
/* garbage after the last read */
gpr_slice_buffer last_read_buffer;
gpr_slice_buffer *incoming_buffer; gpr_slice_buffer *incoming_buffer;
gpr_slice_buffer *outgoing_buffer; gpr_slice_buffer *outgoing_buffer;
/** slice within outgoing_buffer to write next */ /** slice within outgoing_buffer to write next */
@ -106,6 +109,7 @@ static void tcp_shutdown(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep) {
static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) { static void tcp_free(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
grpc_fd_orphan(exec_ctx, tcp->em_fd, NULL, "tcp_unref_orphan"); grpc_fd_orphan(exec_ctx, tcp->em_fd, NULL, "tcp_unref_orphan");
gpr_slice_buffer_destroy(&tcp->last_read_buffer);
gpr_free(tcp->peer_string); gpr_free(tcp->peer_string);
gpr_free(tcp); gpr_free(tcp);
} }
@ -226,7 +230,8 @@ static void tcp_continue_read(grpc_exec_ctx *exec_ctx, grpc_tcp *tcp) {
if ((size_t)read_bytes < tcp->incoming_buffer->length) { if ((size_t)read_bytes < tcp->incoming_buffer->length) {
gpr_slice_buffer_trim_end( gpr_slice_buffer_trim_end(
tcp->incoming_buffer, tcp->incoming_buffer,
tcp->incoming_buffer->length - (size_t)read_bytes); tcp->incoming_buffer->length - (size_t)read_bytes,
&tcp->last_read_buffer);
} else if (tcp->iov_size < MAX_READ_IOVEC) { } else if (tcp->iov_size < MAX_READ_IOVEC) {
++tcp->iov_size; ++tcp->iov_size;
} }
@ -259,6 +264,7 @@ static void tcp_read(grpc_exec_ctx *exec_ctx, grpc_endpoint *ep,
tcp->read_cb = cb; tcp->read_cb = cb;
tcp->incoming_buffer = incoming_buffer; tcp->incoming_buffer = incoming_buffer;
gpr_slice_buffer_reset_and_unref(incoming_buffer); gpr_slice_buffer_reset_and_unref(incoming_buffer);
gpr_slice_buffer_swap(incoming_buffer, &tcp->last_read_buffer);
TCP_REF(tcp, "read"); TCP_REF(tcp, "read");
if (tcp->finished_edge) { if (tcp->finished_edge) {
tcp->finished_edge = 0; tcp->finished_edge = 0;
@ -457,6 +463,7 @@ grpc_endpoint *grpc_tcp_create(grpc_fd *em_fd, size_t slice_size,
tcp->read_closure.cb_arg = tcp; tcp->read_closure.cb_arg = tcp;
tcp->write_closure.cb = tcp_handle_write; tcp->write_closure.cb = tcp_handle_write;
tcp->write_closure.cb_arg = tcp; tcp->write_closure.cb_arg = tcp;
gpr_slice_buffer_init(&tcp->last_read_buffer);
return &tcp->base; return &tcp->base;
} }

@ -208,7 +208,7 @@ void gpr_slice_buffer_move_into(gpr_slice_buffer *src, gpr_slice_buffer *dst) {
src->length = 0; src->length = 0;
} }
void gpr_slice_buffer_trim_end(gpr_slice_buffer *sb, size_t n) { void gpr_slice_buffer_trim_end(gpr_slice_buffer *sb, size_t n, gpr_slice_buffer *garbage) {
GPR_ASSERT(n <= sb->length); GPR_ASSERT(n <= sb->length);
sb->length -= n; sb->length -= n;
for (;;) { for (;;) {
@ -216,14 +216,15 @@ void gpr_slice_buffer_trim_end(gpr_slice_buffer *sb, size_t n) {
gpr_slice slice = sb->slices[idx]; gpr_slice slice = sb->slices[idx];
size_t slice_len = GPR_SLICE_LENGTH(slice); size_t slice_len = GPR_SLICE_LENGTH(slice);
if (slice_len > n) { if (slice_len > n) {
sb->slices[idx] = gpr_slice_sub_no_ref(slice, 0, slice_len - n); sb->slices[idx] = gpr_slice_split_head(&slice, slice_len - n);
gpr_slice_buffer_add_indexed(garbage, slice);
return; return;
} else if (slice_len == n) { } else if (slice_len == n) {
gpr_slice_unref(slice); gpr_slice_buffer_add_indexed(garbage, slice);
sb->count = idx; sb->count = idx;
return; return;
} else { } else {
gpr_slice_unref(slice); gpr_slice_buffer_add_indexed(garbage, slice);
n -= slice_len; n -= slice_len;
sb->count = idx; sb->count = idx;
} }

Loading…
Cancel
Save