-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP]: Fix array growth thresholding #32035
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,8 +48,6 @@ typedef __uint128_t wideint_t; | |
typedef uint64_t wideint_t; | ||
#endif | ||
|
||
size_t jl_arr_xtralloc_limit = 0; | ||
|
||
#define MAXINTVAL (((size_t)-1)>>1) | ||
|
||
static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims, | ||
|
@@ -695,16 +693,9 @@ static void NOINLINE array_try_unshare(jl_array_t *a) | |
} | ||
} | ||
|
||
static size_t limit_overallocation(jl_array_t *a, size_t alen, size_t newlen, size_t inc) | ||
static size_t array_grow_newsize(size_t curlen) | ||
{ | ||
// Limit overallocation to jl_arr_xtralloc_limit | ||
size_t es = a->elsize; | ||
size_t xtra_elems_mem = (newlen - a->offset - alen - inc) * es; | ||
if (xtra_elems_mem > jl_arr_xtralloc_limit) { | ||
// prune down | ||
return alen + inc + a->offset + (jl_arr_xtralloc_limit / es); | ||
} | ||
return newlen; | ||
return curlen <= 10 ? curlen * 2 : curlen * 1.5; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the threshold be increased to 1000 as per Jeff's comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like it should be more like |
||
} | ||
|
||
STATIC_INLINE void jl_array_grow_at_beg(jl_array_t *a, size_t idx, size_t inc, | ||
|
@@ -752,10 +743,11 @@ STATIC_INLINE void jl_array_grow_at_beg(jl_array_t *a, size_t idx, size_t inc, | |
size_t nb1 = idx * elsz; | ||
if (inc > (a->maxsize - n) / 2 - (a->maxsize - n) / 20) { | ||
// not enough room for requested growth from end of array | ||
size_t newlen = a->maxsize == 0 ? inc * 2 : a->maxsize * 2; | ||
while (n + 2 * inc > newlen - a->offset) | ||
newlen *= 2; | ||
newlen = limit_overallocation(a, n, newlen, 2 * inc); | ||
size_t scaled_size = array_grow_newsize(a->maxsize); | ||
size_t newlen = a->maxsize == 0 ? inc * 2 : scaled_size; | ||
while (n + 2 * inc > newlen - a->offset) { | ||
newlen = array_grow_newsize(newlen); | ||
} | ||
size_t newoffset = (newlen - newnrows) / 2; | ||
if (!array_resize_buffer(a, newlen)) { | ||
data = (char*)a->data + oldoffsnb; | ||
|
@@ -842,10 +834,10 @@ STATIC_INLINE void jl_array_grow_at_end(jl_array_t *a, size_t idx, | |
size_t nbinc = inc * elsz; | ||
// if the requested size is more than 2x current maxsize, grow exactly | ||
// otherwise double the maxsize | ||
size_t newmaxsize = reqmaxsize >= a->maxsize * 2 | ||
size_t scaled_size = array_grow_newsize(a->maxsize); | ||
size_t newmaxsize = reqmaxsize >= scaled_size | ||
? (reqmaxsize < 4 ? 4 : reqmaxsize) | ||
: a->maxsize * 2; | ||
newmaxsize = limit_overallocation(a, n, newmaxsize, inc); | ||
: scaled_size; | ||
size_t oldmaxsize = a->maxsize; | ||
int newbuf = array_resize_buffer(a, newmaxsize); | ||
char *newdata = (char*)a->data + a->offset * elsz; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that it matters much, but in #16305 I used
((curlen*3)>>1)
to avoid floating-point conversion here.