Home > Articles > Programming > Windows Programming

Reflection: Browsing Code Information, Part 3

Reflection is used in the process of obtaining type information at runtime. In this three-part series, Kevin Burton discusses the concept of reflection, which (among many other things) allows you to browse for information about any particular piece of code.
This third of three articles is excerpted from .NET Common Language Runtime Unleashed (Sams, 2002, ISBN: 0672321246).
This chapter is from the book

Up to this point in this series, the reflection services have been used to read type information at runtime. It is also possible to have your code build code. Dynamically building and running code is used in a number of different places throughout the .NET Framework. Two such examples are the Regex (regular expression) class and the XmlSerializer class. The tools are available for you to generate your own code by using code.

This section first discusses a simple "Hello World!" sample in which a single function can generate code for all of the languages supported. This function builds a code tree and then hands the tree over to the code generation APIs. This section will then delve into a more real-world sample where you gain improvement over the brute force method of multiplying matrixes. Finally, this section will discuss Reflection.Emit and dynamically generating IL code.

Code Document Object Model (CodeDom)

The complete source for this sample is located in CodeCom\GenerateCode. The source that is presented here is shown in Listing 1–2.

Listing 1—CodeProvider Initialization

public static void HelloWorldCode(CodeDomProvider provider, StreamWriter output)
{
  // Generate code
  ICodeGenerator cg = provider.CreateGenerator(output);
  CodeGeneratorOptions op = new CodeGeneratorOptions();

The function takes two generic arguments. The first is a CodeDomProvider, which is the base class for all CodeDomProviders. Currently, only two CodeDomProviders exist: one for VB (VBCodeProvider) and one for C# (CsharpCodeProvider). Eventually, many other languages will be supported. If you pass a VBCodeProvider to this method, then VB code will be generated. If you pass CsharpCodeProvider, then C# code will be generated. The output Stream argument is a generic Stream for the destination of the generated code. You will generate some comments and wrap a namespace around the code. The code listing continues with 2.

Listing 2—Comments and Namespace

// Generate the comments at the beginning of the function
CodeCommentStatement comment = 
   new CodeCommentStatement("Code to generate Hello World");
cg.GenerateCodeFromStatement(comment, output, op);
comment = new CodeCommentStatement("First generate the namespace.");
cg.GenerateCodeFromStatement(comment, output, op);

// The namespace
CodeNamespace codeNamespace = new CodeNamespace("HelloWorld");
// The namespace
CodeNamespaceImport import = new CodeNamespaceImport("System");
codeNamespace.Imports.Add(import);

Some comments are inserted at the beginning of the file, and a namespace is wrapped around the code. Notice that these classes are all generic in nature, making it easy to generate code in many different languages.

Listing 3—Main Entry Point

// The class is named with a unique name
CodeTypeDeclaration mainClass = new CodeTypeDeclaration("HelloWorldMain");

// Add the class to the namespace
codeNamespace.Types.Add(mainClass);

// Set up the Main function
CodeEntryPointMethod main = new CodeEntryPointMethod();

CodeParameterDeclarationExpression param = 
  new CodeParameterDeclarationExpression("System.String []", "args");

comment = new CodeCommentStatement("Output the greeting on the Console");
main.Statements.Add(comment);

This section of code generates an entry point for the generated code. In C#, this should look something like this: "void Main(strings[] args)". The listing continues with a variable declaration that will contain the message to be written. Next, you declare a variable to hold the message in Listing 4.

Listing 4—Declare a Variable

CodeVariableDeclarationStatement variable = 
   new CodeVariableDeclarationStatement(typeof(string), "message",
   new CodePrimitiveExpression("Hello World!"));
main.Statements.Add(variable);

A string variable called message is declared and initialized to "Hello World!". In Listing 5, you finish things up and generate the code.

Listing 5—Call Console.WriteLine to Output a Message to the Console

CodeVariableReferenceExpression[] arg = new CodeVariableReferenceExpression[1];
arg[0] = new CodeVariableReferenceExpression(variable.Name);
CodeMethodReferenceExpression type = new CodeMethodReferenceExpression();
type.MethodName = "Console";
CodeMethodInvokeExpression methodCall =
   new CodeMethodInvokeExpression(type,"WriteLine",arg);
main.Statements.Add(methodCall);

mainClass.Members.Add(main);

cg.GenerateCodeFromNamespace(codeNamespace, output, op);

}

