Browse Source

[core] reduce oversized mem alloc for backends

reduce oversized memory allocations when reading from backends:
avoid extra power-2 allocation for 1 byte ('\0') when data
available to read is exactly power-2
master
Glenn Strauss 1 month ago
parent
commit
0b56c16a8b
  1. 14
      src/chunk.c
  2. 2
      src/http-header-glue.c

14
src/chunk.c

@ -144,7 +144,7 @@ static void chunk_push_oversized(chunk * const c, const size_t sz) {
}
__attribute_returns_nonnull__
static buffer * chunk_buffer_acquire_sz(size_t sz) {
static buffer * chunk_buffer_acquire_sz(const size_t sz) {
chunk *c;
buffer *b;
if (sz <= (chunk_buf_sz|1)) {
@ -159,11 +159,15 @@ static buffer * chunk_buffer_acquire_sz(size_t sz) {
* (and if doing so, might replace chunks_oversized_n) */
}
else {
/*(round up to nearest chunk_buf_sz)*/
sz = (sz + (chunk_buf_sz-1)) & ~(chunk_buf_sz-1);
c = chunk_pop_oversized(sz);
if (NULL == c)
c = chunk_init(sz);
if (NULL == c) {
/*(round up to nearest chunk_buf_sz)*/
/* NB: round down power-2 + 1 to avoid excess allocation
* (sz & ~1uL) relies on buffer_realloc() adding +1 *and* on callers
* of this func never passing power-2 + 1 sz unless the direct caller
* adds +1 for '\0', as is done in chunk_buffer_prepare_append() */
c = chunk_init(((sz & ~1uL)+(chunk_buf_sz-1)) & ~(chunk_buf_sz-1));
}
}
c->next = chunk_buffers;
chunk_buffers = c;

2
src/http-header-glue.c

@ -1207,7 +1207,7 @@ handler_t http_response_read(request_st * const r, http_response_opts * const op
if (avail < toread) {
/*(add avail+toread to reduce allocations when ioctl EOPNOTSUPP)*/
avail = avail ? avail - 1 + toread : toread;
avail = toread < MAX_READ_LIMIT && avail ? avail-1+toread : toread;
avail = chunk_buffer_prepare_append(b, avail);
}

Loading…
Cancel
Save