From e69de57def5e3de48cc61e71393a55e73169261a Mon Sep 17 00:00:00 2001 From: cpq Date: Sat, 29 May 2021 09:46:50 +0100 Subject: [PATCH] Optimise mg_iobuf_append() --- mongoose.c | 6 +++--- src/iobuf.c | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/mongoose.c b/mongoose.c index fe8ff5b2..d5d08d47 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1535,9 +1535,9 @@ size_t mg_iobuf_append(struct mg_iobuf *io, const void *buf, size_t len, size_t chunk_size) { size_t new_size = io->len + len; if (new_size > io->size) { - new_size += chunk_size; - new_size -= new_size % chunk_size; - if (new_size != io->size) mg_iobuf_resize(io, new_size); + new_size += chunk_size; // Make sure that io->size + new_size -= new_size % chunk_size; // is aligned by chunk_size boundary + mg_iobuf_resize(io, new_size); // Attempt to realloc if (new_size != io->size) len = 0; // Realloc failure, append nothing } if (buf != NULL) memmove(io->buf + io->len, buf, len); diff --git a/src/iobuf.c b/src/iobuf.c index 3275f20b..28d5ea64 100644 --- a/src/iobuf.c +++ b/src/iobuf.c @@ -46,9 +46,9 @@ size_t mg_iobuf_append(struct mg_iobuf *io, const void *buf, size_t len, size_t chunk_size) { size_t new_size = io->len + len; if (new_size > io->size) { - new_size += chunk_size; - new_size -= new_size % chunk_size; - if (new_size != io->size) mg_iobuf_resize(io, new_size); + new_size += chunk_size; // Make sure that io->size + new_size -= new_size % chunk_size; // is aligned by chunk_size boundary + mg_iobuf_resize(io, new_size); // Attempt to realloc if (new_size != io->size) len = 0; // Realloc failure, append nothing } if (buf != NULL) memmove(io->buf + io->len, buf, len);