Here, you pass the variable message to Console.WriteLine, effectively writing "Hello World!" to the Console. After this last bit of code is generated, the GenerateCodeFromNamespace method on ICodeGenerator interface is called to generate the code for the tree that you have just constructed.

The code that drives this function is in the Main entry point for the sample. Listing 6 shows a portion of this code.

Listing 6—Generating Code and Executing the Code

static void Main(string[] args)
{
  CSharpCodeProvider csprovider = new CSharpCodeProvider();
  string filename = "HelloWorld";
  Stream s = File.Open(filename + ".cs", FileMode.Create);
  StreamWriter t = new StreamWriter(s);

  GenerateCode.HelloWorldCode(csprovider, t);
  t.Close();
  s.Close();

  GenerateCode.CompileAndExecute(csprovider, filename + ".cs",
   filename + "cs.exe");

  VBCodeProvider vbprovider = new VBCodeProvider();
  s = File.Open(filename + ".vb", FileMode.Create);
  t = new StreamWriter(s);
  GenerateCode.HelloWorldCode(vbprovider, t);

  t.Close();
  s.Close();
  GenerateCode.CompileAndExecute(vbprovider, filename + ".vb",
   filename + "vb.exe");
}

This listing illustrates driving code generation for two different languages. As soon as the interface (CodeProvider) is available for other languages, this function could be extended. The methodology is similar in each section (CSharp and VB). First, a file is created that contains the source to be compiled. Next, the code is generated, and then the code is compiled and executed. Listings 7 and 8 show the code that is generated for this sample.

Listing 7—C# Generated Code

// Code to generate Hello World! and "any" language
// First generate the namespace.
namespace HelloWorld {
  using System;
  
 
  public class HelloWorldMain {
    
    public static void Main() {
      // Output the greeting on the Console
      string message = "Hello World!";
      Console.WriteLine(message);
    }
  }
}

Listing 8 shows the VB code.

Listing 8—VB-Generated Code

'Code to generate Hello World! and "any" language
'First generate the namespace.
Imports System

Namespace HelloWorld
  
  Public Class HelloWorldMain
    
    Public Shared Sub Main()
      'Output the greeting on the Console
      Dim message As String = "Hello World!"
      Console.WriteLine(message)
    End Sub
  End Class
End Namespace

Except for syntax differences, the code looks the same. That is the point.

Compiling Code for Multiple Languages

Just as you can generate code for multiple languages, you can also compile code for all languages for which you have a specific implementation of the CodeProvider interface. Again, a short example will help you realize how to accomplish this. This sample will be illustrated in Listings 9–11. The full source for this sample is in the CodeDom\HelloWorld directory. Listing 9 shows the common code used to compile VB and C# code.

Listing 9—Building a Generic Function to Compile and Run Code

public static void CompileAndRun(CodeDomProvider provider,
   string source, string target)
{
  // Now use the CodeProvider interface
  CompilerParameters param = new CompilerParameters(null, target, true);
  param.GenerateExecutable = true;
  ICodeCompiler cc = provider.CreateCompiler();
  CompilerResults cr = cc.CompileAssemblyFromSource(param, source);
  System.Collections.Specialized.StringCollection output = cr.Output;
  foreach(string s in output)
  {
    Console.WriteLine(s);
  }

  if(cr.Errors.Count != 0)
  {
    CompilerErrorCollection es = cr.Errors;
    foreach(System.CodeDom.Compiler.CompilerError e in es)
    {
      Console.WriteLine(e.ToString());
    }
}

Here you just build up the compiler parameters (in this case, just two). The first compiler parameter is the name of the file (assembly) to which to output the compiled results. The second compiled parameter is a flag indicating that you want a standalone executable as opposed to a library. Based on the passed in CodeDomProvider, you create an ICodeCompiler interface. This interface compiles the code and generates the errors if any exist. If errors are present, then you print them. If no errors are present, then you move on to the code in Listing 10.

Listing 10—Running Generically Compiled Code

  else
  {
    // Set ApplicationBase to the current directory
    AppDomainSetup info = new AppDomainSetup();
    info.ApplicationBase = "file:\\\\" + System.Environment.CurrentDirectory;
    // Create an application domain with null evidence
    AppDomain dom = AppDomain.CreateDomain("HelloWorld", null, info);
    dom.ExecuteAssembly(target);
    // Clean up by unloading the application domain
    AppDomain.Unload(dom); 
  }
}

