Www.itu.dk Zero-overhead composable aspects for.NET Joint work with (i.e. most work done by) Rasmus Johansen and Stephan Spangenberg Peter Sestoft IT University.

Slides:



Advertisements
Similar presentations
Ahead of Time Dynamic Translation PreJit/NGEN by any other name George Bosworth Microsoft MRE04 March 21, 2004.
Advertisements

Mike Barnett RSDE Microsoft Research Nikolai Tillmann RSDE Microsoft Research TL51.
Containers CMPS Reusable containers Simple data structures in almost all nontrivial programs Examples: vectors, linked lists, stacks, queues, binary.
SYMBOL TABLES &CODE GENERATION FOR EXECUTABLES. SYMBOL TABLES Compilers that produce an executable (or the representation of an executable in object module.
Aspect Oriented Programming. AOP Contents 1 Overview 2 Terminology 3 The Problem 4 The Solution 4 Join point models 5 Implementation 6 Terminology Review.
Feature requests for Case Manager By Spar Nord Bank A/S IBM Insight 2014 Spar Nord Bank A/S1.
Generic programming in Java
1 Generics and Using a Collection Generics / Parameterized Classes Using a Collection Customizing a Collection using Inheritance Inner Classes Use of Exceptions.
AspectJ2EE/Clasa Israel Institute of Technology The Computer Science department Itay Maman.
Cmp Sci 187: Midterm Review Based on Lecture Notes.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
1 Integrated Development Environment Building Your First Project (A Step-By-Step Approach)
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
FEN 2012UCN Technology - Computer Science 1 Data Structures and Collections Principles revisited.NET: –Two libraries: System.Collections System.Collections.Generics.
1 Stacks Chapter 4 2 Introduction Consider a program to model a switching yard –Has main line and siding –Cars may be shunted, removed at any time.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
HeuristicLab. Motivation  less memory pressure no DOM single pass linear process  less developer effort no interfaces to implement  modularity & flexibility.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Chapter 1: A First Program Using C#. Programming Computer program – A set of instructions that tells a computer what to do – Also called software Software.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
Chapter 1: Introducing JAVA. 2 Introduction Why JAVA Applets and Server Side Programming Very rich GUI libraries Portability (machine independence) A.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
Data structures Abstract data types Java classes for Data structures and ADTs.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
AOP-1 Aspect Oriented Programming. AOP-2 Aspects of AOP and Related Tools Limitation of OO Separation of Concerns Aspect Oriented programming AspectJ.
CCC: An Aspect-Oriented Intermediate Language on.Net Platform Yingfei Xiong and Feng Wan University of Electronic Science and Technology of China, China.
1 Proving aspect-oriented programming laws Leonardo Cole Paulo Borba Alexandre Mota Informatics Center Federal University of.
© 2004 Goodrich, Tamassia Hash Tables1  
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Understanding Data Types and Collections Lesson 2.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
1 Linked-list, stack and queue. 2 Outline Abstract Data Type (ADT)‏ Linked list Stack Queue.
CS536 Semantic Analysis Introduction with Emphasis on Name Analysis 1.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Software Engineering Laboratory, Department of Computer Science, Graduate School of Information Science and Technology, Osaka University IWPSE 2003 Program.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Generics Generics vs. heterogeneous collections Doing your own generics FEN 2014UCN Teknologi/act2learn1.
问题 Code scattering Blocks of duplicated code Blocks of complementary code, and different modules implementing complementary parts of the concern Code.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
CSCI-383 Object-Oriented Programming & Design Lecture 25.
1 Principles revisited.NET: Two libraries: System.Collections System.Collections.Generics Data Structures and Collections.
Onlinedeeneislam.blogspot.com1 Design and Analysis of Algorithms Slide # 1 Download From
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Winter 2006CISC121 - Prof. McLeod1 Stuff Solution to midterm is posted. Marking has just started… Lab for this week is not posted (yet?). Final exam (full.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Some of the utilities associated with the development of programs. These program development tools allow users to write and construct programs that the.
Chapter 2 Build Your First Project A Step-by-Step Approach 2 Exploring Microsoft Visual Basic 6.0 Copyright © 1999 Prentice-Hall, Inc. By Carlotta Eaton.
Pyragen A PYTHON WRAPPER GENERATOR TO APPLICATION CORE LIBRARIES Fernando PEREIRA, Christian THEIS - HSE/RP EDMS tech note:
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Binding & Dynamic Linking Presented by: Raunak Sulekh(1013) Pooja Kapoor(1008)
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Introduction toData structures and Algorithms
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Stacks Chapter 4.
Abstract Data Types (ADTs)
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Generic programming in Java
Arrays and Collections
Introduction to Data Structure
SPL – PS1 Introduction to C++.
Presentation transcript:

Zero-overhead composable aspects for.NET Joint work with (i.e. most work done by) Rasmus Johansen and Stephan Spangenberg Peter Sestoft IT University of Copenhagen IFIP WG 2.11 meeting 5, 21 August 2007

The C5 Generic Collection Library for C# and.NET Array lists, linked list, sets, bags, stacks, queues, priority queues etc in coherent design All functionality described by interfaces; “code to interface not implementation” Rich functionality, including –Hash-indexed array lists and linked lists –Slidable updatable sublist views –Snapshots of tree-based sets –Event listeners on all updates lines of C#, lines unit tests See Flashback to August 2006

BUT … implementation is complex Rich functionality costs space for extra fields A LinkedList has many special fields: –c for Count; –e for events; –f for enumeration; –h for caching hash codes; –v for slidable views HashedLinkedList has even more fields... Rich functionality costs time to maintain and test fields e EventBlock events bool isSortedType bool isReadOnlyType IEqualityComparer … h int iUnsequencedHashCode h int iUnsequenceHashCodeStamp c int size f int stamp h Int iSequencedHashCode h Int iSequencedHashCodeStamp Node startsentinel Node endsentinel bool fifo v bool isValid v int offset v List underlying v WeakViewList views v WeakViewList.Node myWeakReference Flashback to August 2006

Example LinkedList code public virtual T Remove() { updatecheck(); if (size == 0) throw new NoSuchItemException("List is empty"); T item = fIFO ? remove(startsentinel.next, 0) : remove(endsentinel.prev, size - 1); (underlying ?? this).raiseForRemove(item); return item; } T remove(Node node, int index) { fixViewsBeforeSingleRemove(node, Offset + index); node.prev.next = node.next; node.next.prev = node.prev; size--; if (underlying != null) underlying.size--; return node.item; } For iteration and hashcode caching For events For sublist views For size For size and sublist views Flashback to August 2006

Wish: A practical specialization tool A means to specify specialized classes, eg: “Make me a linked list class –with hash index –with constant-time Count property, but –no slidable views, –no event listeners, –no support for enumeration, and –no caching of the list's hashcode". A tool that –Checks the consistency of these choices –Generates an implementation that carries no superfluous data and performs no superfluous runtime actions. –Possibly generates specialized (reduced) test cases (McIlroy 1968: Mass produced software components) Flashback to August 2006

Aspect weaving for.NET MSc students Johansen & Spangenberg undertook to look at library specialization They concluded that –Aspect-oriented programming was plausible solution –No existing weaver for.NET was up to the task So they built the Yiihaw aspect weaver –& used it to add events and enumerators as aspects My assessment: –For library specialization, feature-oriented pro- gramming (Hyper/J, AHEAD) seems more plausible –Yiihaw has independent merits –The name is silly: Yiihaw = Yiihaw is an intelligent and high-performing aspect weaver

Notable advantages of Yiihaw Weaving introduces no overhead for casting, wrapping or unwrapping Advice and target type-checked early Weaver type checks (should) ensure consistency of woven assembly Already woven assemblies can be further woven Advice can be applied to advice before applying it to a target assembly … so advice can be composed

What can Yiihaw do Input to Yiihaw –A target assembly (.exe or.dll) to add advice to –An advice assembly (.dll) –A pointcut file (.txt) Output from Yiihaw –A new woven assembly (.exe or.dll): woven = weave(target, advice, pointcuts) Advice kinds: –around : wrap code around method bodies –insert : add member to class, or type to assembly –modify : add base class or interface to class Hence only static advice, no cflow But also no runtime or space overhead

Example aspect weaving class TargetClass { void Method1() { WriteLine("Method1"); } class AspectConstructs { public int count; T CountAspect () { count++; return Proceed (); } insert field public instance int AspectConstructs:count into TargetClass; around * instance * TargetClass:*() do AspectConstructs:CountAspect; class TargetClass { public int count; void Method1() { count++; WriteLine("Method1"); } weave Pointcut file Target Advice Woven output.field public int32 count.method !!T CountAspect () cil managed { // Code size 20 (0x14).maxstack 8 IL_0000: ldarg.0 IL_0001: dup IL_0002: ldfld int32 count IL_0007: ldc.i4.1 IL_0008: add IL_0009: stfld int32 count IL_000e: call !!0 Proceed () IL_0013: ret }.method void Method1() cil managed { // Code size 13 (0xd).maxstack 8 IL_0000: nop IL_0001: ldstr "Method1" IL_0006: call WriteLine(string) IL_000b: nop IL_000c: ret }.field public int32 count.method void Method1() { // Code size 26 (0x1a).maxstack 8 IL_0000: ldarg.0 IL_0001: dup IL_0002: ldfld int32 count IL_0007: ldc.i4.1 IL_0008: add IL_0009: stfld int32 count IL_000e: ldstr "Method1" IL_0013: call WriteLine(string) IL_0018: nop IL_0019: ret }

Commands for weaving The commands: –Compile advice, producing Target.exe –Compile target, producing Advice.dll –Weave advice and target according to pointcut.txt, producing new Target.exe Compiling of advice and target checks types Weaver checks consistency of advice, target and pointcut file May run peverify to check result of weaving csc Target.cs csc /r:..\YIIHAW.API.dll /t:library Advice.cs..\yiihaw pointcut.txt Target.exe Advice.dll

Yiihaw “API” for advice methods R Proceed () executes the target method’s body and returns its result T GetTarget () executes the target method’s body and returns its result other methods to get arguments etc. Compiling the advice against the API gives static, pre-weaving type checking During weaving –Proceed is replaced with target’s body –GetTarget is replaced with ldarg.0 No overhead in the woven code

Strongly typed advice (new) Advice methodSome legal targets void AdviceMethod1() { … Proceed () … } void M() { … } void M(bool x) { … } void M(bool x, int y) { … } void AdviceMethod2(bool x) { … Proceed () … } void M(bool x) { … } void M(bool x, int y) { … } int AdviceMethod3() { … Proceed () … } int M() { … return … } int M(bool x) { … return … } R AdviceMethod4 () { … Proceed () … } void M() { … } int M() { return … } String[] M() { return … } R AdviceMethod5 () where R : IComparable { … Proceed ().CompareTo(…) } int M() { return … } String M() { return … } City M() { return … } when City : IComparable

Typed aspects eliminate need for boxing and unboxing In most weavers, Proceed() returns Object, so result must be tested and unwrapped: int TargetMethod() { int res =... computation...; return res; } int AdviceMethod() { Object res = Proceed(); int i = (int)res;... } And primitive type results must be wrapped: Wrapping/unwrapping gives runtime overhead Yiihaw avoids this entirely; impossible on JVM Test and unwrap Wrapping, boxing will happen

Application?: Microsoft Dynamics AX, Highly customizable enterprise software WhatBy USRUser/department-specific functionalitycust CUSCompany-wide specific functionalitycust VARBusiness solutions, unrestrictedptnr BUSBusiness solutions, certified add-onsptnr LOSMS-made local business solutionsMS DISCritical hotfixes to SYS and GLSMS GLSCountry-specific functionalityMS SYSSystem, core functionalityMS kernel A layer contains application elements, such as table, field, form (e.g. SalesOrder), class, method element, … Microsoft makes lower kernel layers Partners and customers make higher customization layers

Layered customization Higher-level elements shadow lower ones by ID Aspects for customization? Pro: –Avoids distributing source code to partners –More disciplined customization –Easier kernel upgrade, with more machine checking USR CUS VAR BUSshadowed LOS DIS GLS SYSshadowed kernel

Example Invoice class Assume this is at the System layer We now want to give 5% discount on invoices over Euro So weave Invoice at the Business layer public class Invoice { public virtual decimal GrandTotal() { decimal total =... complex computation... ; return total; }

Adding the discount advice The pointcut file: public class MyInvoiceAspect { public decimal DoDiscountAspect() { decimal total = JoinPointContext.Proceed (); return total * (total < ? 1.0M : 0.95M); } around * * decimal Invoice:GrandTotal() do MyInvoiceAspect:DoDiscountAspect; public class Invoice { public virtual decimal GrandTotal() { decimal total =... complex computation... ; return total * (total < ? 1.0M : 0.95M); } The resulting woven class:

Further weaving of class Invoice A charity should get everything for free (and such gifts are deductible) public class MyNewInvoiceAspect { private bool charity; private decimal deductible; public bool Charity { set { charity = value; } } public decimal CharityAspect() { decimal total = JoinPointContext.Proceed (); if (charity) { deductible += total; total = 0; } return total; }

Pointcut file for further weaving Inserts fields charity and deductible Inserts set-property Charity Intercepts method GrandTotal insert field private instance bool MyNewInvoiceAspect:charity into Invoice; insert field private instance decimal MyNewInvoiceAspect:deductible into Invoice; insert method public instance void MyNewInvoiceAspect:set_Charity(bool) into Invoice; around * * decimal Invoice:GrandTotal() do MyNewInvoiceAspect:CharityAspect;

Advice composition (new?) Let a(t) be advice a applied to target t The result is code and can be further woven as in b(a(t)) Advice is normal code; can be woven So advice composes b(a(t))=(b(a))(t) ab If feature=aspect, then features compose But do not commute: b(a(t)) ≠ a(b(t)) ta(t)b(a(t)) b(a) Composition by application

Layered customization Higher-level elements shadow lower ones by ID Aspects for customization? Pro: –Avoids distributing source code to partners –More disciplined customization –Easier kernel upgrade, with more machine checking USR CUS VAR BUSshadowed LOS DIS GLS SYSshadowed kernel

Other aspect weavers for.NET AOP.NET AspectDNG Aspect.NET Aspect# DotSpect EOS Gripper-LOOM.NET NKalore PostSharp LAOS Rapier LOOM Setpoint Sophus Weave.NET Wicca

Future work, maybe Yiihaw is a prototype, so need to –Fix bugs –Revise the ”API” –Support full generics in advice and targets Weave also debugging info (.pdb files) The pointcut language is inexpressive Formalize current weaver type rules Prove: weaving preserves verifiability Current type system too strict –Disallows plausible solution to the expression problem (Zenger & Odersky) –Design more permissive weaver type rules?

Reference Rasmus Johansen & Stephan Spangenberg: YIIHAW. An aspect weaver for.NET. MSc thesis, IT University of Copenhagen, March 2007