Mutable structs seem cause way more allocations in multithreaded code

Your imagination is at least partially correct. @allocated effectively asks the GC how many bytes have ever been allocated, then executes the expression and then asks again about the overall allocations. The difference is then printed.

This does what you think it does when only a single task is scheduled. As soon as you have multiple tasks, this can be completely misleading.

Note: I am not saying that this is not the problem. I am just hinting, that the used method is flawed in this context.

Your if will allocate. So as soon as one task got a 6400 allocation (because it was not scheduled for some time), the allocations will multiply.

2 Likes