Skip to content

Commit

Permalink
tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
radkesvat committed Aug 2, 2024
1 parent df86a06 commit 99eceb4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
41 changes: 21 additions & 20 deletions ww/shiftbuffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}

Expand All @@ -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)
{
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
17 changes: 9 additions & 8 deletions ww/ww.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +23,8 @@
enum
{
kSocketManagerWorkerId = 0,
kAdditionalReservedWorkers = 1
kTunManagerWorkerId = 1,
kAdditionalReservedWorkers = 2
};

ww_global_state_t global_ww_state = {0};
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 99eceb4

Please sign in to comment.