This is standard boilerplate code that creates an AppDomain and loads and runs the compiled code in that AppDomain. The driver for this routine simply generates the source and creates the CodeDomProvider. This is illustrated in Listing 11.

Listing 11—Driver for Testing Various CodeDom Compilers

static void Main(string[] args)
{
  string target = "HelloWorld.exe";
  string [] source = new string[1];
  source[0] = 
@"using System;

namespace HelloWorld
{
  /// <summary>
  /// Summary description for HelloWorldMain.
  /// </summary>
  class HelloWorldMain
  {
    static void Main(string[] args)
    {
      Console.WriteLine(""Hello World!"");
    }
  }
}";
  CSharpCodeProvider cscp = new CSharpCodeProvider();
  Compiler.CompileAndRun(cscp, source[0], target);

  source[0] = 
@"Imports System
Namespace SimpleHelloWorld
  Public Class SimpleHelloWorld
    'Run the application
    Shared Sub Main()
      Console.WriteLine(""Hello World from VB!"")
    End Sub
  End Class
End Namespace

";
  VBCodeProvider vbscp = new VBCodeProvider();
  Compiler.CompileAndRun(vbscp, source[0], target);
}

This code initializes a string with the source to be compiled, creates a CodeDomProvider, and passes that information to the compiler code shown in Listings 9–11. The result looks like this:

Hello World!
Hello World from VB!

This is a convenient method for dynamically generating and compiling code in a generic fashion.

A specialized compiler is available only in the Microsoft.CSharp namespace that allows the user to compile multiple sources at a time. Instead of a single string containing the source, an array of strings is passed to the interface to be compiled. This is handy, but so far, only a C# version of it is available.

Matrix Multiplication

Now you will generate some code that actually does some work. The Regex class uses dynamically generated code to increase performance, and so does XmlSerializer. In the managed code world, a large body of code is being generated that uses templates to the extreme. The idea is that the compiler is used to help optimize the code. Why can't the same thing be done with managed code?

Eric Gunnerson presented a great example of how dynamically generated code can rival the performance of a custom solution with his polynomial evaluation code. Presented next is a matrix multiplication scheme that shows similar performance gains. The source for the code presented in Listings 12–15 is located in the CodeDom\MatrixMultiplication directory.

Just for reference, Listing 12 shows the brute force method of multiplying two matrixes.

Listing 12—Brute Force Matrix Multiplication

public override double [,] Multiply()
{
  // Initialize the result array
  for(int i = 0; i < A.GetLength(0); i++)
    for(int j = 0; j < B.GetLength(1); j++)
      C[i,j] = 0.0;

  // Loop over the rows of A
  for(int i = 0; i < A.GetLength(0); i++)
    // Loop over the columns of A and rows of B
    for(int j = 0; j < A.GetLength(1); j++)
    {
      // Loop over the columns of B
      for(int k = 0; k < B.GetLength(1); k++)
        C[i,k] += A[i,j] * B[j,k];
    }
  return C;
}

The code for Listing 12 performs reasonably well. It will be used as a baseline for measuring how this multiplication can be improved. On my machine, Table 1 shows some performance numbers for multiplying two square N x N matrixes.

Table 1—Direct Matrix Multiplication

N

Multiplications/Second

10

3,200

20

450

50

30

100

4


These were respectable results. Now see if you can improve things with some dynamically generated code. You would mainly be focusing on unrolling the loops involved with matrix multiplication. You could apply other algorithms such as Strassen's divide and conquer algorithm, but to show the benefits of dynamic code generation, just focus on loop unrolling.

Listing 13 shows how to generate code at runtime to multiply two arrays. This listing builds on the information presented in the previous section on CodeDom.

Listing 13—Matrix Multiplication Using Dynamic Code Generation

