From a24769d71cc0668271452b05f85347b5542d323d Mon Sep 17 00:00:00 2001 From: Ram Fattah Date: Tue, 11 Jul 2023 08:52:06 -0700 Subject: [PATCH] Use LRUCache in ExpressionParser for efficient memory usage --- libraries/AdaptiveExpressions/parser/ExpressionParser.cs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/libraries/AdaptiveExpressions/parser/ExpressionParser.cs b/libraries/AdaptiveExpressions/parser/ExpressionParser.cs index c9113cd065..d7173b30ca 100644 --- a/libraries/AdaptiveExpressions/parser/ExpressionParser.cs +++ b/libraries/AdaptiveExpressions/parser/ExpressionParser.cs @@ -2,7 +2,6 @@ // Licensed under the MIT License. using System; -using System.Collections.Concurrent; using System.Collections.Generic; using System.Data; using System.Globalization; @@ -11,7 +10,6 @@ using Antlr4.Runtime; using Antlr4.Runtime.Misc; using Antlr4.Runtime.Tree; -using Newtonsoft.Json.Linq; namespace AdaptiveExpressions { @@ -20,7 +18,7 @@ namespace AdaptiveExpressions /// public class ExpressionParser : IExpressionParser { - private static ConcurrentDictionary expressionDict = new ConcurrentDictionary(); + private static LRUCache expressionDict = new LRUCache(); /// /// Initializes a new instance of the class. @@ -64,7 +62,7 @@ public Expression Parse(string expression) /// A ParseTree. protected static IParseTree AntlrParse(string expression) { - if (expressionDict.TryGetValue(expression, out var expressionParseTree)) + if (expressionDict.TryGet(expression, out var expressionParseTree)) { return expressionParseTree; } @@ -78,7 +76,7 @@ protected static IParseTree AntlrParse(string expression) parser.AddErrorListener(ParserErrorListener.Instance); parser.BuildParseTree = true; var expressionContext = parser.file()?.expression(); - expressionDict.TryAdd(expression, expressionContext); + expressionDict.Set(expression, expressionContext); return expressionContext; }