Skip to content
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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 10 additions & 18 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the threshold be increased to 1000 as per Jeff's comment?

Copy link
Member

@stevengj stevengj Sep 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it should be more like curlen * elsz <= 4096, e.g. about the typical page size (or I guess we could get the actual page size with sysconf).

}

STATIC_INLINE void jl_array_grow_at_beg(jl_array_t *a, size_t idx, size_t inc,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,6 @@ void _julia_init(JL_IMAGE_SEARCH rel)
if (total_mem >= (size_t)-1) {
total_mem = (size_t)-1;
}
jl_arr_xtralloc_limit = total_mem / 100; // Extra allocation limited to 1% of total RAM
jl_prep_sanitizers();
void *stack_lo, *stack_hi;
jl_init_stack_limits(1, &stack_lo, &stack_hi);
Expand Down
1 change: 0 additions & 1 deletion src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,6 @@ extern char jl_using_oprofile_jitevents;
#ifdef JL_USE_PERF_JITEVENTS
extern char jl_using_perf_jitevents;
#endif
extern size_t jl_arr_xtralloc_limit;

void jl_init_types(void) JL_GC_DISABLED;
void jl_init_box_caches(void);
Expand Down