private void GenerateCode()
{
  string filename = "mmf";
  Stream s = File.Open(filename + ".cs", FileMode.Create);
  StreamWriter t = new StreamWriter(s);

  // Generate code in C#
  CSharpCodeProvider provider = new CSharpCodeProvider();
  ICodeGenerator cg = provider.CreateGenerator(t);
  CodeGeneratorOptions op = new CodeGeneratorOptions();

  // Generate the comments at the beginning of the function
  CodeCommentStatement comment = 
   new CodeCommentStatement("Matrix Multiplication (Fixed)");
  cg.GenerateCodeFromStatement(comment, t, op);

  // The class is named with a unique name
  string className = "__MatrixMultiplyFixed";
  CodeTypeDeclaration matrixClass = new CodeTypeDeclaration(className);
  // The class implements IPolynomial
  matrixClass.BaseTypes.Add("MatrixMultiplication.IMatrixMultiply");

  // Set up the Multiply function
  CodeMemberMethod multiplyMethod = new CodeMemberMethod();
  multiplyMethod.Name = "Multiply";
  multiplyMethod.Parameters.Add(
   new CodeParameterDeclarationExpression("double [,]", "A"));
  multiplyMethod.Parameters.Add(
   new CodeParameterDeclarationExpression("double [,]", "B"));
  multiplyMethod.Parameters.Add(
   new CodeParameterDeclarationExpression("double [,]", "C"));
  // workaround for bug below...
  multiplyMethod.ReturnType = new CodeTypeReference("public void");
  // BUG: This doesn't generate "public"; it just leaves
  // the attribute off of the member...
  multiplyMethod.Attributes |= MemberAttributes.Public;
  StringBuilder str = new StringBuilder();

  str.Append("{");
  // Loop over the rows of A
  for(int i = 0; i < A.GetLength(0); i++)
  {
    // Loop over the columns of A and rows of B
    for(int j = 0; j < A.GetLength(1); j++)
    {
      // Loop over the columns of B
      for(int k = 0; k < B.GetLength(1); k++)
        C[i,k] += A[i,j] * B[j,k];
    }
    str.Append("{");
    for(int j = 0; j < B.GetLength(1); j++)
    {
      if(j < A.GetLength(0) - 1)
        str.Append(string.Format("{0},\r\n",C[i,j]));
      else
        str.Append(string.Format("{0}",C[i,j]));
    }
    if(i < A.GetLength(0) - 1)
      str.Append("},\r\n");
    else
      str.Append("}");
  }
  str.Append("}");
  CodeSnippetTypeMember variable = new CodeSnippetTypeMember(
   string.Format(
   "static readonly double [,] result = new double[{0},{1}]\r\n{2};",
   A.GetLength(0), B.GetLength(1), str.ToString()));
  matrixClass.Members.Add(variable);

  CodeAssignStatement assignment = new CodeAssignStatement();
  assignment.Left = new CodeVariableReferenceExpression("C");
  assignment.Right = new CodeVariableReferenceExpression("result");
  multiplyMethod.Statements.Add(assignment);

  matrixClass.Members.Add(multiplyMethod);
  cg.GenerateCodeFromType(matrixClass, t, op);

  t.Close();
  s.Close();

  // Now use the CodeProvider interface
  CSharpCodeProvider cscp = new CSharpCodeProvider();
  CompilerParameters param = new CompilerParameters(null, null, true);
  param.GenerateInMemory = true;
  param.IncludeDebugInformation = false;
  param.CompilerOptions = "/optimize+";
  param.ReferencedAssemblies.Add("MatrixMultiplication.exe");
  ICodeCompiler cc = cscp.CreateCompiler();
  CompilerResults cr = cc.CompileAssemblyFromFile(param, filename + ".cs");
  System.Collections.Specialized.StringCollection output = cr.Output;
  foreach(string outputSting in output)
  {
    Console.WriteLine(outputSting);
  }

  if(cr.Errors.Count != 0)
  {
    CompilerErrorCollection es = cr.Errors;
    foreach(System.CodeDom.Compiler.CompilerError e in es)
    {
      Console.WriteLine(e.ToString());
    }
  }
  else
  {
    matrixMultiplyInterface = 
   (IMatrixMultiply)cr.CompiledAssembly.CreateInstance(className);
  }
}

This code is not unlike the simple code that generated the "Hello World" message in Listings 1–6. Now see how it performs. The performance numbers have been compiled in Table 2.

Table 2— Fixed Matrix Multiplication

N

Multiplications/Second

10

10,876,658

20

10,794,473

50

10,976,948

100

10,638,297


Wow! The results seem to be independent of the size of the array. And talk about fast! As you suspected, this is too good to be true. Essentially, the two matrixes were multiplied, and the results were statically recorded. Now whenever a user asks to multiply those two matrixes, you can just return the static result array. The code that is generated looks like that in Listing 14.

Listing 14—Fixed Matrix Multiplication-Generated Code

public class __MatrixMultiplyFixed : MatrixMultiplication.IMatrixMultiply {
  
