Skip to content

Commit 2d96305

Browse files
committed
define ARENA_EXPANSION_FACTOR and set it to 1.25 instead of 2.0
1 parent b087ed5 commit 2d96305

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

stringdtype/stringdtype/src/static_string.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,10 @@ vstring_buffer(npy_string_arena *arena, _npy_static_string_u *string)
100100
return (char *)((size_t)arena->buffer + string->vstring.offset);
101101
}
102102

103+
#define ARENA_EXPAND_FACTOR 1.25
104+
103105
char *
104-
NpyString_arena_malloc(npy_string_arena *arena, npy_string_realloc_func r,
105-
size_t size)
106+
arena_malloc(npy_string_arena *arena, npy_string_realloc_func r, size_t size)
106107
{
107108
// one extra size_t to store the size of the allocation
108109
size_t string_storage_size;
@@ -119,15 +120,16 @@ NpyString_arena_malloc(npy_string_arena *arena, npy_string_realloc_func r,
119120
if (arena->size == 0) {
120121
newsize = string_storage_size;
121122
}
122-
else if (((2 * arena->size) - arena->cursor) > string_storage_size) {
123-
newsize = 2 * arena->size;
123+
else if (((ARENA_EXPAND_FACTOR * arena->size) - arena->cursor) >
124+
string_storage_size) {
125+
newsize = ARENA_EXPAND_FACTOR * arena->size;
124126
}
125127
else {
126128
newsize = arena->size + string_storage_size;
127129
}
128130
if ((arena->cursor + size) >= newsize) {
129-
// doubling the current size isn't enough
130-
newsize = 2 * (arena->cursor + size);
131+
// need extra room beyond the expansion factor, leave some padding
132+
newsize = ARENA_EXPAND_FACTOR * (arena->cursor + size);
131133
}
132134
// passing a NULL buffer to realloc is the same as malloc
133135
char *newbuf = r(arena->buffer, newsize);
@@ -155,7 +157,7 @@ NpyString_arena_malloc(npy_string_arena *arena, npy_string_realloc_func r,
155157
}
156158

157159
int
158-
NpyString_arena_free(npy_string_arena *arena, _npy_static_string_u *str)
160+
arena_free(npy_string_arena *arena, _npy_static_string_u *str)
159161
{
160162
if (arena->size == 0 && arena->cursor == 0 && arena->buffer == NULL) {
161163
// empty arena, nothing to do
@@ -193,7 +195,7 @@ NpyString_new_allocator(npy_string_malloc_func m, npy_string_free_func f,
193195
allocator->malloc = m;
194196
allocator->free = f;
195197
allocator->realloc = r;
196-
// arena buffer gets allocated in NpyString_arena_malloc
198+
// arena buffer gets allocated in arena_malloc
197199
allocator->arena = NEW_ARENA;
198200
return allocator;
199201
}
@@ -338,8 +340,7 @@ heap_or_arena_allocate(npy_string_allocator *allocator,
338340
}
339341
}
340342
// string isn't previously allocated, so add to existing arena allocation
341-
char *ret = NpyString_arena_malloc(arena, allocator->realloc,
342-
sizeof(char) * size);
343+
char *ret = arena_malloc(arena, allocator->realloc, sizeof(char) * size);
343344
if (size < NPY_MEDIUM_STRING_MAX_SIZE) {
344345
*flags |= NPY_STRING_MEDIUM;
345346
}
@@ -368,7 +369,7 @@ heap_or_arena_deallocate(npy_string_allocator *allocator,
368369
if (arena == NULL) {
369370
return -1;
370371
}
371-
if (NpyString_arena_free(arena, str_u) < 0) {
372+
if (arena_free(arena, str_u) < 0) {
372373
return -1;
373374
}
374375
if (arena->buffer != NULL) {

0 commit comments

Comments
 (0)