Generating and compiling c# code on the fly and then executing it (very slow, why)? MarshallByRef ?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
general idea : I convert user defined formulas into c# and then execute them (.NET 3.5)<br/>

<pre style="font-family:Consolas; font-size:13; color:black; background:white <span style="color:blue var cp = <span style="color:blue new Microsoft.CSharp.<span style="color:#2b91af CSharpCodeProvider();<br/><span style="color:blue var cpar = <span style="color:blue new System.CodeDom.Compiler.<span style="color:#2b91af CompilerParameters { GenerateInMemory = <span style="color:blue true, GenerateExecutable = <span style="color:blue false };<br/>cpar.ReferencedAssemblies.Add(<span style="color:#a31515 "system.dll");<span style="color:blue <br/>var sbCode = <span style="color:blue new<span style="color:#2b91af StringBuilder();
// add tons of code: 1 static Execute Method with ~ 10000 lines, <br/>// each line has ~ 1..50 dictionary "calls" like result["a"] = val["b"]+val["c"]-val["d"];<br/><span style="color:blue <br/>var cr = cp.CompileAssemblyFromSource(cpar, sbCode.ToString());<br/><span style="color:blue var cde = cr.CompiledAssembly.GetType(<span style="color:#a31515 "myclass");
<span style="color:blue var mt = cde.GetMethod(<span style="color:#a31515 "Execute");
mt.Invoke(<span style="color:blue null, <span style="color:blue new<span style="color:blue object[] { val, result, errors }); class myclass {<br/> // signature of generated method (BTW parameter errors only used if errors pop up, none do)<br/><span style="color:#a31515 public static void Execute(Dictionary<string,decimal> val, Dictionary<string,decimal> result, StringBuilder errors) {
[/code]
I pass val and result dictionaries via parameter to method
Method executes correctly, results are ok <- so everything seems to work<br/>
<br/>
I understand that calling a method the first time with > 10000 lines can take a while, but calling the same method again takes just as long as first call.
I already tired converting dictionaries (val + result) to strings and passing them to the method and converting them in the method to dictionaries, calulating and converting the resulting dictionary into a string and decoding that string back into a dictionary
(by caller (via SplitLines)) doesnt speed up (nor slow down)
I also pasted generated code into a test program (making it part of that test program) to see if I could reproduce the problem:
first call took just as long (~30 s), second call completed within a few ms ~ 0.
restrictions:

.NET 3.5on the fly generate and execute capability should be maintainedC# only<br/>

Questions:

I have to call my "Execute" method hundreds of times with different values, how can I speed it up?the on the fly code generation: does the code get generated into current AppDomain?what happens when I generate myclass multiple times (it works but not sure why anymore)what happens when I dispose cp (e.g. stick it in using(var cp = ...){}), is cr still valid after that and availible for additional calls?
This is an old program that has been in production since years w/o any problems. I just want to speed it up for en masse calculations
<
If you dont dig, you wont find it
<br/>

View the full article
 
Back
Top