  static readonly double [,] result = new double[10,10]
{{1.88956889330368,
1.30658585741975,
1.31508360075735,
. . .
1.43246374777113,
1.80918436134982,
1.19113607833798}};
  public void Multiply(double [,] A, double [,] B, double [,] C) {
    C = result;
  }
}

If you have a computationally intense function or expression to evaluate, and you only need to evaluate it once and use the results many times, this might be one method to employ. You can achieve the same result with a global static variable and possibly a flag, but this is just another means to an end.

Listing 15 shows the code to dynamically generate code to multiply two matrixes, unrolling the loop to increase performance.

Listing 15—Matrix Multiplication Unrolling Loops at Runtime

private void GenerateCode()
{
  string filename = "mml";
  Stream s = File.Open(filename + ".cs", FileMode.Create);
  StreamWriter t = new StreamWriter(s);

  // Generate code in C#
  CSharpCodeProvider provider = new CSharpCodeProvider();
  ICodeGenerator cg = provider.CreateGenerator(t);
  CodeGeneratorOptions op = new CodeGeneratorOptions();

  // Generate the comments at the beginning of the function
  CodeCommentStatement comment = new CodeCommentStatement(
   "Matrix Multiplication (Loop)");
  cg.GenerateCodeFromStatement(comment, t, op);

  // The class is named with a unique name
  string className = "__MatrixMultiplyLoop";
  CodeTypeDeclaration matrixClass = new CodeTypeDeclaration(className);
  // The class implements IPolynomial
  matrixClass.BaseTypes.Add("MatrixMultiplication.IMatrixMultiply");

  // Set up the Multiply function
  CodeMemberMethod multiplyHelperMethod = new CodeMemberMethod();
  multiplyHelperMethod.Name = "_Multiply";
  multiplyHelperMethod.Parameters.Add(
   new CodeParameterDeclarationExpression(typeof(double [,]), "A"));
  multiplyHelperMethod.Parameters.Add(
   new CodeParameterDeclarationExpression(typeof(double [,]), "B"));
  multiplyHelperMethod.Parameters.Add(
   new CodeParameterDeclarationExpression(typeof(int), "i"));
  multiplyHelperMethod.Parameters.Add(
   new CodeParameterDeclarationExpression(typeof(int), "j"));
  multiplyHelperMethod.ReturnType = new CodeTypeReference(typeof(double));
  {
    CodeBinaryOperatorExpression plus;
    CodeBinaryOperatorExpression lastplus;
    plus = new CodeBinaryOperatorExpression();
    plus.Operator = CodeBinaryOperatorType.Add;
    CodeMethodReturnStatement returnStatement =
   new CodeMethodReturnStatement(plus);
    CodeBinaryOperatorExpression multiply;
    multiply = new CodeBinaryOperatorExpression();
    multiply.Operator = CodeBinaryOperatorType.Multiply;
    multiply.Right = new CodeSnippetExpression("A[i,0]");
    multiply.Left = new CodeSnippetExpression("B[0,j]");
    plus.Left = multiply;
    lastplus = plus;
    for(int k = 1; k < A.GetLength(1); k++)
    {
      multiply = new CodeBinaryOperatorExpression();
      multiply.Operator = CodeBinaryOperatorType.Multiply;
      multiply.Right = new CodeSnippetExpression(
   string.Format("A[i,{0}]",k));
      multiply.Left = new CodeSnippetExpression(
   string.Format("B[{0},j]",k));
      if(k < A.GetLength(1) - 1)
      {
        plus = new CodeBinaryOperatorExpression();
        plus.Operator = CodeBinaryOperatorType.Add;
        lastplus.Right = plus;
        plus.Left = multiply;
      }
      else
      {
        lastplus.Right = multiply;
      }
      lastplus = plus;
    }
    multiplyHelperMethod.Statements.Add(returnStatement);
  }
  matrixClass.Members.Add(multiplyHelperMethod);

  // Set up the Multiply function
  CodeMemberMethod multiplyMethod = new CodeMemberMethod();
  multiplyMethod.Name = "Multiply";
  multiplyMethod.Parameters.Add(
   new CodeParameterDeclarationExpression(typeof(double [,]), "A"));
  multiplyMethod.Parameters.Add(
   new CodeParameterDeclarationExpression(typeof(double [,]), "B"));
  multiplyMethod.Parameters.Add(
   new CodeParameterDeclarationExpression(typeof(double [,]), "C"));
  // workaround for bug below...
  multiplyMethod.ReturnType = new CodeTypeReference("public void");
  // BUG: This doesn't generate "public"; it just leaves
  // the attribute off of the member...
  multiplyMethod.Attributes |= MemberAttributes.Public;
  // Loop over the rows of A
  for(int i = 0; i < A.GetLength(0); i++)
  {
    // Loop over the columns of B
    for(int j = 0; j < B.GetLength(1); j++)
    {
      CodeAssignStatement assignment = new CodeAssignStatement();
      assignment.Left = new CodeArrayIndexerExpression(
               new CodeVariableReferenceExpression("C"), 
          new CodePrimitiveExpression(i),
               new CodePrimitiveExpression(j));
      assignment.Right = new CodeMethodInvokeExpression(
               null,
               "Multiply",
               new CodeVariableReferenceExpression("A"),
        new CodeVariableReferenceExpression("B"),
        new CodePrimitiveExpression(i),
               new CodePrimitiveExpression(j));
      multiplyMethod.Statements.Add(assignment);
    }
  }

  matrixClass.Members.Add(multiplyMethod);
  cg.GenerateCodeFromType(matrixClass, t, op);

  t.Close();
  s.Close();

  string target = "mml.dll";
  // Now use the CodeProvider interface
  CSharpCodeProvider cscp = new CSharpCodeProvider();
  CompilerParameters param = new CompilerParameters(null, null, true);
  param.GenerateInMemory = true;
  param.IncludeDebugInformation = false;
  param.CompilerOptions = "/optimize+";
  param.ReferencedAssemblies.Add("MatrixMultiplication.exe");
  ICodeCompiler cc = cscp.CreateCompiler();
  CompilerResults cr = cc.CompileAssemblyFromFile(param, filename + ".cs");
  System.Collections.Specialized.StringCollection output = cr.Output;
  foreach(string outputSting in output)
  {
    Console.WriteLine(outputSting);
  }
  if(cr.Errors.Count != 0)
  {
    CompilerErrorCollection es = cr.Errors;
    foreach(System.CodeDom.Compiler.CompilerError e in es)
    {
      Console.WriteLine(e.ToString());
    }
  }
  else
  {
    matrixMultiplyInterface = 
   (IMatrixMultiply)cr.CompiledAssembly.CreateInstance(className);
  }
}

