From 99eceb46313869712a9922ec1e7640e9be33cd71 Mon Sep 17 00:00:00 2001 From: Radkesvat <134321679+radkesvat@users.noreply.github.com> Date: Fri, 2 Aug 2024 23:25:51 +0000 Subject: [PATCH] tweaks --- ww/shiftbuffer.c | 41 +++++++++++++++++++++-------------------- ww/ww.c | 17 +++++++++-------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/ww/shiftbuffer.c b/ww/shiftbuffer.c index 1126c0d..097133f 100644 --- a/ww/shiftbuffer.c +++ b/ww/shiftbuffer.c @@ -52,7 +52,7 @@ shift_buffer_t *newShiftBuffer(uint8_t tid, unsigned int pre_cap) // NOLINT pre_cap = (unsigned int) pow(2, ceil(log2((double) max(16, pre_cap)))); } - unsigned int real_cap = pre_cap + LEFTPADDING + RIGHTPADDING; + unsigned int real_cap = (pre_cap + LEFTPADDING + RIGHTPADDING) - REFC_SIZE; // shift_buffer_t *self = globalMalloc(sizeof(shift_buffer_t)); shift_buffer_t *self = (shift_buffer_t *) popPoolItem(getWorkerShiftBufferPool(tid)); @@ -65,16 +65,6 @@ shift_buffer_t *newShiftBuffer(uint8_t tid, unsigned int pre_cap) // NOLINT self->refc = (shiftbuffer_refc_t *) (self->pbuf + real_cap); *(self->refc) = 1; - if (real_cap > 0) // map the virtual memory page to physical memory - { - unsigned int i = 0; - do - { - self->pbuf[i] = 0x0; - i += 4096; - } while (i < real_cap); - } - return self; } @@ -98,7 +88,7 @@ void reset(shift_buffer_t *self, unsigned int pre_cap) pre_cap = (unsigned int) pow(2, ceil(log2(((double) max(16, pre_cap))))); } - unsigned int real_cap = pre_cap + LEFTPADDING + RIGHTPADDING; + unsigned int real_cap = (pre_cap + LEFTPADDING + RIGHTPADDING) - REFC_SIZE; if (self->offset != 0) { @@ -111,7 +101,7 @@ void reset(shift_buffer_t *self, unsigned int pre_cap) if (self->full_cap != real_cap) { globalFree(self->pbuf - self->offset); - self->pbuf = globalMalloc(real_cap + +REFC_SIZE); + self->pbuf = globalMalloc(real_cap + REFC_SIZE); self->refc = (shiftbuffer_refc_t *) (self->pbuf + real_cap); *(self->refc) = 1; self->full_cap = real_cap; @@ -140,7 +130,13 @@ void expand(shift_buffer_t *self, unsigned int increase) if (isShallow(self)) { const unsigned int old_realcap = self->full_cap; - unsigned int new_realcap = (unsigned int) pow(2, ceil(log2((old_realcap) + (increase * 2)))); + // unsigned int new_realcap = (unsigned int) pow(2, ceil(log2((old_realcap) + (increase * 2)))); + const unsigned int calculated_increase = increase * 2UL; + const unsigned int minimum_increase = old_realcap / 4; + + unsigned int new_realcap = + old_realcap + (minimum_increase > calculated_increase ? minimum_increase : calculated_increase); + // unShallow char *old_buf = self->pbuf; *(self->refc) -= 1; @@ -156,12 +152,17 @@ void expand(shift_buffer_t *self, unsigned int increase) else { const unsigned int old_realcap = self->full_cap; - unsigned int new_realcap = (unsigned int) pow(2, ceil(log2((old_realcap) + (increase * 2)))); - char *old_buf = self->pbuf; - self->pbuf = globalMalloc(new_realcap + REFC_SIZE); - self->refc = (shiftbuffer_refc_t *) (self->pbuf + new_realcap); - *(self->refc) = 1; - unsigned int dif = (new_realcap - self->full_cap) / 2; + // unsigned int new_realcap = (unsigned int) pow(2, ceil(log2((old_realcap) + (increase * 2)))); + const unsigned int calculated_increase = increase * 2UL; + const unsigned int minimum_increase = old_realcap / 4; + + unsigned int new_realcap = + old_realcap + (minimum_increase > calculated_increase ? minimum_increase : calculated_increase); + char *old_buf = self->pbuf; + self->pbuf = globalMalloc(new_realcap + REFC_SIZE); + self->refc = (shiftbuffer_refc_t *) (self->pbuf + new_realcap); + *(self->refc) = 1; + unsigned int dif = (new_realcap - self->full_cap) / 2; memcpy(&(self->pbuf[self->curpos + dif]), &(old_buf[self->curpos]), self->calc_len); self->curpos += dif; self->full_cap = new_realcap; diff --git a/ww/ww.c b/ww/ww.c index 6fb92b8..a1fb86e 100644 --- a/ww/ww.c +++ b/ww/ww.c @@ -12,7 +12,7 @@ /* additional threads that dose not require instances of every pools and they will create what they need - so, these additions will only reserve their own space on the workers array + so, these additions will reserve their own space on the workers array the only purpose of this is to reduce memory usage @@ -23,7 +23,8 @@ enum { kSocketManagerWorkerId = 0, - kAdditionalReservedWorkers = 1 + kTunManagerWorkerId = 1, + kAdditionalReservedWorkers = 2 }; ww_global_state_t global_ww_state = {0}; @@ -135,12 +136,12 @@ static void initializeShortCuts(void) void **space = globalMalloc(sizeof(void *) * kShourtcutsCount * total_workers); - GSTATE.shortcut_loops = (hloop_t **) (space + (0 *total_workers)); - GSTATE.shortcut_buffer_pools = (buffer_pool_t **) (space + (1 * total_workers)); - GSTATE.shortcut_shift_buffer_pools = (generic_pool_t **) (space + (2 * total_workers)); - GSTATE.shortcut_context_pools = (generic_pool_t **) (space + (3 * total_workers)); - GSTATE.shortcut_line_pools = (generic_pool_t **) (space + (4 * total_workers)); - GSTATE.shortcut_pipeline_msg_pools = (generic_pool_t **) (space + (5 * total_workers)); + GSTATE.shortcut_loops = (hloop_t **) (space + (0UL * total_workers)); + GSTATE.shortcut_buffer_pools = (buffer_pool_t **) (space + (1UL * total_workers)); + GSTATE.shortcut_shift_buffer_pools = (generic_pool_t **) (space + (2UL * total_workers)); + GSTATE.shortcut_context_pools = (generic_pool_t **) (space + (3UL * total_workers)); + GSTATE.shortcut_line_pools = (generic_pool_t **) (space + (4UL * total_workers)); + GSTATE.shortcut_pipeline_msg_pools = (generic_pool_t **) (space + (5UL * total_workers)); } void createWW(const ww_construction_data_t init_data)