Possible quick win in GC?

Freddy via Digitalmars-d digitalmars-d at puremagic.com
Sun Sep 28 13:09:34 PDT 2014


On Sunday, 28 September 2014 at 17:47:42 UTC, Abdulhaq wrote:
> Here's a code snippet which mopefully makes things a bit 
> clearer:
>
>
> /**
> * In this example the variable foo can be statically analysed 
> as safe to go on the stack.
> * The new instance of Bar allocated in funcLevelB is only 
> referred to by foo. foo can
> * be considered a root 'scoped' variable and the GC can delete 
> both foo and the new Bar()
> * when foo goes out of scope. There is no need (except when 
> under memory pressure) for
> * the GC to scan the band created for foo and it's related 
> child allocations.
> */
>
> import std.stdio;
>
> class Bar {
> 	public:
> 	int x;
> 	
> 	this(int x) {
> 		this.x = x;
> 	}
> }
>
> class Foo {
> 	public:
> 	Bar bar;
> }
>
> void funcLevelA() {
> 	Foo foo = new Foo(); // static analysis could detect this as 
> able to go on the stack
> 	funcLevelB(foo);
> 	writeln(foo.bar.x);
> }
>
> void funcLevelB(Foo foo) {
> 	foo.bar = new Bar(12); // this allocated memory is only 
> referred to by foo, which
> 						   // static analysis has established can go on the stack	
> }
>
> void main() {
> 	funcLevelA();
> }
You mean this?
https://en.wikipedia.org/wiki/Escape_analysis


More information about the Digitalmars-d mailing list