Skip to content

Commit

Permalink
Cache compilation exceptions to speed up looped exception creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruce-dunwiddie committed Aug 25, 2023
1 parent 2780863 commit a99a5f7
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Data_Eval/Data_Eval/Data_Eval.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<SignAssembly>true</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>Data_Eval.snk</AssemblyOriginatorKeyFile>
<Version>2.5.0</Version>
<Version>2.6.0</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
6 changes: 3 additions & 3 deletions Data_Eval/Data_Eval/Data_Eval.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
<package>
<metadata>
<id>Data.Eval</id>
<version>2.5.0</version>
<version>2.6.0</version>
<title>Data.Eval</title>
<authors>Bruce Dunwiddie</authors>
<owners>shriop</owners>
<license type="expression">Apache-2.0</license>
<projectUrl>https://github.com/bruce-dunwiddie/data-eval</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>.Net Library for Evaluating Expressions at Runtime</description>
<releaseNotes>Added basic support for anonymous classes.</releaseNotes>
<copyright>Copyright © 2020</copyright>
<releaseNotes>Cache compilation exceptions to speed up looped exception creation.</releaseNotes>
<copyright>Copyright © 2023</copyright>
<tags>eval expression evaluator compile</tags>
<dependencies>
<dependency id="Microsoft.CodeAnalysis" version="3.6.0" />
Expand Down
30 changes: 25 additions & 5 deletions Data_Eval/Data_Eval/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ private void Init(
if (alreadyCompiled)
{
execution = compiledTypes[classText];

if (execution.Exception != null)
{
throw execution.Exception;
}
}
else
{
Expand All @@ -297,11 +302,24 @@ private void Init(

Compiler compiler = new Compiler();

Type newType = compiler.Compile(
classText,
references,
"EvalAssembly",
"CustomEvaluator");
Type newType;

try
{
newType = compiler.Compile(
classText,
references,
"EvalAssembly",
"CustomEvaluator");
}
catch (CompilationException ex)
{
execution.Exception = ex;

compiledTypes[classText] = execution;

throw;
}

execution.Constructor = new DefaultClassConstructorExpression().GetFunc(
newType);
Expand Down Expand Up @@ -549,6 +567,8 @@ private sealed class Execution
public Func<object> Constructor = null;

public Dictionary<string, ExecutionVariable> Variables = new Dictionary<string, ExecutionVariable>();

public CompilationException Exception = null;
}

private sealed class ExecutionVariable
Expand Down
2 changes: 1 addition & 1 deletion Data_Eval/Data_Eval/Pack.bat
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
nuget pack Data_Eval.nuspec -Symbols -SymbolPackageFormat snupkg
nuget push Data.Eval.2.5.0.nupkg -Source https://api.nuget.org/v3/index.json -apikey %NUGET_KEY%
nuget push Data.Eval.2.6.0.nupkg -Source https://api.nuget.org/v3/index.json -apikey %NUGET_KEY%
pause
22 changes: 22 additions & 0 deletions Data_Eval/Tests/EvaluatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using NUnit.Framework;

using Data.Eval;
using Data.Eval.Compilation;
using System.Diagnostics;

namespace Tests
{
Expand Down Expand Up @@ -214,5 +216,25 @@ public void Evaluator_LinqReference()

Assert.AreEqual(1, evaluator.Eval<int>());
}

[Ignore("Don't want to have an intensive long running test unless needed.")]
[Test]
public void Evaluator_LoopedExceptionTime()
{
Stopwatch stopwatch = Stopwatch.StartNew();

for (int loopCount = 0; loopCount < 200; loopCount++)
{
CompilationException ex = Assert.Throws<CompilationException>(
delegate
{
var blah = Evaluator.Eval("return blah");
});
}

stopwatch.Stop();

Assert.Less(stopwatch.ElapsedMilliseconds, 3000);
}
}
}
6 changes: 3 additions & 3 deletions Data_Eval/Tests/ReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ public void Evaluator_ExecAddCallingAssemblyReference()
[Test]
public void Evaluator_ExecAddUsing()
{
var eval = new Evaluator("message = ExampleClass.HelloWorld");
var eval = new Evaluator("testMessage = ExampleClass.HelloWorld");
eval.AddReference(typeof(TestExternalReference.ExampleClass).Assembly.Location);
eval.AddUsing("TestExternalReference");
eval["message"] = "";
eval["testMessage"] = "";
eval.Exec();
Assert.AreEqual("Hello World", eval["message"]);
Assert.AreEqual("Hello World", eval["testMessage"]);
}

[Test]
Expand Down

0 comments on commit a99a5f7

Please sign in to comment.