Hacker News new | ask | show | jobs
by lelanthran 1641 days ago
> The optimization you are getting at has not much to do with object size, but subsequent usage.

Size plays a part: it determines whether or not an instance first gets allocated on the heap or the stack[1]. Heap allocation gets expensive in a tight loop.

> If the object reference escapes, it has to be allocated on the heap. Value semantics could/will help here.

The assumption is that we are talking about local-only data objects (not returned or outliving the scope). Forgive (and correct) me if I am under the incorrect assumption.

[1] I'd expect a smart compiler to do this: a data object that requires 1MB should at no point be on the stack, while a data object that requires 32 bytes has no business starting the allocator, causing a context switch to the kernel that faults a new page. The specific thresholds are dependent on the runtime and OS support.