Thread overview
D gc on local objects
Apr 16, 2014
monnoroch
Apr 16, 2014
Adam D. Ruppe
Apr 16, 2014
John Colvin
Apr 16, 2014
Adam D. Ruppe
Apr 16, 2014
Paulo Pinto
April 16, 2014
I often see, that D developers say something like "remove
allocations from std lib", and it seems, that the main reason to
do it is eliminate gc calls.
What about the idea, that local objects do not use gc at all?
Maby, all temporary variables can be destroyed just like in C++,
when out of scope without stop-the-world?
Here's a silly example:

bool hasDot(string s1, string s2) {
     auto tmp = s1 + s2;
     return tmp.find(".") != -1;
}

Clearly, the tmp variable allocates, but there is no point to do
it via gc, since all memory is allocated and used in specific
scope.

What if dmd could fins those variables and swich gc allocation to
just malloc+constructor call, and destructor+free call at the end
of a scope?
April 16, 2014
This is one of the things the `scope` storage class on local variables can do, but since it isn't implemented properly, it is not memory safe and thus its usage is deprecated.

I really really really want to see scope be fully implemented, including not allowing a reference to the variable to escape the scope, but this is easier said than done.
April 16, 2014
On Wednesday, 16 April 2014 at 16:51:57 UTC, Adam D. Ruppe wrote:
> This is one of the things the `scope` storage class on local variables can do, but since it isn't implemented properly, it is not memory safe and thus its usage is deprecated.
>
> I really really really want to see scope be fully implemented, including not allowing a reference to the variable to escape the scope, but this is easier said than done.

I would love to have a "scope" that works properly, with or without blade-guards to stop me chopping off my hands when the function returns.
April 16, 2014
On Wednesday, 16 April 2014 at 17:14:55 UTC, John Colvin wrote:
> I would love to have a "scope" that works properly, with or without blade-guards to stop me chopping off my hands when the function returns.

The blade guards are the important part though: if you just want the allocation pattern, you can do that fairly easily yourself with plain library code and stuff like scope(exit) or raii destructors.
April 16, 2014
Am 16.04.2014 18:51, schrieb Adam D. Ruppe:
> This is one of the things the `scope` storage class on local variables
> can do, but since it isn't implemented properly, it is not memory safe
> and thus its usage is deprecated.
>
> I really really really want to see scope be fully implemented, including
> not allowing a reference to the variable to escape the scope, but this
> is easier said than done.

Not allowing a variable to escape scope should be easily done with dataflow analysis if I recall correctly.

Now, I don't have any idea how easy/simple it is to implement it in the existing code base, in a compatible way across all three compilers.

So just speaking out of my soap box.

--
Paulo