Skip to content

Commit 55acfca

Browse files
committed
wip: caching checkpoint
1 parent 895b5cf commit 55acfca

3 files changed

Lines changed: 26 additions & 18 deletions

File tree

NTDLS.ExpressionParser/ExpressionState.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,9 @@ public void StoreScanStep(ScanStepItem value)
8181
{
8282
Array.Resize(ref _scanStep, (_scanStep.Length + 1) * 2);
8383
}
84-
value.IsValid = true;
8584
_scanStep[_consumedScanStepSlots++] = value;
8685
}
8786

88-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
89-
public void IncrementScanStep()
90-
{
91-
if (_consumedScanStepSlots >= _scanStep.Length) //Resize the cache if needed.
92-
{
93-
Array.Resize(ref _scanStep, (_scanStep.Length + 1) * 2);
94-
}
95-
_scanStep[_consumedScanStepSlots++] = new ScanStepItem() { IsValid = false };
96-
}
97-
9887
#endregion
9988

10089
#region Computed Step Cache Management.

NTDLS.ExpressionParser/SubExpression.cs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,14 @@ private void CollapseRightAndLeft(string operation, int operationBeginIndex, out
283283
var cacheKey = span.Slice(operationIndex - outParsedLength + 1, outParsedLength - 2);
284284
var cachedItem = _parentExpression.State.GetPlaceholderCacheItem(cacheKey);
285285
isUserVariableDerived = cachedItem.IsUserVariableDerived;
286-
_parentExpression.State.IncrementScanStep();
286+
287+
_parentExpression.State.StoreScanStep(new ScanStepItem
288+
{
289+
Value = cachedItem.ComputedValue,
290+
Length = outParsedLength,
291+
IsValid = !isUserVariableDerived
292+
});
293+
287294
return cachedItem.ComputedValue;
288295
}
289296
else
@@ -311,7 +318,8 @@ private void CollapseRightAndLeft(string operation, int operationBeginIndex, out
311318
_parentExpression.State.StoreScanStep(new ScanStepItem
312319
{
313320
Value = result,
314-
Length = outParsedLength
321+
Length = outParsedLength,
322+
IsValid = true
315323
});
316324

317325
return result;
@@ -344,7 +352,14 @@ private void CollapseRightAndLeft(string operation, int operationBeginIndex, out
344352
outParsedLength = i;
345353
var cachedItem = _parentExpression.State.GetPlaceholderCacheItem(span.Slice(1, i - 2));
346354
isUserVariableDerived = cachedItem.IsUserVariableDerived;
347-
_parentExpression.State.IncrementScanStep();
355+
356+
_parentExpression.State.StoreScanStep(new ScanStepItem
357+
{
358+
Value = cachedItem.ComputedValue,
359+
Length = outParsedLength,
360+
IsValid = !isUserVariableDerived
361+
});
362+
348363
return cachedItem.ComputedValue;
349364
}
350365
else
@@ -365,7 +380,8 @@ private void CollapseRightAndLeft(string operation, int operationBeginIndex, out
365380
_parentExpression.State.StoreScanStep(new ScanStepItem
366381
{
367382
Value = result,
368-
Length = outParsedLength
383+
Length = outParsedLength,
384+
IsValid = true
369385
});
370386

371387
return result;

TestHarness/Program.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,12 @@ static void EvalPrint(string exp)
1414

1515
static void Baseline()
1616
{
17+
var expression = new Expression("10 * ((5 + 1000 + ( 10 )) * 60.5) * 10"); //71.8829
18+
1719
var stopwatch = Stopwatch.StartNew();
1820
for (int i = 0; i < 100000; i++)
1921
{
20-
var expression = new Expression("10 * ((5 + 1000 + ( 10 )) * 60.5) * 10"); //71.8829
21-
expression.Evaluate(); //559.4445
22+
expression.Evaluate(); //522.17
2223

2324
}
2425
stopwatch.Stop();
@@ -29,6 +30,8 @@ static void Baseline()
2930

3031
static void Main()
3132
{
33+
Baseline();
34+
/*
3235
var expression = new Expression("10 * ((5 + extra + CustomSum(11,55) + ( 10 + !0 )) * Ceil(SUM(11.6, 12.5, 14.7, 11.11)) + 60.5) * 10");
3336
3437
expression.SetParameter("extra", 1000);
@@ -39,7 +42,7 @@ static void Main()
3942
});
4043
4144
Console.WriteLine(expression.Evaluate()?.ToString() ?? "{NULL}");
42-
45+
*/
4346
//var expression = new Expression("10 * ((5 + 1000 + ( 10 )) * 60.5) * 10");
4447
//Console.WriteLine("---FIRST---");
4548
//Console.WriteLine(expression.Evaluate()); //20

0 commit comments

Comments
 (0)