Jump to content

Aspect weaver: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Shirik (talk | contribs)
Shirik (talk | contribs)
→‎Performance: Explain jargon
Line 81: Line 81:


==Performance==
==Performance==
Aspect weavers' performance, as well as the performance of the code that they produce, has been a subject of analysis. It is preferable that the improvement in modularity supplied by aspect weaving does not impact run-time performance. Aspect weavers are able to perform aspect-specific optimizations.<ref name="Aspect Optimization">{{cite journal|title=On Minimal Overhead Operating Systems andAspect-Oriented Programming|last1=Gal|first1=Andreas|last2=Schröder-Preikschat|first2=Wolfgang|last3=Spinczyk|first3=Olaf|url=http://fusion.cs.uni-magdeburg.de/pubs/ecoop2001.pdf|format=PDF|journal=Proceedings of the 4th Workshop on Object-Orientation and Operating Systems at the 15th European Conference on Object-Oriented Programming (ECOOP-OOOSW)|year=2001|accessdate=27 January 2010}}</ref> While traditional optimizations such as the elimination of unused special [[variable (programming)|variables]] from aspect code can be done during [[compile-time]], some optimizations, such as the optimization of replacing the AspectJ keyword <code>thisJoinPoint</code> with the more efficient and [[static variable|static]] keyword <code>thisJoinPointStaticPart</code>, can only be done by the aspect weaver. This optimization avoids the creation of a join point [[object (computer science)|object]] on every execution.<ref name="Weaving" />{{rp|2}} Studies have shown that the unnecessary creation of join point objects in AspectJ can lead to a performance overhead of 5% at run-time, while performance degradation is only approximately 1% when this object is not created.<ref name="AspectJ Middleware">{{cite journal|title=Using AspectJ for Component Integration in Middleware|last1=Colyer|first1=Adrian|last2=Clement|first2=Andy|last3=Bodkin|first3=Ron|last4=Hugunin|first4=Jim|url=http://wwwx.cs.unc.edu/~lee/docs/pra03-colyer.pdf|format=PDF|journal=Companion of the 18th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications|pages=339&ndash;344|doi=10.1145/949344.949440|year=2003|accessdate=23 January 2009}}</ref>{{rp|2}}
Aspect weavers' performance, as well as the performance of the code that they produce, has been a subject of analysis. It is preferable that the improvement in modularity supplied by aspect weaving does not impact run-time performance. Aspect weavers are able to perform aspect-specific optimizations.<ref name="Aspect Optimization">{{cite journal|title=On Minimal Overhead Operating Systems andAspect-Oriented Programming|last1=Gal|first1=Andreas|last2=Schröder-Preikschat|first2=Wolfgang|last3=Spinczyk|first3=Olaf|url=http://fusion.cs.uni-magdeburg.de/pubs/ecoop2001.pdf|format=PDF|journal=Proceedings of the 4th Workshop on Object-Orientation and Operating Systems at the 15th European Conference on Object-Oriented Programming (ECOOP-OOOSW)|year=2001|accessdate=27 January 2010}}</ref> While traditional optimizations such as the elimination of unused special [[variable (programming)|variables]] from aspect code can be done during [[compile-time]], some optimizations. For example, AspectJ contains two similar but distinct keywords, <code>thisJoinPoint</code>, which contains information about this particular instance of woven code, and <code>thisJoinPointStaticPart</code>, which contains information common to all instances of code relevant to that set of advice. The optimization of replacing <code>thisJoinPoint</code> with the more efficient and [[static variable|static]] keyword <code>thisJoinPointStaticPart</code> can only be done by the aspect weaver. By performing this replacement, the woven program avoids the creation of a join point [[object (computer science)|object]] on every execution.<ref name="Weaving" />{{rp|2}} Studies have shown that the unnecessary creation of join point objects in AspectJ can lead to a performance overhead of 5% at run-time, while performance degradation is only approximately 1% when this object is not created.<ref name="AspectJ Middleware">{{cite journal|title=Using AspectJ for Component Integration in Middleware|last1=Colyer|first1=Adrian|last2=Clement|first2=Andy|last3=Bodkin|first3=Ron|last4=Hugunin|first4=Jim|url=http://wwwx.cs.unc.edu/~lee/docs/pra03-colyer.pdf|format=PDF|journal=Companion of the 18th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications|pages=339&ndash;344|doi=10.1145/949344.949440|year=2003|accessdate=23 January 2009}}</ref>{{rp|2}}


