Skip to content

Commit 7583bed

Browse files
authored
Merge pull request #29950 from charris/backport-29885
MAINT: Simplify string arena growth strategy (#29885)
2 parents 3186751 + fe8447d commit 7583bed

File tree

1 file changed

+9
-18
lines changed

1 file changed

+9
-18
lines changed

numpy/_core/src/multiarray/stringdtype/static_string.c

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,6 @@ vstring_buffer(npy_string_arena *arena, _npy_static_string_u *string)
155155
return (char *)((size_t)arena->buffer + string->vstring.offset);
156156
}
157157

158-
#define ARENA_EXPAND_FACTOR 1.25
159-
160158
static char *
161159
arena_malloc(npy_string_arena *arena, npy_string_realloc_func r, size_t size)
162160
{
@@ -168,24 +166,17 @@ arena_malloc(npy_string_arena *arena, npy_string_realloc_func r, size_t size)
168166
else {
169167
string_storage_size = size + sizeof(size_t);
170168
}
171-
if ((arena->size - arena->cursor) <= string_storage_size) {
172-
// realloc the buffer so there is enough room
173-
// first guess is to double the size of the buffer
174-
size_t newsize;
175-
if (arena->size == 0) {
176-
newsize = string_storage_size;
177-
}
178-
else if (((ARENA_EXPAND_FACTOR * arena->size) - arena->cursor) >
179-
string_storage_size) {
180-
newsize = ARENA_EXPAND_FACTOR * arena->size;
169+
if ((arena->size - arena->cursor) < string_storage_size) {
170+
size_t minsize = arena->cursor + string_storage_size;
171+
if (minsize < arena->cursor) {
172+
return NULL; // overflow means out of memory
181173
}
182-
else {
183-
newsize = arena->size + string_storage_size;
184-
}
185-
if ((arena->cursor + size) >= newsize) {
186-
// need extra room beyond the expansion factor, leave some padding
187-
newsize = ARENA_EXPAND_FACTOR * (arena->cursor + size);
174+
// Allocate 25% more than needed for this string.
175+
size_t newsize = minsize + minsize / 4;
176+
if (newsize < minsize) {
177+
return NULL; // overflow means out of memory
188178
}
179+
189180
// passing a NULL buffer to realloc is the same as malloc
190181
char *newbuf = r(arena->buffer, newsize);
191182
if (newbuf == NULL) {

0 commit comments

Comments
 (0)