This code still uses CodeDom to build the matrix multiplication code, but the code generation has been fixed so that the matrix multiplication occurs with every matrix. This code truly unrolls the loops when doing a matrix multiplication.

A sample of the code that this code generates is shown in Listing 16.

Listing 16—Matrix Multiplication with Unrolled Loops

// Matrix Multiplication (Loop)
public class __MatrixMultiplyLoop : MatrixMultiplication.IMatrixMultiply {
  
  private System.Double _Multiply(System.Double[,] A, System.Double[,] B,
   int i, int j) {
    return ((B[0,j] * A[i,0]) 
          + ((B[1,j] * A[i,1]) 
          + ((B[2,j] * A[i,2]) 
          + ((B[3,j] * A[i,3]) 
          + ((B[4,j] * A[i,4]) 
          + ((B[5,j] * A[i,5]) 
          + ((B[6,j] * A[i,6]) 
          + ((B[7,j] * A[i,7]) 
          + ((B[8,j] * A[i,8]) 
          + (B[9,j] * A[i,9]))))))))));
  }
  
  public void Multiply(System.Double[,] A, System.Double[,] B,
   System.Double[,] C) {
    C[0, 0] = _Multiply(A, B, 0, 0);
    C[0, 1] = _Multiply(A, B, 0, 1);
    C[0, 2] = _Multiply(A, B, 0, 2);
. . .
    C[9, 7] = _Multiply(A, B, 9, 7);
    C[9, 8] = _Multiply(A, B, 9, 8);
    C[9, 9] = _Multiply(A, B, 9, 9);
  }
}

Table 3 shows the performance of this dynamically generated code.

Table 3—Matrix Multiplication With Unrolled Loops

N

Multiplications/Second

10

8,260

20

1,080

50

67

100

5


Comparing these results with Table 1, you can see that until you get to multiplying two 100 [ts] 100 matrixes, unrolling the loops seems to increase performance about two fold.

NOTE

In putting these samples together, I ran into some obstacles that under normal circumstances would only occur because the code was being generated by code.