Compile-time performance is generally worse in aspect weavers than their traditional compiler counterparts due to the additional work necessary for locating methods which match the specified pointcuts. A study done showed that the AspectJ compiler ajc is about 34% slower than the [[Sun Microsystems]] [[Java 1.3]] compiler and about 62% slower than the [[Java 1.4]] compiler.<ref name="Weaving" />{{rp|7}}
Compile-time performance is generally worse in aspect weavers than their traditional compiler counterparts due to the additional work necessary for locating methods which match the specified pointcuts. A study done showed that the AspectJ compiler ajc is about 34% slower than the [[Sun Microsystems]] [[Java 1.3]] compiler and about 62% slower than the [[Java 1.4]] compiler.<ref name="Weaving" />{{rp|7}}

Revision as of 18:47, 29 January 2010

Template:Infobox programming tool An aspect weaver is a metaprogramming utility designed to take instructions specified by aspects in aspect-oriented languages in an effort to generate the final implementation code. The aspect weaver integrates aspects into the locations specified by the software as a pre-compilation step. By merging aspects and classes, the aspect weaver generates a woven class which can then be compiled as would code from a typical object-oriented language language.[1]: 4 

Aspect weavers take advice specified through the use of pointcuts and join points to indicate what methods should be handled. The implementation of the aspect then specifies whether the related code should be added before, after, or throughout the related methods. By doing this, aspect weavers improve modularity, keeping code in one place that would otherwise have been interspersed throughout various, unrelated classes.

Motivation

Many programming languages are already widely accepted and understood. However, the desire to create radically different programming languages to support the aspect-oriented programming paradigm is not significant due to business-related concerns. Use of an entirely new language relies on a business's ability to acquire new developers. Additionally, the existing code base of a business would need to be discarded. Finally, a business would need to acquire a new toolchain for development, which is often both an expense both money-wise and time-wise.[2]: 7 

To address these business concerns, an aspect weaver enables the use of widely adopted languages like Java with aspect-oriented programming through minor adaptations such as AspectJ which work with existing tools.[2]: 5  Instead of developing an entirely new language, the aspect weaver interprets the extensions defined by AspectJ and builds "woven" Java code which can then be used by any existing Java compiler. This enforces that any existing object-oriented code will still be valid aspect-oriented code and that development will feel like a natural extension of the object-oriented language.[3]: 3  The AspectC++ programming language extends C++ through the use of an aspect weaver, offering the additional efficiency over AspectJ which is necessary for embedded systems while still retaining the benefits of aspect-oriented programming.[4]

Implementation

Aspect weavers operate by taking instructions specified by aspects, known as advice, and distributing it throughout the various classes in the program automatically. The result of the weaving process is a set of classes with the same names as the original classes but with additional code injected into the classes' functions automatically. The advice specifies the exact location and functionality of the injected code.[5]: 1 

Through this weaving process, aspect weavers allow for code which would have otherwise been duplicated across classes. By eliminating this duplication, aspect weavers promote modularity of cross-cutting concerns.[5]: 7  Aspects define the implementation code which would have otherwise been duplicated and then use pointcuts and join points to define the advice. During weaving, the aspect weaver uses the pointcuts and join points, known as a pointcut designator, to identify the positions in candidate classes at which the implementation should be injected.[6]: 2  The implementation is then injected into the classes at the points identified, thus permitting the code to be executed at the appropriate times without relying on manual duplication by the programmer.[7]

aspect Logger {
   pointcut method() : execution(* *(..));
   before() : method() {
      System.out.println("Entering " + 
         thisJoinPoint.getSignature().toString());
   }
   after() : method() { 
      System.out.println("Leaving " + 
         thisJoinPoint.getSignature().toString());
   }
}
public class Foo {
   public void bar() {
      System.out.println("Executing Foo.bar()");
   }
   public void baz() {
      System.out.println("Executing Foo.baz()");
   }
}
A sample aspect and class defined in the AspectJ programming language
public class Foo {
   public void bar() {
      System.out.println("Entering Foo.bar()");
      System.out.println("Executing Foo.bar()");
      System.out.println("Leaving Foo.bar()");
   }
   public void bar() {
      System.out.println("Leaving Foo.baz()");
      System.out.println("Executing Foo.baz()");
      System.out.println("Leaving Foo.baz()");
   }
}
The woven class that results from executing an aspect weaver on the above sample

