While we're on the subject of escape analysis...

Benji Smith dlanguage at benjismith.net
Thu Oct 30 19:20:33 PDT 2008


Consider this code:

    class MyObject {

       public int x() {
          return obj.a().b().c().d();
       }

       public MyObject a() { ... }
       public MyObject b() { ... }
       public MyObject c() { ... }
       public int d() { ... }

    }

This code creates three temporary objects that cannot possibly escape 
the scope of the "x" function. Although the user might be content with 
those object being allocated on the heap, the compiler could decide to 
allocate them on the stack instead, and the programmer would be 
blissfully unaware of the switcharoo.

Of course, this kind of optimization would require an interprocedural 
graph-coloring algorithm to determine whether to use the heap allocator 
or the stack allocator for every allocation. And I don't know whether 
the current optimizer does any interprocedural analysis at all. (Though 
it does inline short methods, which seems roughly equivalent, so I dunno.)

Thoughts?

--benji



More information about the Digitalmars-d mailing list