What is the memory overhead of a mutable struct?

The issue is the call setindex!(testers, MemTester(i), i), i.e. testers[i] = MemTester(i). The first argument is of unknown type (because it is a global variable), therefore julia needs to call into the runtime: It calls a C function that walks the method table and figures out whom to call.

I think the issue is that this C function expects arguments that are pointers to valid heap-allocated julia objects and then extracts the argument types from their headers. We already have a pointer to MemTester(i), but we also need a pointer to an object that contains the integer i and an object header that tells the runtime that it is an integer. We obtain this object via jl_box_int64. That appears to allocate sometimes.

TLDR: Don’t use non-const globals. Really, don’t.

2 Likes