www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - D gc on local objects

reply "monnoroch" <monnoroch gmail.com> writes:
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?
Apr 16 2014
parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Apr 16 2014
next sibling parent reply "John Colvin" <john.loughran.colvin gmail.com> writes:
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.
Apr 16 2014
parent "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Apr 16 2014
prev sibling parent Paulo Pinto <pjmlp progtools.org> writes:
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
Apr 16 2014