Weaving in AspectJ

In the programming language AspectJ, pointcuts, join points, and the modularized code are defined in an aspect block similar to that of Java classes. Classes are defined using Java syntax. The weaving process consists of executing the aspect advice to produce only a set of generated classes that have the aspect implementation code woven into it.[8]

The example at right shows a potential implementation of an aspect which logs the entry and exit of all methods. Without an aspect weaver, this feature would necessitate duplication of code in the class for every method. Instead, the entry and exit code is defined solely within the aspect.[8]

The aspect weaver analyzes the advice specified by the pointcut in the aspect and uses that advice to distribute the implementation code into the defined class. The code differs slightly in each method due to slight variances in requirements for the method (as the method identifier has changed). The aspect weaver determines the appropriate code to generate in each situation as defined by the implementation advice and then injects it into methods matching the specified pointcut.[9]

Weaving to bytecode

Instead of generating a set of woven source code, some aspect weavers instead weave the aspects and classes together directly into bytecode, acting both as the aspect weaver and compiler.[9]: 2  While it is expected that the performance of aspect weavers which also perform the compilation process will require more computation time due to the weaving process involved, it is expected that this performance can be improved as aspect weavers' compiler implementations are relatively new, while traditional compilers have been thoroughly tested and optimized.[9]: 7 

Run-time weaving

Newer developments in aspect-oriented programming have revealed the potential to incorporate the concepts of just-in-time compilation with aspect-oriented programming.[10] At run-time, an aspect weaver could translate aspects in a more efficient manner than traditional, static weaving approaches. Using AspectJ on a Java Virtual Machine, dynamic weaving of aspects at run-time has been shown to improve code performance by 26%.[11] However, implementing this capability relies on the development of a new virtual machine, contrary to one of the original design goals of AspectJ.[3]: 3 

To accomplish just-in-time weaving, a change to the virtual machine that executes the compiled bytecode is necessary. A proposed solution for AspectJ uses a layered approach which builds upon the existing Java Virtual Machine to add support for join point management and callbacks to a Dynamic Aspect-Oriented Programming Engine.[10] An alternative implementation uses a weaving engine that uses breakpoints to halt execution at the pointcut, select an appropriate method, embed it into the application, and continue.[11]: 11  The use of breakpoints in this manner has been shown to reduce performance due to a very large number of context switches.[11]: 17 

Performance

Aspect weavers' performance, as well as the performance of the code that they produce, has been a subject of analysis. It is preferable that the improvement in modularity supplied by aspect weaving does not impact run-time performance. Aspect weavers are able to perform aspect-specific optimizations.[12] While traditional optimizations such as the elimination of unused special variables from aspect code can be done during compile-time, some optimizations. For example, AspectJ contains two similar but distinct keywords, thisJoinPoint, which contains information about this particular instance of woven code, and thisJoinPointStaticPart, which contains information common to all instances of code relevant to that set of advice. The optimization of replacing thisJoinPoint with the more efficient and static keyword thisJoinPointStaticPart can only be done by the aspect weaver. By performing this replacement, the woven program avoids the creation of a join point object on every execution.[9]: 2  Studies have shown that the unnecessary creation of join point objects in AspectJ can lead to a performance overhead of 5% at run-time, while performance degradation is only approximately 1% when this object is not created.[13]: 2 

Compile-time performance is generally worse in aspect weavers than their traditional compiler counterparts due to the additional work necessary for locating methods which match the specified pointcuts. A study done showed that the AspectJ compiler ajc is about 34% slower than the Sun Microsystems Java 1.3 compiler and about 62% slower than the Java 1.4 compiler.[9]: 7 

See also

