Dynamic caching level<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Dynamic caching level

Posted on: September 4, 2012

I have read an interesting article that talk about the different level of caching that Dynamic keyword use.

The dynamic lookup is performed at runtime with 3 different ways. First, it checks if the last dynamic methods and arguments type correspond to the last call. If yes, it will reuse the compiled code. This is very fast, as fast as using something not dynamic minus the little extra step of validation.

The second way is to check into the complete list of dynamic code that has been called to find a method signature corresponding. This is also very fast. If it found something, it will update the last called method to be faster next time otherwise, it will goes into the last step.

The last step is so build the method call and to update the list of methods. The pseudo code of the logic is like that:

1if (lastMethod = currentOneInvoked) {
2 Invoke(currentOneInvoked);
3} else if(listMethod.Find(currentOneInvoked)) {
4 Invoke(currentOneInvoked);
5} else {
6 UpdateAndInvoke(currentOneInvoked, arg0, ...);

So, dynamic method is slower but is optimized to be as must fast as possible.