Send As SMS

Thursday, August 04, 2005

Hashed Aspect Oriented Programming [Articles]

What is Aspect Oriented Programming

Dictionary: AOP = Aspect Oriented Programming
The AOP model allows a developer to implement individual concerns of a system in a loosely coupled fashion and combine them into a whole. This enables one to easily modify the behavior of a system dynamically at runtime and thus better enables a system design to meet growing or changing requirements.
Idea is to force running environment to trigger event or method whenever that method or property is called or accessed. Also it should allow perform some action before, after, instead of target method/property call.

Microsoft provides a class System.ContextBoundObject to support AOP in dotnet. Classes derived from System.ContextBoundObject could use features of AOP, and this is described very well in this article, also author provides sample source code.

Microsoft generates proxy classes and uses mechanism similar with remoting. On each call developer able to determine context and add additional logic to basic functionality.However, this gives relatively low performance – performance test was able to run only about 20,000 of context bound operations per second. This is approximately 100 times slower than virtual method call.
Why they have such performance problem?

  • Environment is going to determine context each time when context bound method is called

What we can do to implement AOP faster?

Idea 1 – Context passed among with message

Passing context information with message, we simplify task of determining what need to be done in particular aspect.
It will be done only once per message, not for each aspect oriented call.

Idea 2 – Compress context by hashing

Actually, we could think about context as a function which answers if some aspect should be taken into attention.
After system analyzed context initially it is defined as set of hashed ids.
For example, for SOHO customers we will have a 75896 in context array, and for customers which are our partner we could define key number 84512. (In real system – Guids)
In each aspect oriented call, instead of looking to customer record and detailed information, we just checked for ID in context which is relatively simple operation.

Idea 3 – Match a overloads to context

Each Context Bounded method has information, in which contexts it should be called, and in which not.
By hashing it with same id, which was used to describe context, we simplify task in each call to compare an ordered lists of two ids:
List of IDs which are describing context (Nc)
List of ID describing possible context overloads for particular methods (No)
Complexity of determining which methods should be called equal to complexity of merging 2 ordered lists, and is O (Nc + No)
For testing Nc was 5, No was 10, and total numbers of aspect dependent places was 1000.

Implementation

I have tested such call structure structure, in terms of Events:
Try
{
    OnBeforeCall();
    If (OnInstead != null)
        OnInstead();
    else
        Call();
    OnAfterCall();
}
Catch (exception)
{
    OnException(exception)
}

I can see such ways for implementation:

  • Reflection
  • Events
  • Run time code generation

I have made tests, and I have that Events are slower 9.85 times than a virtual call (213K op/s), Reflection - 9.55 (220K op/s), and code generation - 4.46 (470K op/s).

However, Code generation need time for first run - it is at least 100 ms, so I think it could be used only in very specific systems, where you need to execute more than 400000 calls with same context during application life.

Summary

Hashed Aspect Oriented Programming could help to add another dimension of flexibility in application which need to be ready for future requirements changes with a reasonable performance and implementation cost.

To be continued...



Applies to all dotnet languages: C#, VB.NET, C++.NET, J#

0 Comments:

Post a Comment

<< Home