For instance, one problem I ran into was that the line lengths became too long as I generated code. The C# compiler limits the line length to 2,046 characters. I would not normally type 2,046 characters on a line, so this case would most likely occur only with code generating code. This is a line-length limit, not a statement length limit. I can break the 2,046 characters into two lines (not two statements) to make the compiler happy again.

Another problem that I ran into was that when generating the last sample that unrolled the loops, I initially put all of the code into one method. When I got to run the 100 [ts] 100 matrix multiplication test, I noticed that the code file that was generated was tens of megabytes in size, and it exhausted my machine's virtual memory to compile it. In addition, even when multiplying smaller arrays, the dynamically generated code ran almost twice as slow as the brute force method.

I wanted to see why the generated code was so big. Multiplying two 100 [ts] 100 matrixes with the loops unrolled would have 10,000 statements each with 100 multiplication terms in it. I had to think of a better way.

My solution was to create a helper function that performed the multiplication for each term. I still had 10,000 statements, but each statement called a function. That function had 100 multiplication terms in it. This reduced the size of the code generated for a 100 [ts] 100 multiplication to about 450Kb and increased performance as well.

The moral of the story is that the CLR is not friendly toward large methods. Even though I had the extra overhead of calling a function, it dramatically increased the performance of the overall function.

Dynamically generating code, although somewhat difficult to work with, is a valid way to increase the performance of certain classes of functions.

Without reflection, it would not be possible to generate and run code as was done in the previous examples. Reflection can be used to examine and to generate type information at runtime.

Directly Generating IL (Reflect.Emit)

The idea behind Reflect.Emit is much the same as with the CodeDom model. With Reflection.Emit, you are just generating code at a much lower level code (IL) than with the CodeDom. A simple Hello World sample is included in Listing 17. The full source is in the Emit directory.

Listing 17—Using Reflection.Emit to Generate a Hello World Program

static Type GenerateCode()
{
  AppDomain currentDomain = AppDomain.CurrentDomain;

  // Create new assembly in the current AppDomain
  AssemblyName assemblyName = new AssemblyName();
  assemblyName.Name = "HelloAssembly";
  // Create AssemblyBuilder
  AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(
   assemblyName, AssemblyBuilderAccess.Run);
  // Create ModuleBuilder
  ModuleBuilder moduleBuilder = 
   assemblyBuilder.DefineDynamicModule("HelloModule");
  // Create TypeBuilder
  TypeBuilder typeBuilder = 
   moduleBuilder.DefineType("HelloClass", TypeAttributes.Public);
  // Create MethodBuilder
  MethodBuilder methodBuilder = typeBuilder.DefineMethod("HelloWorld",
   MethodAttributes.Public, null, null);
  // Create MSIL generator
  ILGenerator msil = methodBuilder.GetILGenerator();
  // Generate code
  msil.EmitWriteLine("Hello World!");
  msil.Emit(OpCodes.Ret);
  // Return created type
  return typeBuilder.CreateType();
}
static void Main(string[] args)
{
  try
  {
    Type t = GenerateCode();
    object o = Activator.CreateInstance(t);
    MethodInfo mi = t.GetMethod("HelloWorld");
    Console.WriteLine(mi.Invoke(o, null));
  }
  catch(Exception e)
  {
    Console.WriteLine(e);
  }
}

The code creates an Assembly called HelloAssembly in the current AppDomain. Notice that a culture, version, or signing information is not assigned to this Assembly that would make the AssemblyName a "full" AssemblyName. An AssemblyBuilder is created with an access permission of Run. The other possibilities are RunAndSave and Save. The AssemblyBuilder is used to create a ModuleBuilder, the ModuleBuilder is used to create a TypeBuilder, and the TypeBuilder is used to create a MethodBuilder. MethodBuilder can create ILGenerator. With the ILGenerator, you can emit IL code. For this sample, you just emit enough code to return "Hello World!" from the "HelloWorld" method. After all of the IL code is emitted, a Type can be created and returned. With this Type, you can do a late-bound call on the "HelloWorld" method and print the string that is returned.

Reflection.Emit obviously has enormous possibilities for dynamically generating code. It is potentially easier to use than CodeDom and has less overhead. Of course, you need to know IL, which might make it harder to use this class. The RegEx and Jscript libraries are built upon Reflection.Emit to boost performance much the same as with the matrix multiplication sample that was illustrated, with the added bonus that using Reflection.Emit has lower overhead.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020