References

  1. ^ Suzuki, Junichi; Yamamoto, Yoshikazu (1999). Moreira, A. M.; Demeyer, Moreira (eds.). "Extending UML with Aspects: Aspect Support in the Design Phase" (PDF). Proceedings of the Workshop on Object-Oriented Technology. 1743: 299–300. {{cite journal}}: Unknown parameter |month= ignored (help)
  2. ^ a b Kiczales, Gregor; Hilsdale, Erik; Hugunin, Jim; Kersten, Mik; Palm, Jeffrey; Griswold, William (2001). "Getting Started with AspectJ". Communications of the ACM. 44 (10). ACM. doi:10.1145/383845.383858. {{cite journal}}: Unknown parameter |month= ignored (help)
  3. ^ a b Kiczales, Gregor; Hilsdale, Erik; Hugunin, Jim; Kersten, Mik; Palm, Jeffery; Griswold, William G. (2001). "An Overview of AspectJ" (PDF). Proceedings of the European Conference on Object-Oriented Programming: 327–354. doi:10.1007/3-540-45337-7_18.
  4. ^ Spinczyk, Olaf; Gal, Andreas; Schröder-Preikschat, Wolfgang (2002). "AspectC++: An Aspect-Oriented Extension to the C++ Programming Language" (PDF). Proceedings of the Fortieth International Conference on Tools Pacific. 21: 53–60.
  5. ^ a b Wand, Michael; Kiczales, Gregor; Dutchyn, Christopher (2004). "A semantics for advice and dynamic join points in aspect-oriented programming" (PDF). ACM Transactions on Programming Languages and Systems (TOPLAS). ACM: 890–910. Retrieved 23 January 2009.
  6. ^ Viega, John; Voas, Jeffrey (2000). "Can Aspect-Oriented Programming lead to More Reliable Software?". IEEE Software. 17 (6): 19–21. doi:10.1109/52.895163. {{cite journal}}: Unknown parameter |month= ignored (help)
  7. ^ Spinczyk, Olaf; Lohmann, Daniel (October 2007). "The design and implementation of AspectC++". Knowledge-Based Systems. 20 (7): 636–651. doi:10.1016/j.knosys.2007.05.004. {{cite journal}}: |access-date= requires |url= (help)
  8. ^ a b Wang, Yi; Zhao, Jianjun (July 2007). "Specifying Pointcuts in AspectJ" (PDF). Proceedings of the 21st Annual International Computer Software and Applications Conference. 2: 5–10. doi:10.1109/COMPSAC.2007.196. Retrieved 23 January 2010.
  9. ^ a b c d e Hilsdale, Erik; Hugunin, Jim (2004). "Advice Weaving in AspectJ" (PDF). Proceedings of the 3rd international conference on Aspect-oriented software development. ACM: 24–35. doi:10.1145/976270.976276. Retrieved 23 January 2009.
  10. ^ a b Popovici, Andrei; Alonso, Gustavo; Gross, Thomas (2003). "Just-In-Time Aspects: Efficient Dynamic Weaving for Java". Proceedings of the 2nd International Conference on Aspect-Oriented Software Development. ACM: 100 109. doi:10.1145/643603.643614. Cite error: The named reference "JITA" was defined multiple times with different content (see the help page).
  11. ^ a b c Sato, Yoshiki; Chiba, Shigeru; Tatsubori, Michiaki (2003). "A Selective, Just-In-Time Aspect Weaver" (PDF). Proceedings of the 2nd international conference on Generative programming and component engineering: 189–208. {{cite journal}}: Unknown parameter |month= ignored (help)
  12. ^ Gal, Andreas; Schröder-Preikschat, Wolfgang; Spinczyk, Olaf (2001). "On Minimal Overhead Operating Systems andAspect-Oriented Programming" (PDF). Proceedings of the 4th Workshop on Object-Orientation and Operating Systems at the 15th European Conference on Object-Oriented Programming (ECOOP-OOOSW). Retrieved 27 January 2010.
  13. ^ Colyer, Adrian; Clement, Andy; Bodkin, Ron; Hugunin, Jim (2003). "Using AspectJ for Component Integration in Middleware" (PDF). Companion of the 18th annual ACM SIGPLAN conference on Object-oriented programming, systems, languages, and applications: 339–344. doi:10.1145/949344.949440. Retrieved 23 January 2009.