Difference between record and struct

Is it correct to say,
A record is on the heap and a struct on the stack.
The heap garbage collector makes few large unallocations, while the stack makes frequent small unallocations. In general this makes the heap faster.
A record is passed by reference and a struct by value.

Iā€™d say this is mostly correct, yes :+1:

The size of allocations and deallocations depends on the size of data objects and, in the heap case, on the garbage collection strategy. It is safe to assume that heap deallocations will not happen frequently and in larger chunks. It is also safe to assume that stack space is usually more limited, so stack-allocated objects should also be small.

Since the garbage collector has to do a lot of bookkeeping and may have to execute managed clean-up hooks before objects are actually destroyed, major garbage collections can take noticeable amounts of time (as in: garbage collection could interfere with fast reaction guarantees in real-time systems). Stack deallocation on the other hand is extremely fast, because ideally it does nothing but add (or subtract) a byte count to (or from) a single machine register.

Also note that heap-allocated data objects are passed by reference, which is extremely fast since a reference fits into a machine register. Stack-allocated structs, on the other hand, are copied as a whole, which is only fast if they are also small.

Judicious use of small stack-allocated objects can bring a performance benefit. Allocation of large data structures on the stack can result in out-of-memory conditions or performance penalties when they are copied around.

2 Likes