Skip to content

Commit 1767dd9

Browse files
committed
fix excess array object alignment
1 parent ae1b469 commit 1767dd9

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

src/array.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,25 @@ static jl_array_t *_new_array_(jl_value_t *atype, uint32_t ndims, size_t *dims,
114114
}
115115

116116
int ndimwords = jl_array_ndimwords(ndims);
117-
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
117+
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t);
118118
if (tot <= ARRAY_INLINE_NBYTES) {
119+
// align data area
119120
if (isunboxed && elsz >= 4)
120-
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT); // align data area
121+
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT);
122+
if (tsz+tot > GC_MAX_SZCLASS) {
123+
// object won't be pool allocated, so take advantage of larger alignment
124+
tsz = JL_ARRAY_ALIGN(tsz, JL_CACHE_BYTE_ALIGNMENT);
125+
}
121126
size_t doffs = tsz;
122127
tsz += tot;
123-
tsz = JL_ARRAY_ALIGN(tsz, JL_SMALL_BYTE_ALIGNMENT); // align whole object
128+
// jl_array_t is large enough that objects will always be aligned 16
124129
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
130+
assert(((size_t)a & 15) == 0);
125131
// No allocation or safepoint allowed after this
126132
a->flags.how = 0;
127133
data = (char*)a + doffs;
128134
}
129135
else {
130-
tsz = JL_ARRAY_ALIGN(tsz, JL_CACHE_BYTE_ALIGNMENT); // align whole object
131136
data = jl_gc_managed_malloc(tot);
132137
// Allocate the Array **after** allocating the data
133138
// to make sure the array is still young
@@ -223,7 +228,7 @@ JL_DLLEXPORT jl_array_t *jl_reshape_array(jl_value_t *atype, jl_array_t *data,
223228
assert(jl_types_equal(jl_tparam0(jl_typeof(data)), jl_tparam0(atype)));
224229

225230
int ndimwords = jl_array_ndimwords(ndims);
226-
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords * sizeof(size_t) + sizeof(void*), JL_SMALL_BYTE_ALIGNMENT);
231+
int tsz = sizeof(jl_array_t) + ndimwords * sizeof(size_t) + sizeof(void*);
227232
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
228233
// No allocation or safepoint allowed after this
229234
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
@@ -304,7 +309,7 @@ JL_DLLEXPORT jl_array_t *jl_string_to_array(jl_value_t *str)
304309
jl_array_t *a;
305310

306311
int ndimwords = jl_array_ndimwords(1);
307-
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t) + sizeof(void*), JL_SMALL_BYTE_ALIGNMENT);
312+
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t) + sizeof(void*);
308313
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, jl_array_uint8_type);
309314
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
310315
a->flags.ndims = 1;
@@ -351,7 +356,7 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array_1d(jl_value_t *atype, void *data,
351356
"unsafe_wrap: pointer %p is not properly aligned to %u bytes", data, align);
352357

353358
int ndimwords = jl_array_ndimwords(1);
354-
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
359+
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t);
355360
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
356361
// No allocation or safepoint allowed after this
357362
a->flags.pooled = tsz <= GC_MAX_SZCLASS;
@@ -418,7 +423,7 @@ JL_DLLEXPORT jl_array_t *jl_ptr_to_array(jl_value_t *atype, void *data,
418423
"unsafe_wrap: pointer %p is not properly aligned to %u bytes", data, align);
419424

420425
int ndimwords = jl_array_ndimwords(ndims);
421-
int tsz = JL_ARRAY_ALIGN(sizeof(jl_array_t) + ndimwords*sizeof(size_t), JL_CACHE_BYTE_ALIGNMENT);
426+
int tsz = sizeof(jl_array_t) + ndimwords*sizeof(size_t);
422427
a = (jl_array_t*)jl_gc_alloc(ct->ptls, tsz, atype);
423428
// No allocation or safepoint allowed after this
424429
a->flags.pooled = tsz <= GC_MAX_SZCLASS;

test/cmdlineargs.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
324324
rm(memfile)
325325
@test popfirst!(got) == " 0 g(x) = x + 123456"
326326
@test popfirst!(got) == " - function f(x)"
327-
@test popfirst!(got) == " 80 []"
327+
@test popfirst!(got) == " 48 []"
328328
if Sys.WORD_SIZE == 64
329329
# P64 pools with 64 bit tags
330330
@test popfirst!(got) == " 16 Base.invokelatest(g, 0)"
@@ -337,7 +337,7 @@ let exename = `$(Base.julia_cmd()) --startup-file=no --color=no`
337337
@test popfirst!(got) == " 8 Base.invokelatest(g, 0)"
338338
@test popfirst!(got) == " 32 Base.invokelatest(g, x)"
339339
end
340-
@test popfirst!(got) == " 80 []"
340+
@test popfirst!(got) == " 48 []"
341341
@test popfirst!(got) == " - end"
342342
@test popfirst!(got) == " - f(1.23)"
343343
@test isempty(got) || got

0 commit comments

Comments
 (0)