Skip to content

Commit

Permalink
svec: optimize allocations in debug
Browse files Browse the repository at this point in the history
The svec set method needs to do a little bit more work (for asserts and
write barriers), which is not necessary if we know it was just allocated.
  • Loading branch information
vtjnash committed Dec 13, 2021
1 parent 3174a37 commit 52300c2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -914,7 +914,7 @@ STATIC_INLINE jl_value_t *jl_svecset(
// TODO: while svec is supposedly immutable, in practice we sometimes publish it first
// and set the values lazily. Those users should be using jl_atomic_store_release here.
jl_svec_data(t)[i] = (jl_value_t*)x;
if (x) jl_gc_wb(t, x);
jl_gc_wb(t, x);
return (jl_value_t*)x;
}
#endif
Expand Down
22 changes: 10 additions & 12 deletions src/simplevector.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ JL_DLLEXPORT jl_svec_t *(ijl_svec)(size_t n, ...)
if (n == 0) return jl_emptysvec;
va_start(args, n);
jl_svec_t *jv = jl_alloc_svec_uninit(n);
for(size_t i=0; i < n; i++)
for (size_t i = 0; i < n; i++)
jl_svecset(jv, i, va_arg(args, jl_value_t*));
va_end(args);
return jv;
Expand All @@ -38,7 +38,7 @@ JL_DLLEXPORT jl_svec_t *jl_svec1(void *a)
jl_svec_t *v = (jl_svec_t*)jl_gc_alloc(ct->ptls, sizeof(void*) * 2,
jl_simplevector_type);
jl_svec_set_len_unsafe(v, 1);
jl_svecset(v, 0, a);
jl_svec_data(v)[0] = (jl_value_t*)a;
return v;
}

Expand All @@ -48,8 +48,8 @@ JL_DLLEXPORT jl_svec_t *jl_svec2(void *a, void *b)
jl_svec_t *v = (jl_svec_t*)jl_gc_alloc(ct->ptls, sizeof(void*) * 3,
jl_simplevector_type);
jl_svec_set_len_unsafe(v, 2);
jl_svecset(v, 0, a);
jl_svecset(v, 1, b);
jl_svec_data(v)[0] = (jl_value_t*)a;
jl_svec_data(v)[1] = (jl_value_t*)b;
return v;
}

Expand All @@ -67,26 +67,24 @@ JL_DLLEXPORT jl_svec_t *jl_alloc_svec(size_t n)
{
if (n == 0) return jl_emptysvec;
jl_svec_t *jv = jl_alloc_svec_uninit(n);
for(size_t i=0; i < n; i++)
jl_svecset(jv, i, NULL);
memset(jl_assume_aligned(jl_svec_data(jv), sizeof(void*)), 0, n * sizeof(void*));
return jv;
}

JL_DLLEXPORT jl_svec_t *jl_svec_copy(jl_svec_t *a)
{
size_t i, n=jl_svec_len(a);
size_t n = jl_svec_len(a);
jl_svec_t *c = jl_alloc_svec_uninit(n);
for(i=0; i < n; i++)
jl_svecset(c, i, jl_svecref(a,i));
memmove_refs((void**)jl_svec_data(c), (void**)jl_svec_data(a), n);
return c;
}

JL_DLLEXPORT jl_svec_t *jl_svec_fill(size_t n, jl_value_t *x)
{
if (n==0) return jl_emptysvec;
if (n == 0) return jl_emptysvec;
jl_svec_t *v = jl_alloc_svec_uninit(n);
for(size_t i=0; i < n; i++)
jl_svecset(v, i, x);
for (size_t i = 0; i < n; i++)
jl_svec_data(v)[i] = x;
return v;
}

Expand Down

0 comments on commit 52300c2

Please sign in to comment.