Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LRUCache in ExpressionParser for efficient memory usage #6667

Merged
merged 1 commit into from
Jul 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions libraries/AdaptiveExpressions/parser/ExpressionParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -11,7 +10,6 @@
using Antlr4.Runtime;
using Antlr4.Runtime.Misc;
using Antlr4.Runtime.Tree;
using Newtonsoft.Json.Linq;

namespace AdaptiveExpressions
{
Expand All @@ -20,7 +18,7 @@ namespace AdaptiveExpressions
/// </summary>
public class ExpressionParser : IExpressionParser
{
private static ConcurrentDictionary<string, IParseTree> expressionDict = new ConcurrentDictionary<string, IParseTree>();
private static LRUCache<string, IParseTree> expressionDict = new LRUCache<string, IParseTree>();

/// <summary>
/// Initializes a new instance of the <see cref="ExpressionParser"/> class.
Expand Down Expand Up @@ -64,7 +62,7 @@ public Expression Parse(string expression)
/// <returns>A ParseTree.</returns>
protected static IParseTree AntlrParse(string expression)
{
if (expressionDict.TryGetValue(expression, out var expressionParseTree))
if (expressionDict.TryGet(expression, out var expressionParseTree))
{
return expressionParseTree;
}
Expand All @@ -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;
}

Expand Down