Skip to content

Commit

Permalink
support concurrent use of PineVMParseCache
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Feb 8, 2025
1 parent 44a1803 commit e6b1e1c
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions implement/pine/Pine/PineVM/PineVMParseCache.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Concurrent;
using Pine.Core;

namespace Pine.PineVM;
Expand All @@ -8,20 +8,16 @@ namespace Pine.PineVM;
/// </summary>
public class PineVMParseCache
{
private readonly Dictionary<PineValue, Result<string, Expression>> parseExprCache = [];
private readonly ConcurrentDictionary<PineValue, Result<string, Expression>> parseExprCache = [];

public Result<string, Expression> ParseExpression(PineValue expressionValue)
{
if (parseExprCache.TryGetValue(expressionValue, out var cachedResult))
{
return cachedResult;
}

var result = ExpressionEncoding.ParseExpressionFromValueDefault(expressionValue);

parseExprCache[expressionValue] = result;

return result;
return
parseExprCache
.GetOrAdd(
expressionValue,
valueFactory:
ExpressionEncoding.ParseExpressionFromValueDefault);
}
}

0 comments on commit e6b1e1c

Please sign in to comment.