Skip to content

Commit

Permalink
Make FormatNCalcCache thread-local (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
axunonb committed Jul 2, 2024
1 parent 1f4ed09 commit b20d240
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/SmartFormat.Extensions.LogiCalc/LogiCalcFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ namespace SmartFormat.Extensions;
/// </remarks>
public class LogiCalcFormatter : IFormatter
{
// Todo: Make the cache static and thread-safe?
private readonly Dictionary<string, object?> _parameters = new(50);

[ThreadStatic] // creates isolated versions of the cache dictionary in each thread
private static Dictionary<string, (Format Format, NCalc.Domain.LogicalExpression LogExpr)>? _formatNCalcCache;
private static readonly
System.Threading.ThreadLocal<Dictionary<string, (Format Format, NCalc.Domain.LogicalExpression LogExpr)>>
FormatNCalcCache = new(() =>
new Dictionary<string, (Format Format, NCalc.Domain.LogicalExpression LogExpr)>(100));

///<inheritdoc/>
public string Name { get; set; } = "calc";
Expand Down Expand Up @@ -58,21 +59,19 @@ public class LogiCalcFormatter : IFormatter
///<inheritdoc />
public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
_formatNCalcCache ??= new Dictionary<string, (Format, NCalc.Domain.LogicalExpression)>(100);

var format = formattingInfo.Format;
if (format == null) return false;

var fi = (FormattingInfo) formattingInfo;

_parameters.Clear();
using var expressionValue = CreateNCalcExpression(fi, _parameters);

try
{
// Create Format and LogicalExpression if they don't exist for the Placeholder
var key = formattingInfo.Placeholder!.ToString();
if (!_formatNCalcCache.TryGetValue(key, out var cache))
if (!FormatNCalcCache.Value!.TryGetValue(key, out var cache))
{
cache.Format = formattingInfo.FormatDetails.Formatter.Parser.ParseFormat(
$"{{,{formattingInfo.Alignment}:d:{formattingInfo.FormatterOptions}}}");
Expand All @@ -86,8 +85,8 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo)

cache.LogExpr = NCalc.Factories.LogicalExpressionFactory.Create(expressionValue.ToString(),
new NCalc.ExpressionContext(nCalcOptions, CultureInfo.InvariantCulture));
_formatNCalcCache.Add(key, cache);

FormatNCalcCache.Value.Add(key, cache);
}

var nCalcExpression = new NCalc.Expression(cache.LogExpr)
Expand All @@ -110,7 +109,7 @@ public bool TryEvaluateFormat(IFormattingInfo formattingInfo)
{
throw new FormattingException(format, exception, format.StartIndex);
}

return true;
}

Expand Down

0 comments on commit b20d240

Please sign in to comment.