[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
This commit is contained in:
Glenn Strauss 2021-09-16 04:24:29 -04:00
parent 94053349c2
commit 0b56c16a8b
2 changed files with 10 additions and 6 deletions

View File

@ -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;

View File

@ -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);
}