I have a hashing method whose operations depend on input to the function. Profiling the program has shown that too much time is spent evaluating this hash method. I want to try changing it into an expression tree, so the inner loop checks can be done once. Hopefully it will be faster, but I'll learn about expression trees either way.
Here is a simplified version of the function (I undid some obvious optimizations for the example, and took out any input validation):
Private Function Checksum(ByVal inputValues As IEnumerable(Of UInt32),
ByVal declarations As IEnumerable(Of String),
ByVal statements As IEnumerable(Of String)) As UInt32
Dim variables = New Dictionary(Of Char, UInt32)
For Each declaration In declarations
'parse declaration (eg. "X=52")'
variables(declaration(0)) = UInt32.Parse(declaration.Substring(2))
Next declaration
For Each value In inputValues
'"I"nput'
variables("I"c) = value
For Each statement In statements
'parse statement (eg. "X=Y+Z")'
Dim varResult = statement(0)
Dim valueLeft = variables(statement(2))
Dim operand = statement(3)
Dim valueRight = variables(statement(4))
'execute statement'
Dim valueResult As UInt32
Select Case operand
Case "+"c : valueResult = valueLeft + valueRight
Case "-"c : valueResult = valueLeft - valueRight
Case "*"c : valueResult = valueLeft * valueRight
Case "&"c : valueResult = valueLeft And valueRight
Case "|"c : valueResult = valueLeft Or valueRight
Case "^"c : valueResult = valueLeft Xor valueRight
End Select
variables(varResult) = valueResult
Next statement
Next value
'"O"utput'
Return variables("O"c)
End Function
I want to create a function which takes the declarations and statements and outputs a specialized expression tree representing a function which takes an IEnumerable of UInt32 and returns a UInt32.
Follow-Up:
I succeeded, and the speed-up was ridiculous (an order of magnitude). The main things I had to learn where:
- Use Expression.Lambda and Expression.Compile to get a delegate you can actually use.
- The Expression.Block factory method has a 'variables' parameter you (essentially) use to declare locals. Similarly, Expression.lambda has 'parameters'.
- If you call Expression.Parameter twice, you're dealing with two different variables (even if their name is the same)! Store the result for later usage. Same for labels, etc.
- The result of a BlockExpression is the last expression in the block.