Subj : Re: OO compilers and efficiency To : comp.programming From : Jon Harrop Date : Sun Jul 24 2005 09:09 pm Chris Dollin wrote: > Rob Thorpe wrote: >> Let's call the commonly used data structures of each of those languages >> "things", for want of a better word. In many of those languages the >> compiler can (and will) allocate those things on the stack so long as >> no pointer to them could possibly leave the context. > > In what way is it different for Java? If no pointer to a `new Spoo()` > can leave the context, then why can't a Java compiler allocate the > Spoo on the stack? > > [Here, the `compiler` is whatever post-processes the bytecode at > runtime, not the Java source -> classfile compiler.] > > I can see that the analysis for "no pointer could possibly leave > the context" can be harder, much harder, because of polymorphism. > So mayhap a Java compiler can stackallocate much less often for > just this reason. Is that the claim? > > [Do you happen to have cites for any of Lisp, Smalltalk, Pop11, SML, > or OCaml doing stack-allocation, 'cos I'd like to chase them up. > I'm sure I remember reading stuff about Lisp compilers attempting > it; I'm equally - or more - sure that Pop11 doesn't do it.] I wrote a mini Mathematica implementation for Wolfram Research in OCaml (the relevant parts of the real Mathematica are written in C). My code turned out to be unintentionally robust when interpreting recursive Mathematica code because the OCaml compiler allocates mostly on the heap and uses very small stack frames. In contrast, the C allocated mainly on the stack, giving huge stack frames and quickly exhausting stack space. So, while stack allocation can run slightly faster, it is no good if it runs out of resources after only 4ms. So you should be trying to avoid stack allocation! -- Dr Jon D Harrop, Flying Frog Consultancy http://www.ffconsultancy.com .