Run-time compiler speed

Public support forum for peer to peer support with related to the Visual Objects and Vulcan.NET products
Post Reply
mbrown
Posts: 9
Joined: Thu Jul 06, 2017 11:35 am

Run-time compiler speed

Post by mbrown »

I'm converting an old VO (2.5) application to X#. This does a lot of complicated calculations, which involves loading many snippets of source code from a file, and compiling and executing them at run-time. But X# seems much slower than VO, so I have run this test code in VO and X# to compare them, which is typical of the calculations used:

LOCAL calcCode AS STRING
LOCAL cbCalc AS _CODEBLOCK
LOCAL values AS ARRAY
LOCAL count AS DWORD
LOCAL result AS USUAL
calcCode := "(a[1,1] + a[2,1]) * (a[1,2] + a[2,2])"
values := { { 12.34, 56.78 },{ 43.21, 87.65 } }
FOR count := 1 UPTO 1000000
cbCalc := &("{ |a| " + calcCode + " }")
result := Eval(cbCalc, values)
NEXT

In VO this takes 8 seconds to run, in X# (release config) it takes 27 minutes 18 seconds.

Is there anything I can do to speed this up?
User avatar
Chris
Posts: 4562
Joined: Thu Oct 08, 2015 7:48 am
Location: Greece

Run-time compiler speed

Post by Chris »

Hi Michael,

That's because of the macro compiler, due to the way that .Net works, it can't be as fast as the one in VO. We are trying to make the x# macro compiler faster than the one that you currently use (the vulcan one), but there will always be a limit on how fast it can be.

For speeding up execution of your program in x#/.Net, there are some ways, like possibly caching macros, using s smart/fast parser to evaluate the snippets instead of macro compiling them, scripting might be a solution, too. It all depends on what type of source code is included in the text files, how repetitive it is etc. Can you post one typical sample file with such source code to have a look?

Chris
Chris Pyrgas

XSharp Development Team test
chris(at)xsharp.eu
User avatar
robert
Posts: 4225
Joined: Fri Aug 21, 2015 10:57 am
Location: Netherlands

Run-time compiler speed

Post by robert »

Michael,

To add to what Chris said:
The macro compiler in .Net can also do much more. In VO you can only call clipper calling convention (untyped) functions. In .Net you can call all functions, typed and untyped.

Can you test what the speed is if you move the creating of the codeblock outside of the loop ? I bet it is much faster then. it is the compilation of the macros that is the most expensive.

I found that it usually helps to cache the macros. Create wrapper code around the macro compiler and cache the result (Dictionary of macro string and resulting codeblock).

You can also improve the runtime speed of the codeblock by adding type casts in there, so the compiler can produce better code

calcCode := "( (FLOAT) a[1,1] + (FLOAT) a[2,1]) * ( (FLOAT)a[1,2] + (FLOAT)a[2,2])"

Robert
XSharp Development Team
The Netherlands
robert@xsharp.eu
mbrown
Posts: 9
Joined: Thu Jul 06, 2017 11:35 am

Run-time compiler speed

Post by mbrown »

Hi Chris,

Thanks for the prompt reply. Unfortunately the files are not simple text files, but the code is embedded within objects, so cannot be read directly. The code varies from basic arithmetic to array manipulation and multiple function calls. Although my test routine runs 1,000,000 times in the loop, in practice the program only evaluates a few hundred code blocks in each run, and the slow speed is only really noticable when running a batch of calculations, so it's not that serious a problem. Do you know when the new libraries might be available?

Michael.
mbrown
Posts: 9
Joined: Thu Jul 06, 2017 11:35 am

Run-time compiler speed

Post by mbrown »

Hi Robert,

Thanks for the reply. Yes, the compilation takes about 3/4 of the time. During these calculations the code blocks that are evaluated are all different, and the results from them are already cached, but I will look at the possibility of caching the code blocks when running batches of calculations.

Thanks for the tip about type casting, although it is important that we can use the existing files as they are, so there is a limit to what we can do here.

Michael.
User avatar
ArneOrtlinghaus
Posts: 384
Joined: Tue Nov 10, 2015 7:48 am
Location: Italy

Run-time compiler speed

Post by ArneOrtlinghaus »

I have made the same experience: It is necessary to cache the compiled blocks where possible. Rexecution of the compiled blocks has a similar speed as in VO. What is nice on the other hand using X#/Vulcan:
- All functions can be called including the strong typed functions.
- Also many dotnet object methods can be called, as long as no method overload has been used.

Arne
mbrown
Posts: 9
Joined: Thu Jul 06, 2017 11:35 am

Run-time compiler speed

Post by mbrown »

Hi Arne,

Unfortunately the code blocks are never re-executed during a calculations sequence, so caching would not help. Thanks anyway.

Michael.
User avatar
Otto
Posts: 174
Joined: Wed Sep 30, 2015 6:22 pm

Run-time compiler speed

Post by Otto »

Is it an option to use a code generator that takes the txt file and transforms it into a dll?
That way, you only have to compile it once in stead of interprete it on startup or during the sequence.

Generating code, calling the compiler and afterwarts loading the additional dll is quite easy.

It only makes sense if the txt isn't constantly changed.

Regards,
Otto
mbrown
Posts: 9
Joined: Thu Jul 06, 2017 11:35 am

Run-time compiler speed

Post by mbrown »

Hi Otto,

Thanks for the suggestion, but it's not a txt file, it's much more complicated than that. The actual sequence of calculations is built up dynamically each time the program is run according to various parameters and data items, so would need a new dll each time.

Thanks anyway,
Michael.
Post Reply