.. include:: ===================================================================== Aspect-Oriented Programming ===================================================================== :Author: Mikael Jansson :Date: 2006-02-23 .. class:: tiny Presenting at Chalmers, Sweden, a paper by: .. class:: small Gregor Kiczales, John Lamping, Anurag Mendhakar, Chris Maeda, Cristina Lopes, Jean-Marc Loingtier and John Irwin .. class:: small Xerox Palo Alto Research Center, 1997 .. |bullet| unicode:: U+02022 .. footer:: Göteborg |bullet| 2006-02-23 Why AOP? =============================== Have you ever felt that you write the same code over and over again, but you can't really parametrize on the parameters of the problem? If you run into a case where... * OOP fails to capture the concept of a system, or * even a continuation or a monad transformer won't help you ... .. class:: incremental ...there will be aspects! Case Study: Image Processing ===================================== Putting images through a list of filters: * tint * or:ing pixels together * flip * scale * ... Easily composable, just apply the next filter on the result of the current filter! Case Study: Image Processing ============================ *What about efficiency?* * Each step at least O(WxH) operation in time * Up to O(WxH) in space * Optimizing requires manual unrolling and combining of filters * Exponential number of filter order configurations .. class:: incremental Not feasible! Aspects vs Components ===================== Observation: Systems are generally built using two constructs * Components are properties of a system which can be encapsulated in a function or an object :small:`Example: an image filter, a bank account or a GUI widget` * Aspects are tangled throughout the system, no clean encapsulation :small:`Example: memory access or synchronization in concurrent programming` Component Language ================== Image processing revisited * Identify and generalize pixel operations for reuse * Identify looping constructs in use * :: orImages xs ys = PerPixel (\(x,y) -> x or y) xs ys tint xs = PerPixel (\(x, y) -> x*y) xs (repeat 1.25) Aspect Language =============== Rewriting filters -- the weaver * Rewrite filter using new information * Add *aspect code* to fuse loops together * Pixel-wise horizontal loops in two filters can be fused together as one:: case type input output of (PerPixel f xs ys, PerPixel f' xs' ys') -> ... To working code =============== Image processing system developed at Xerox. Goals: memory efficiency and code maintainability. * Manual loop unrolling and filter folding: 35K LOC. Lots and lots of work. * Rewriting to aspect-oriented system: 3.5K LOC. Fully automatic. Results: better space efficiency and worse time efficiency, but with *10 times less code!* .. class:: tiny (additionally, the paper includes an example using Java) Why aspects? ============ Measuring efficiency:: reduction in bloat due to tangling = (tangled code size - component program size) / sum of aspect program sizes As such, anything greater than 1 is a success. Putting the the image processing system in concrete figures, we get:: (35000-756) / 352 = 98 Aspect Orientation ================== Writing a system requires three parts: * Component language in which to express the algorithm or process * Aspect language to express the *cross-cutting* aspects of above * Weaver, to introduce the aspects in the system. Can be done either at runtime or at compile time. Expose joint points in the component language for the weaver to, well, weave in the aspects. Open Issues =========== As of writing (1997), not much real evidence of aspects being generally applicable. * What kinds of component and aspect language designs are possible? * How can the aspect language be standardized so it can be shared among several systems? * How to find more applications that would benefit from aspects? * Good practices for aspects? Design, analysis, debugging? * Integrating into existing processes. Summing Up ========== .. class:: large If you're writing code doing a similar task for a heterogenous set of components and behaviours, consider introducing aspects to your system! EOF.