Skip to content

Commit 0c64d4d

Browse files
author
Sébastien Geiser
committed
Caching types resolution OK
1 parent 2522778 commit 0c64d4d

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,22 @@ private enum TryBlockEvaluatedState
451451

452452
#region Caching
453453

454+
/// <summary>
455+
/// if set to <c>true</c> use a cache for types that were resolved to resolve faster next time.
456+
/// if set to <c>false</c> the cach of types resolution is not use for this instance of ExpressionEvaluator.
457+
/// Default : false
458+
/// the cache is the static Dictionary TypesResolutionCaching (so it is shared by all instances of ExpressionEvaluator that have CacheTypesResolutions enabled)
459+
/// </summary>
460+
public bool CacheTypesResolutions { get; set; } = false;
461+
462+
/// <summary>
463+
/// A shared cache for types resolution.
464+
/// </summary>
454465
public static IDictionary<string, Type> TypesResolutionCaching { get; set; } = new Dictionary<string, Type>();
455466

467+
/// <summary>
468+
/// Clear all ExpressionEvaluator caches
469+
/// </summary>
456470
public static void ClearAllCaches()
457471
{
458472
TypesResolutionCaching.Clear();
@@ -2980,20 +2994,29 @@ private bool DefaultFunctions(string name, List<string> args, out object result)
29802994
private Type GetTypeByFriendlyName(string typeName, string genericTypes = "", bool throwExceptionIfNotFound = false)
29812995
{
29822996
Type result = null;
2997+
string formatedGenericTypes = string.Empty;
2998+
bool isCached = false;
29832999
try
29843000
{
29853001
typeName = typeName.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", "");
29863002
genericTypes = genericTypes.Replace(" ", "").Replace("\t", "").Replace("\r", "").Replace("\n", "");
29873003

2988-
string formatedGenericTypes = string.Empty;
2989-
2990-
if (!genericTypes.Equals(string.Empty))
3004+
if (CacheTypesResolutions && (TypesResolutionCaching?.ContainsKey(typeName + genericTypes) ?? false))
29913005
{
2992-
Type[] types = GetConcreteTypes(genericTypes);
2993-
formatedGenericTypes = $"`{types.Length}[{ string.Join(", ", types.Select(type => type.FullName))}]";
3006+
result = TypesResolutionCaching[typeName + genericTypes];
3007+
isCached = true;
29943008
}
29953009

2996-
result = Type.GetType(typeName + formatedGenericTypes, false, !OptionCaseSensitiveEvaluationActive);
3010+
if (result == null)
3011+
{
3012+
if (!genericTypes.Equals(string.Empty))
3013+
{
3014+
Type[] types = GetConcreteTypes(genericTypes);
3015+
formatedGenericTypes = $"`{types.Length}[{ string.Join(", ", types.Select(type => type.FullName))}]";
3016+
}
3017+
3018+
result = Type.GetType(typeName + formatedGenericTypes, false, !OptionCaseSensitiveEvaluationActive);
3019+
}
29973020

29983021
if (result == null)
29993022
{
@@ -3035,6 +3058,9 @@ private Type GetTypeByFriendlyName(string typeName, string genericTypes = "", bo
30353058
if (result == null && throwExceptionIfNotFound)
30363059
throw new ExpressionEvaluatorSyntaxErrorException($"Type or class {typeName}{genericTypes} is unknown");
30373060

3061+
if (CacheTypesResolutions && (result != null) && !isCached)
3062+
TypesResolutionCaching[typeName + genericTypes] = result;
3063+
30383064
return result;
30393065
}
30403066

TryWindow/MainWindow.xaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
<Label DockPanel.Dock="Left"
1616
Content="Iterations"
1717
Padding="2,0"/>
18+
<CheckBox x:Name="UseCachesCheckbox"
19+
Content="Use Caches"
20+
DockPanel.Dock="Right"
21+
VerticalAlignment="Center"
22+
Margin="5,0"/>
1823
<TextBox x:Name="IterationsTextBox"
1924
Text="1" />
25+
2026
</DockPanel>
27+
2128
<Button x:Name="CalculateButton" Content="_Execute" IsDefault="True" Click="CalculateButton_Click" />
2229
<Button x:Name="CancelButton"
2330
Content="_Cancel"

TryWindow/MainWindow.xaml.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ private async void CalculateButton_Click(object sender, RoutedEventArgs e)
3838

3939
ExpressionEvaluator evaluator = new ExpressionEvaluator();
4040

41+
if (UseCachesCheckbox.IsChecked ?? false)
42+
evaluator.CacheTypesResolutions = true;
43+
4144
evaluator.Namespaces.Add("System.Windows");
4245
evaluator.Namespaces.Add("System.Diagnostics");
4346

4447
evaluator.EvaluateVariable += Evaluator_EvaluateVariable;
4548

4649
Stopwatch stopWatch = new Stopwatch();
4750

48-
4951
try
5052
{
5153
string script = evaluator.RemoveComments(ScriptTextBox.Text);

0 commit comments

Comments
 (0)