Efficient Layered Method Execution in Contextamber
Total Page:16
File Type:pdf, Size:1020Kb
Efficient Layered Method Execution in ContextAmber Matthias Springer Jens Lincke Robert Hirschfeld Hasso Plattner Institute Hasso Plattner Institute Hasso Plattner Institute University of Potsdam University of Potsdam University of Potsdam [email protected] [email protected] [email protected] ABSTRACT can be as small as 1% [3], whereas in context-oriented pro- We present ContextAmber, a framework for context-oriented gramming, the performance decrease of layer-aware method programming, in Amber Smalltalk, an implementation of dispatch is typically more than 75% [1]. the Smalltalk programming language that compiles to Java- Upon invocation of a layered method, a COP implemen- Script. ContextAmber is implemented using metaprogram- tation without any further optimizations has to perform the ming facilities and supports global, object-wise, and scoped following steps. layer activation. Current COP implementations come at the 1. Compute the receiver's layer composition (active layers). expense of significantly reduced execution performance due to multiple partial method invocations and layer composi- 2. Find and invoke the partial method corresponding to the tion computations every time a layered method is invoked. topmost layer having a partial method for the invoked ContextAmber can reduce this overhead by inlining partial base method. methods, caching layer compositions, and caching inlined 3. Find and invoke the next partial method when a proceed layered methods, resulting in a runtime overhead of about call is encountered. 5% in our vector graphics rendering benchmarks. In this paper, we present optimizations that speed up the Categories and Subject Descriptors execution of COP applications: our implementation, Con- textAmber, avoids unnecessary computations of layer com- D.1.5 [Programming Techniques]: Object-Oriented Pro- positions and reduces the number of method invocations by gramming; inlining partial methods. It is no longer necessary to find D.3.4 [Programming Languages]: Processors|code gen- the next corresponding partial method since only a single eration, optimization fully inlined method is invoked. Note, that finding the next partial method can be expensive in other implementations, General Terms because it requires going through the collection of active Languages layers and checking whether a layer has a method for the re- ceiver's class or one of the receiver's super classes. Our main Keywords contribution is an evaluation of techniques for invalidating inlined methods. Context-oriented programming, partial method inlining, in- ContextAmber is implemented using JavaScript/Smalltalk lined layered method invalidation metaprogramming facilities and does not require changes to the underlying JavaScript interpreter or virtual machine. 1. INTRODUCTION Most findings of this paper can be applied to all program- Layer-based context-oriented programming [6] is a way ming languages that support on-the-fly code generation like to modularize crosscutting concerns that can dynamically Ruby, Smalltalk, or JavaScript. adapt their runtime behavior: layers can be activated and In the remainder of this paper, we discuss vector graph- deactivated at runtime, making it hard to predict the se- ics rendering debugging which serves as a running example quence and nesting of invoked partial methods for a given in this paper (Section 2). In Sections 3-5, we present the layered method at compile time. Performance studies have key ideas of our implementation, along with performance shown that in static aspect-oriented programming, where as- measurements in Section 6. pect weaving is done at compile time, the runtime overhead 2. VECTOR GRAPHICS RENDERING DE- Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are not made or distributed BUGGING WITH COP for profit or commercial advantage and that copies bear this notice and the full citation In this section, we will show how graphical user interfaces on the first page. Copyrights for components of this work owned by others than the can be rendered in Amber Smalltalk using Athens, serving author(s) must be honored. Abstracting with credit is permitted. To copy otherwise, or as a running example for the remainder of this work. republish, to post on servers or to redistribute to lists, requires prior specific permission 1 and/or a fee. Request permissions from [email protected]. Athens is a vector graphics library available for Pharo COP’15 July 05 2015, Prague, Czech Republic. and Amber Smalltalk. It provides a simple API for drawing Copyright is held by the owner/author(s). Publication rights licensed to ACM. 1 ACM 978-1-4503-3654-3/15/07 . $15.00. http://dx.doi.org/10.1145/2786545.2786548. http://smalltalkhub.com/#!/~Pharo/Athens geometric shapes such as rectangles and ellipses. It is also Control Point Layer. possible to define paths which consist of a number of path To make it easier to find drawing bugs, a control point segments. A segment can be a line or any kind of mathe- layer is defined, which draws control points for all segments matical curve, e.g., a B´ezier curve. Once a path object is of a path. Control points are, for example, start and end created, it can be shared in the application and be drawn points of lines and curves, or B´ezier control points. an arbitrary number of times. Consider, for example, that Athens is used to draw the user interface for an application. The user interface consists moveTo: aPoint a small number of distinct user interface elements. All ele- s e l f proceed: a P o i n t . ments of a certain type are separate objects but share the s e l f drawControlPoint : e n d P o i n t . same path objects for drawing, making it possible to enforce drawControlPoint: aPoint a consistent style and to change this style easily. For exam- canvas pushStyle . 2 ple, every time the application draws a button , it uses the canvas fillStyle : ' rgba(0, 0, 0, 0.5) ' . path defined in Figure 1. canvas fillRect :( a P o i n t − (5 @ 5 ) c o r n e r : 10 @ 1 0 ). canvas popStyle . p a t h := surface createPath :[: b u i l d e r j b u i l d e r Figure 3: Some partial methods for drawing control points. a b s o l u t e ; m o v e T o : 5 @ 0 ; l i n e T o : 45 @ 0 ; Figure 3 shows some of the partial methods defined for the c w A r c T o : 50 @ 5 a n g l e : 9 0 ; control point layer. AthensPath is the base class for these l i n e T o : 50 @ 1 5 ; c w A r c T o : 45 @ 2 0 a n g l e : 9 0 ; methods. ContextAmber allows programmers to add new l i n e T o : 5 @ 2 0 ; methods to classes. For example, drawControlPoint: is not c w A r c T o : 0 @ 1 5 a n g l e : 9 0 ; defined on AthensPath. By activating the control point layer l i n e T o : 0 @ 5 ; c w A r c T o : 5 @ 0 a n g l e : 90 ] . on the path defined in Figure 1, it is possible to show con- trol points only for buttons, but not for other user interface c a n v a s d r a w : p a t h . elements. Figure 1: Drawing a rectangle with rounded corners in 3. IMPLEMENTATION Athens. ContextAmber is our framework for context-oriented pro- gramming and used to evaluate and benchmark the concepts Figure 2 shows how Athens draws paths. AthensCanvas presented in this paper. It runs on top of Amber Smalltalk4, is the entry point for all drawings and has methods for per- an implementation of the Smalltalk programming language forming primitive operations such as moving the pen some- that compiles to JavaScript. where on the canvas. The draw: method can be used to draw complex shapes such as paths. It delegates the draw- Amber Smalltalk Object Model. ing to that path object which acts as a proxy here. A path The Amber Smalltalk object model is minimalistic but fol- contains a list of segments that is created when the path is lows closely the Smalltalk-80 object model. Every Smalltalk created. During drawing, these segments are replayed using object is at the same time a JavaScript object. Instance- double dispatch. In our example, the path object simply 3 of and inheritance relationships are implemented using the forwards these commands to the canvas object . JavaScript prototype hierarchy, i.e., every object has a pro- totype containing the instance methods for the correspond- : AthensMove : AthensCWArc : AthensCanvas : AthensPath Segment Segment ing class, and that object's prototype contains the instance draw: aPath methods of the super class. Message passing is implemented drawOn: self by JavaScript method calls, where colons in the selector are sendCommandTo: self replaced with underscores. moveTo: ... When a Smalltalk method is compiled, its source code is moveTo: ... first converted into an AST. Then a semantic analyzer per- forms checks and determines the type and scope of variables. sendCommandTo: self The AST is then converted into an intermediate representa- cwArcTo: ... angle: ... tion (IR) which is similar to the AST but distinguishes be- cwArcTo: ... angle: ... tween local returns and non-local returns, for example. An IR tree can directly be transformed into JavaScript code. ... ContextAmber performs method inlining on the AST level, making it possible to use the step-through debugger, which Figure 2: Drawing paths in Athens. operates on the AST. 2To simplify this example, we assume that all buttons have Layer Activation. the same size. In ContextAmber, layers can be activated globally, within 3It is possible to extend Athens for different drawing back- a certain scope, and for a single object.