Resize!(matrix)

@foobar_lv2, what’s you opinion on the alignment issue? Julia seems to aim for 64-byte alignments (because it’s the cache-line size?), but it looks like calloc will only guarantee 16-byte alignment. Since unsafe_wrap doesn’t allow for specifying an offset, I’m not sure how best to handle this.

Giant-sized allocations “should ™” give you memory at the start of a page, and should also give you a larger page-size (which saves you TLB misses). You could also use malloc, if you don’t need guaranteed zero initialization (you “should” get virgin zero-initialized lazy virtual memory anyway, for giant allocations).

As far as I understand, the worse alignment should not be a problem, normally, and the wrapped array should work just fine. Except if your code is very hand-crafted to exploit the cache optimally (say e.g. judy arrays).

If you get unaligned memory, and your application gets too slow with this, you can try another dirty (untested) trick (only if you wrap a vector instead of a higher-dim array): You increment the pointer until it is well-aligned, and wrap around that one. Finally, when you are done and want to wrap your “good” vector that takes ownership of the array, you wrap around the incremented pointer, and then set the “offset” field of your array. That way, the array will look like you removed the first couple of elements. And, ta-da, free() will be called by the garbage collector with the correct address (pointer(vector) - offset) when the giant-vector is finally collected.

Yes, I was wondering if that was possible - how user-accessible is the array offset? Are there any Julia-internal C-functions to manipulate it, or do I have to fiddle around with the array struct directly (which, I guess, is not guaranteed to stay unchanged over time)?

I would just write to it, via

ap = pointer_from_objref(some_vec)
unsafe_store!(convert(Ptr{Int32},ap+20), offset_new)

All the dirty tricks will break anyway, whenever julia.h changes sufficiently. Of course, it would be preferable to use something (cxx.jl?) that parses julia.h instead of you looking into julia.h and hand-coding everything into your source file (after hand-computing all offsets).

Thanks - well, I guess the array struct will stay unchanged at least for a while, and one can always check the Julia version.