Say I have a value type Foo
, and a method Bar
which accepts a reference to a Foo
. Most languages will allow me to allocate a new Foo
on the stack, and will automatically box it when I try and pass it in to Bar
. However, as far as I am aware, this involves copying the Foo
value onto the heap, and then using that reference.
Is it possible for a language to include a way of allocating a garbage collected object on the stack? When the method ends, the runtime could check if the object is still in use, and only then would it need to allocate the object on the heap, and update the references.
I imagine this would improve performance for methods that do not keep the reference, and it would hinder performance for methods that do.