JSONRPC: Increase network performance by reducing the reallocation count
The `all_data_cb` callback was called by `libcurl` with small buffers causing frequent reallocations, which killed network performance on large block templates. The new code decouples content length from allocation size. It starts out by allocating a minimum of 16 KiB and doubles the buffer size every time the allocation gets too small. The maximum allowed increase after doubling is capped to 8 MiB.
This commit is contained in:
parent
173a497e60
commit
cdb1c92c82
1 changed files with 31 additions and 10 deletions
35
util.c
35
util.c
|
@ -41,6 +41,7 @@
|
|||
struct data_buffer {
|
||||
void *buf;
|
||||
size_t len;
|
||||
size_t allocated;
|
||||
};
|
||||
|
||||
struct header_info {
|
||||
|
@ -182,21 +183,41 @@ static size_t all_data_cb(const void *ptr, size_t size, size_t nmemb,
|
|||
{
|
||||
struct data_buffer *db = user_data;
|
||||
size_t len = size * nmemb;
|
||||
size_t oldlen, newlen;
|
||||
size_t newalloc, reqalloc;
|
||||
void *newmem;
|
||||
static const unsigned char zero = 0;
|
||||
static const size_t max_realloc_increase = 8 * 1024 * 1024;
|
||||
static const size_t initial_alloc = 16 * 1024;
|
||||
|
||||
oldlen = db->len;
|
||||
newlen = oldlen + len;
|
||||
/* minimum required allocation size */
|
||||
reqalloc = db->len + len + 1;
|
||||
|
||||
newmem = realloc(db->buf, newlen + 1);
|
||||
if (reqalloc > db->allocated) {
|
||||
if (db->len > 0)
|
||||
newalloc = db->allocated * 2;
|
||||
else
|
||||
newalloc = initial_alloc;
|
||||
|
||||
/* limit the maximum buffer increase */
|
||||
if (newalloc - db->allocated > max_realloc_increase)
|
||||
newalloc = db->allocated + max_realloc_increase;
|
||||
|
||||
/* ensure we have a big enough allocation */
|
||||
if (reqalloc > newalloc)
|
||||
newalloc = reqalloc;
|
||||
|
||||
newmem = realloc(db->buf, newalloc);
|
||||
if (!newmem)
|
||||
return 0;
|
||||
|
||||
db->buf = newmem;
|
||||
db->len = newlen;
|
||||
memcpy(db->buf + oldlen, ptr, len);
|
||||
memcpy(db->buf + newlen, &zero, 1); /* null terminate */
|
||||
db->allocated = newalloc;
|
||||
}
|
||||
|
||||
memcpy(db->buf + db->len, ptr, len); /* append new data */
|
||||
memcpy(db->buf + db->len + len, &zero, 1); /* null terminate */
|
||||
|
||||
db->len += len;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue