Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Escape analysis

From Wikipedia, the free encyclopedia
Determination of the dynamic scope of pointers
icon
This articleneeds additional citations forverification. Please helpimprove this article byadding citations to reliable sources. Unsourced material may be challenged and removed.
Find sources: "Escape analysis" – news ·newspapers ·books ·scholar ·JSTOR
(August 2013) (Learn how and when to remove this message)

Incompiler optimization,escape analysis is a method for determining the dynamic scope ofpointers – where in the program a pointer can be accessed. It is related topointer analysis andshape analysis.

When a variable (or an object) is allocated in asubroutine, apointer to the variable canescape to otherthreads of execution, or to calling subroutines. If an implementation usestail call optimization (usually required forfunctional languages), objects may also be seen as escaping tocalled subroutines. If a language supports first-classcontinuations (as doScheme andStandard ML of New Jersey), portions of thecall stack may also escape.

If a subroutine allocates an object and returns a pointer to it, the object can be accessed from undetermined places in the program – the pointer has "escaped". Pointers can also escape if they are stored in global variables or other data structures that, in turn, escape the current procedure.

Escape analysis determines all the places where a pointer can be stored and whether the lifetime of the pointer can be proven to be restricted only to the current procedure and/or thread.

Optimizations

[edit]

A compiler can use the results of escape analysis as a basis for optimizations:[1]

  • Convertingheap allocations tostack allocations.[2] If an object is allocated in a subroutine, and a pointer to the object never escapes, the object may be a candidate for stack allocation instead of heap allocation. In garbage-collected languages this can reduce how often the collector needs to run.
  • Synchronization elision. If an object is found to be accessible from one thread only, operations on the object can be performed without synchronization.
  • Breaking up objects orscalar replacement.[3] An object may be found to be accessed in ways that do not require the object to exist as a sequential memory structure. This may allow parts (or all) of the object to be stored in CPU registers instead of in memory.

Practical considerations

[edit]

Inobject-oriented programming languages,dynamic compilers are particularly good candidates for performing escape analysis. In traditionalstatic compilation,method overriding can make escape analysis impossible, as any called method might be overridden by a version that allows a pointer to escape. Dynamic compilers can perform escape analysis using the available information on overloading, and re-do the analysis when relevant methods are overridden by dynamic code loading.[1]

The popularity of theJava programming language has made escape analysis a target of interest. Java's combination of heap-only object allocation, built-in threading, the SunHotSpot dynamic compiler, andOpenJ9'sjust-in-time compiler (JIT) creates a candidate platform for escape analysis related optimizations (seeEscape analysis in Java). Escape analysis is implemented in Java Standard Edition 6. Some JVMs support a stronger variant of escape analysis calledpartial escape analysis that makes scalar replacement of an allocated object possible even if the object escapes in some paths of a function.[4]

Example (Java)

[edit]
classMain{publicstaticvoidmain(String[]args){example();}publicstaticvoidexample(){Foofoo=newFoo();//allocBarbar=newBar();//allocbar.setFoo(foo);}}classFoo{}classBar{privateFoofoo;publicvoidsetFoo(Foofoo){this.foo=foo;}}

In this example, two objects are created (commented with alloc), and one of them is given as an argument to a method of another. The methodsetFoo() stores a reference to a received Foo object. If the Bar object was on the heap then the reference to Foo would escape. But in this case a compiler can determine, with escape analysis, that the Bar object itself does not escape the invocation ofexample(). As a result, the reference to Foo cannot escape either, and the compiler can safely allocate both objects on the stack.

Examples (Scheme)

[edit]

In the following example, the vectorp does not escape intog, so it can be allocated on the stack and then removed from the stack before callingg.

(define(fx)(let((p(make-vector10000)))(fill-vector-with-good-stuffp)(g(vector-refp7023))))

If, however, we had

(define(fx)(let((p(make-vector10000)))(fill-vector-with-good-stuffp)(gp)))

then eitherp would need to be allocated on the heap or (ifg is known to the compiler whenf is compiled, and behaves well) allocated on the stack in such a fashion that it can remain in place wheng is called.

If continuations are used to implement exception-like control structures, escape analysis can often detect this to avoid having to actually allocate a continuation and copy the call stack into it. For example, in

;;Reads scheme objects entered by the user. If all of them are numbers,;;returns a list containing all of them in order. If the user enters one that;;is not a number, immediately returns #f.(define(getnumlist)(call/cc(lambda(continuation)(define(get-numbers)(let((next-object(read)))(cond((eof-object?next-object)'())((number?next-object)(consnext-object(get-numbers)))(else(continuation#f)))))(get-numbers))))

escape analysis will determine that the continuation captured bycall/cc doesn't escape, so no continuation structure needs to be allocated, and invoking the continuation by callingcontinuation can be implemented by unwinding the stack.

See also

[edit]

References

[edit]
  1. ^abT. Kotzmann and H. Mössenböck, “Escape analysis in the context of dynamic compilation and deoptimization,” in Proceedings of the 1st ACM/USENIX international conference on Virtual execution environments, New York, NY, USA, 2005, pp. 111–120.
  2. ^Blanchet, Bruno (November 2003)."Escape Analysis for JavaTM: Theory and Practice".ACM Transactions on Programming Languages and Systems.25 (6):713–775.doi:10.1145/945885.945886.ISSN 0164-0925.
  3. ^Kotzmann, Thomas; Mössenböck, Hanspeter (March 2007). "Run-Time Support for Optimizations Based on Escape Analysis".International Symposium on Code Generation and Optimization (CGO'07). pp. 49–60.CiteSeerX 10.1.1.394.5944.doi:10.1109/CGO.2007.34.ISBN 978-0-7695-2764-2.S2CID 16011497.
  4. ^Stadler, Lukas; Würthinger, Thomas; Mössenböck, Hanspeter (2014). "Partial Escape Analysis and Scalar Replacement for Java".Proceedings of Annual IEEE/ACM International Symposium on Code Generation and Optimization - CGO '14. pp. 165–174.doi:10.1145/2581122.2544157.ISBN 9781450326704.
Basic block
Loop
Data-flow
analysis
SSA-based
Code generation
Functional
Global
Other
Static analysis
Retrieved from "https://en.wikipedia.org/w/index.php?title=Escape_analysis&oldid=1327254467"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp