last day (14 days later) » 

12:56 PM
0
Q: Performance issue using CSharpCodeProvider in .Net MVC

NileshI had a requirement where user can create and store conditions in database for eg. if(FB > 5000) then 100 else 200 it is working fine user can create the formula but my main main problem is executing that formula so at runtime FB gets replaced with specific value and proper string is ge...

 
Are you caching the generated code or are you recreating it every time?
 
@xanatos it is recreating each and every time it is called only difference is the formula
 
@Nilesh Then you should cache the generated code, so that each formula is "compiled" only once
 
@CodeCaster i'm looking for faster way of compiling code at runtime and using it
@xanatos every time it is new formula with different values so how does the caching will work
 
@Nilesh How many formulas do you have?
 
12:56 PM
@xanatos there is no such limit user can add as many formula he wants
basically user will create master fields and use it in formula
 
@Nilesh With this logic, you need infinite hard disk space, because "user can add ... he wants". How many formulas will you have? 10? 100? 1000?10000? 100000? 1 million?
 
@xanatos if i look into practical scenarios then it will not go above 20-30
 
@Nilesh Ok... You'll have a small number of functions... That FB, why do you want to set it in text? Can't you change your EvalCode() to EvalCode(int value)?
Hi...
You have 20-30 EvalCode functions...
 
hi..
 
Can you put the FB "value" as a parameter?
 
12:57 PM
nope...
 
why not?
 
Firstly user will create Formula using Fields
 
Yep
 
i.e. it willbe something like this
if(FB > 5000) then 100 else 200
then will processing
i have converted that string into proper c# statement
int a; if(6000 > 5000) {a= 100 }else {a= 200}
 
ok
wait
 
12:58 PM
it is also a string
 
where do you take 6000 from?
 
while generating String i have replaced FB with its proper value which comes from db
 
but is it a fixed value for all the rows of the db
 
so to execute that string i have used CSharpCodeProvider
 
or does it change?
 
1:00 PM
it changes depends on users
at a time 700-800 users data gets processed
in for loop
 
the 20-30 functions are user-set or are application-wide?
 
they are user-set
 
so you have up to 800*30 functions?
 
yes approx.
it may vary user to user
but max will be 800*30
 
ok. It gets complex because you can't have 800 assembly in memory
and surely you can't have 800*30 assembly :-)
 
1:06 PM
but it is executed one by one not all at a time
 
What you can do is make a single very big assembly with all the functions together (auto-generating the names of the methods/the namespaces/the name of the containing classes). In this way it becomes much smaller/quicker. It's much quicker to compile 10000 lines of code than it's to compile 1000 times 10 lines of code
 
but the problem here is i have to process one by one. i can't execute them at a once since it is dependent on other value and next process is dependent on this value
 
then find the way to pass these values as parameters. Instead of EvalScript make the signature EvalScript(parameters), so you can compile it without knowing these values
If needed create a big object class AllValues { int Value1; int Value2; int SomeOtherNumber; } that contains all the values that you have calculated, so that you can pass it around and functions can use its values
and only pass around it
 
i'm not getting you
CSharpCodeProvider ??? is this my only option
or there are some other options
 
You could create Expression trees
but the problem is the same
you could probably find some interpreter (like a LUA or a Javascript interpreter to run inside your app)
Mmmh... With Expression Trees you don't have to create a new assembly... But it's quite complex to build them from scratch
 
1:18 PM
thanks. i'll try with Expression trees and see the results in terms of performance
 
ok
 
bye...\
have a nice day
 

last day (14 days later) »