Skip to content
Snippets Groups Projects
Commit 8c2fab05 authored by Craig Tiller's avatar Craig Tiller Committed by GitHub
Browse files

Merge pull request #7399 from sreecha/poll_alloc_fix

Reduce the number of unnecessary allocations
parents 8bcf6c1d 69b74784
No related branches found
No related tags found
No related merge requests found
...@@ -65,7 +65,7 @@ class ClientAsyncResponseReader GRPC_FINAL ...@@ -65,7 +65,7 @@ class ClientAsyncResponseReader GRPC_FINAL
const W& request) const W& request)
: context_(context), : context_(context),
call_(channel->CreateCall(method, context, cq)), call_(channel->CreateCall(method, context, cq)),
collection_(new CallOpSetCollection) { collection_(std::make_shared<CallOpSetCollection>()) {
collection_->init_buf_.SetCollection(collection_); collection_->init_buf_.SetCollection(collection_);
collection_->init_buf_.SendInitialMetadata( collection_->init_buf_.SendInitialMetadata(
context->send_initial_metadata_, context->initial_metadata_flags()); context->send_initial_metadata_, context->initial_metadata_flags());
......
...@@ -844,6 +844,11 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, ...@@ -844,6 +844,11 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
*worker_hdl = &worker; *worker_hdl = &worker;
grpc_error *error = GRPC_ERROR_NONE; grpc_error *error = GRPC_ERROR_NONE;
/* Avoid malloc for small number of elements. */
enum { inline_elements = 96 };
struct pollfd pollfd_space[inline_elements];
struct grpc_fd_watcher watcher_space[inline_elements];
/* pollset->mu already held */ /* pollset->mu already held */
int added_worker = 0; int added_worker = 0;
int locked = 1; int locked = 1;
...@@ -899,15 +904,23 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, ...@@ -899,15 +904,23 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
int r; int r;
size_t i, fd_count; size_t i, fd_count;
nfds_t pfd_count; nfds_t pfd_count;
/* TODO(ctiller): inline some elements to avoid an allocation */
grpc_fd_watcher *watchers; grpc_fd_watcher *watchers;
struct pollfd *pfds; struct pollfd *pfds;
timeout = poll_deadline_to_millis_timeout(deadline, now); timeout = poll_deadline_to_millis_timeout(deadline, now);
/* TODO(ctiller): perform just one malloc here if we exceed the inline
* case */ if (pollset->fd_count + 2 <= inline_elements) {
pfds = gpr_malloc(sizeof(*pfds) * (pollset->fd_count + 2)); pfds = pollfd_space;
watchers = gpr_malloc(sizeof(*watchers) * (pollset->fd_count + 2)); watchers = watcher_space;
} else {
/* Allocate one buffer to hold both pfds and watchers arrays */
const size_t pfd_size = sizeof(*pfds) * (pollset->fd_count + 2);
const size_t watch_size = sizeof(*watchers) * (pollset->fd_count + 2);
void *buf = gpr_malloc(pfd_size + watch_size);
pfds = buf;
watchers = (void *)((char *)buf + pfd_size);
}
fd_count = 0; fd_count = 0;
pfd_count = 2; pfd_count = 2;
pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd); pfds[0].fd = GRPC_WAKEUP_FD_GET_READ_FD(&grpc_global_wakeup_fd);
...@@ -974,8 +987,11 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset, ...@@ -974,8 +987,11 @@ static grpc_error *pollset_work(grpc_exec_ctx *exec_ctx, grpc_pollset *pollset,
} }
} }
if (pfds != pollfd_space) {
/* pfds and watchers are in the same memory block pointed to by pfds */
gpr_free(pfds); gpr_free(pfds);
gpr_free(watchers); }
GPR_TIMER_END("maybe_work_and_unlock", 0); GPR_TIMER_END("maybe_work_and_unlock", 0);
locked = 0; locked = 0;
} else { } else {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment