oleg-shilo / cs-script

C# scripting platform
http://www.cs-script.net
MIT License
1.57k stars 234 forks source link

Cache compiled scripts as files? #332

Closed tim-rue closed 1 year ago

tim-rue commented 1 year ago

I was wondering if it's possible to save compiled script assemblies as files (dll or something like that), so there is no need to recompile them after the application restarted.

Would there even be a performance benefit from that? In my case there are up to 100 scripts that need to be compiled on every application launch.

Thanks in advance!

oleg-shilo commented 1 year ago

You can use code like that:

var info = new CompileInfo
{
    AssemblyFile = @"E:\temp\asm.dll"
};

Assembly asm = CSScript.Evaluator
                       .CompileCode(@"using System;
                                      public class Script
                                      {
                                          public int Sum(int a, int b)
                                          {
                                              return a+b;
                                          }
                                      }",
                                     info);

or

string asmFile = CSScript.Evaluator
                         .CompileAssemblyFromCode(
                                @"using System;
                                  public class Script
                                  {
                                      public int Sum(int a, int b)
                                      {
                                          return a+b;
                                      }
                                  }",
                                  "MyScript.dll");
tim-rue commented 1 year ago

Works like a charm. Thanks!

oleg-shilo commented 1 year ago

Great, glad it all worked out.

damian-666 commented 1 year ago

Not sure if this is an option in your case, but in the new tool chain with net core, IDE ( visual studio 2022 or maybe all of them) the project folder builds every new file that appears in its folder, (unless explicitly excluded) So i have the scripts go in a "plugins" folder with a project that has a Net7 Core shared DLL target assembly. That way they just get built if the files are present. So i will instantiate them if an object refers to the plugin file by name, but if the script is touched during tuning , the custom IDE with unload and or dispose the prebuilt instance and and then load/ replace with the new one from the modified script. This is for interfaced "IEntityPlugin" extensions so i'm not sure if fits every use and certainly not with the extra scripting features.

There might be some issues with serializing if that's done in Roslyn scripts, and prebuild serializers, but not impossible to get around.. with casting.. But In the end the tool chain can collect and package and build all the files all with the same settings, and optimizations and such, maybe as AOT.. Trimming, it can be an issue because the classes are found via reflection, but you can link to the module and or/ tell it not to trimm anything out from that DLL. if its packaging a release build for android or using trimming and the plugins wont instantiate because they are unreferenced at build time.