News for hackers. Stuff that matters.
Re:Garbage Collection by vidarh on Sunday December 01, 2019 @03:54AM (#21674605)
Depending on the language, you can do this for some things. The fundamental challenge is escape analysis: when this function returns, what might have been retained elsewhere?

If you can answer that, then you can optimize accordingly.

E.g you can choose to allocate objects that won't be retained on the stack instead of the heap, or in separate blocks.

You can even potentially benefit from this even if you can't 100% know. If you use a generational collector, an object that almost certainly escapes could bypass the nursery, for example.

Re:Garbage Collection by ufo on Sunday December 01, 2019 @06:04AM (#21674906)
There are some optimizations that do thus. When you know the object won't outlive the function, allocate it on thw stack instead of on the heap

And on the more extreme case you can even replace the objects with a bunch of local variables. For example, replace a Point object allocated on the stack with a pair of variables gor the x and y coordinates.