Francesco's blog

 Friday, November 04, 2005

Have a look at this simple Visual Basic code snippet:

' The version that does NOT cache the value type in a reference variable.
Dim start As Date = Now
For i As Integer = 1 To
1000
  
For j = 1 As Integer To
100000
      GetObject(i, j)
  
Next
Next
Console.WriteLine("Version 1: "
& Now.Subtract(start).ToString)
GC.Collect() : GC.WaitForPendingFinalizers()

' The version that caches the value type in a reference variable.
start = Now
For i = 1 As Integer To
1000
  
' Cache the value type in an Object variable.
  
Dim o As Object
= i
  
For j As Integer = 1  To
100000
      GetObject(o, j)
  
Next
Next
Console.WriteLine("Version 2: " & Now.Subtract(start).ToString)

GetObject is a very simple routine, that takes two objects and therefore causes a box operation if they are value types:

Private Function GetObject(ByVal o As Object, ByVal o2 As Object) As Object
  
Return
o
End Function

As you can read in comments, the second portion caches the boxed version of the i variable in an Object variable, because this value doesn't change inside the innermost loop. You'd expect that this second version would run faster, even if by a little, and in fact this is what happens with Visual Basic .NET 2003. However, if you try this code with VB 2005 you'll be surprised to see that - as counterintuitive as it sounds - the version that caches the boxed value is 30-40% slower!

You need ILDASM to understand what happens behind the scenes. Visual Basic calls the GetObjectValue static method of the RuntimeHelpers type (in the System.Runtime.CompilerServices namespace) before passing an object variable to an object argument, and this extra call explains the overhead just observed. The weird thing is that this extra call is generated by the VB2003 compiler as well, however it doesn't nullify our manual optimization based on the cached variable. I am doing the benchmark with the RTM version, therefore this overhead is real (in other words, it isn't caused by pieces of the CLR compiled in debug mode), therefore I can only conclude that the 2.0 version of the GetObjectValue method is less efficient than the 1.1 version.

This is what the GetObjectValue method does. (Thanks to Adrian Florea, who found this note in Rotor's source code.)

GetObjectValue is intended to allow value classes to be manipulated as Object but have aliasing behavior of a value class. The intent is that you would use this function just before an assignment to a variable of type Object. If the value being assigned is a mutable value class, then a shallow copy is returned (because value classes have copy semantics), but otherwise the object itself is returned.

Note: VB calls this method when they're about to assign to an Object or pass it as a parameter. The goal is to make sure that boxed value types work identical to unboxed value types - ie, they get cloned when you pass them around, and are always passed by value. Of course, reference types are not cloned."

Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):

 
Get RSS/Atom Feed
RSS 2.0 | Atom 1.0
Search in the blog
Archive
<July 2008>
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789
Categories

Powered by: newtelligence dasBlog 1.8.5223.1