How do I translate this code into an expression tree?
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 = 开发者_JAVA技巧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.
There was a documentation update with VS 2010 RC. I have added some examples on new ET API here: http://msdn.microsoft.com/en-us/library/bb397951(VS.100).aspx It shows how to create local variables and how to execute expression trees. Examples are in both VB and C#.
But honestly, I still don't understand what you are trying to do. Why do your declarations and statements come in as strings? And this OpToExp function is also a mystery to me. How did you manage to get operands as expressions, but the operator (which you call "operand" for some reason) as char? If you provide more info on what you are trying to do and the overall design of your system, I might help you better.
精彩评论