John Lam 1997 — 2026

Why __arglist is undocumented

Today was an interesting day.

I started off the day with a problem: I needed to pass an arbitrary set of parameters to a constructor. The standard way of doing this in C# is to use the params object[] syntax in the constructor’s argument list. I talked about this in an earlier entry in my weblog.

The problem with this approach is that the caller has to do the heavy lifting. It has to:

  1. Create an array of objects
  2. Assign each of the parameters that you wish to pass to an element in the object array. You need to box valuetypes before stuffing them into the array.
  3. Pass the array of objects to the method

However, I was intrigued by the varargs calling convention that is supported by the CLR. This seemed to be a cleaner way of passing an arbitrary set of parameters since all that you needed to do was push the parameters onto the call stack and invoke the method. The method would then parse the parameters itself using the System.ArgIterator type.

I looked through the docs and couldn’t find any way of implementing varargs methods using C# (or MC++ for that matter). So, I started writing the methods using CIL. I decided to hedge my bets, so I posted a message to the DOTNET list to see whether folks had heard of a way to either call a varargs method from C# or to implement a varargs method using C#. Jason Bock replied with the suggestion to use the undocumented __arglist keyword in C# to call a varargs method.

After a bit of experimentation, I managed to figure out how to use __arglist to both call a varargs method and to implement a varargs method. Here’s a simple code fragment that illustrates its usage:

class Class1 { static int AddABunchOfInts( __arglist ) { int result = 0; System.ArgIterator iter = new System.ArgIterator( __arglist ); int argCount = iter.GetRemainingCount(); for( int i = 0; i < argCount; i++ ) { System.TypedReference typedRef = iter.GetNextArg(); result += (int)TypedReference.ToObject( typedRef ); } return result; } static int Main( string[] args ) { return AddABunchOfInts( __arglist( 2, 3, 4 ) ); } }

If you look at the CIL code that is generated for the Main() method, you will see that it just pushes the three constants onto the stack and calls the AddABunchOfInts() method.

IL_0000: ldc.i4.2 IL_0001: ldc.i4.3 IL_0002: ldc.i4.4 IL_0003: call vararg int32 Client.Class1::AddABunchOfInts()

I was ecstatic. I thought wow, this is the way we should always call methods that expect a variable number of parameters. Who wants to use something crufty like params object[]? Why on earth is __arglist undocumented? Why aren’t varargs methods used in more places in the Framework Class Libraries?

As it turns out, there’s a very good reason why __arglist is undocumented. The performance of the varargs calling convention is horrible.

I did a super-simple benchmark to illustrate the relative difference in performance. Here are the three method signatures for methods that will add three integers together. The relative time taken to call each of these three methods is shown in square brackets:

1. [1] int AddThreeInts( int x, int y, int z ) 2. [68] int AddThreeIntsUsingObjectArray( params object[] args ) 3. [680] int AddThreeIntsUsingArgList( __arglist )

As you can see, the varargs calling convention is TEN TIMES SLOWER than the parameter array method. Then Mattias Sjogren showed me a cool trick on the DOTNET list where you replace:

result += (int)TypedReference.ToObject( typedRef );

with

result += __refvalue( typedRef, int );

This narrowed the gap to a 5x spread between varargs and parameter arrays.

Unless I’m missing something in my benchmarks, the take home message here is clear: do NOT use varargs methods if you care about performance!