Skip to content

Commit 6d66ee6

Browse files
committed
Operation visitor.
1 parent 044f67c commit 6d66ee6

4 files changed

Lines changed: 46 additions & 17 deletions

File tree

NTDLS.ExpressionParser/ExpressionState.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ internal class ExpressionState
88
{
99
public VisitorCache<ScanStepItem> ScanStepCache;
1010
public VisitorCache<ComputedStepItem> ComputedStepCache;
11+
public VisitorCache<OperationStepItem> OperationStepCache;
1112

1213
public string WorkingText { get; set; } = string.Empty;
1314
public readonly StringBuilder Buffer;
@@ -31,6 +32,7 @@ public ExpressionState(Sanitized sanitized, ExpressionOptions options)
3132

3233
ScanStepCache = new(_operationCount * 3);
3334
ComputedStepCache = new(_operationCount * 3);
35+
OperationStepCache = new(_operationCount);
3436

3537
for (int i = 0; i < sanitized.ConsumedPlaceholderCacheSlots; i++)
3638
{
@@ -46,8 +48,9 @@ public ExpressionState(Sanitized sanitized, ExpressionOptions options)
4648
public ExpressionState(ExpressionOptions options, int operationCount, int preAllocation)
4749
{
4850
Buffer = new StringBuilder(preAllocation);
49-
ScanStepCache = new(_operationCount * 3);
50-
ComputedStepCache = new(_operationCount * 3);
51+
ScanStepCache = new(operationCount * 3);
52+
ComputedStepCache = new(operationCount * 3);
53+
OperationStepCache = new(operationCount);
5154
_options = options;
5255
}
5356

@@ -110,6 +113,7 @@ public void HydrateTemplateCache(int expressionHash)
110113
{
111114
ComputedStepCache.CopyTo(entry.State.ComputedStepCache);
112115
ScanStepCache.CopyTo(entry.State.ScanStepCache);
116+
OperationStepCache.CopyTo(entry.State.OperationStepCache);
113117
}
114118
_isTemplateCacheHydrated = true;
115119
}
@@ -123,6 +127,7 @@ public void Reset(Sanitized sanitized)
123127
_nextPlaceholderCacheSlot = sanitized.ConsumedPlaceholderCacheSlots;
124128
ComputedStepCache.Reset();
125129
ScanStepCache.Reset();
130+
OperationStepCache.Reset();
126131
}
127132

128133
public ExpressionState Clone(Sanitized sanitized)
@@ -133,13 +138,12 @@ public ExpressionState Clone(Sanitized sanitized)
133138
_operationCount = _operationCount,
134139
_nextPlaceholderCacheSlot = _nextPlaceholderCacheSlot,
135140
_placeholderCache = new PlaceholderCacheItem[_placeholderCache.Length],
136-
_isTemplateCacheHydrated = _isTemplateCacheHydrated,
137-
ComputedStepCache = new(_operationCount * 3),
138-
ScanStepCache = new(_operationCount * 3),
141+
_isTemplateCacheHydrated = _isTemplateCacheHydrated
139142
};
140143

141144
ComputedStepCache.CopyTo(clone.ComputedStepCache);
142145
ScanStepCache.CopyTo(clone.ScanStepCache);
146+
OperationStepCache.CopyTo(clone.OperationStepCache);
143147

144148
for (int i = 0; i < sanitized.ConsumedPlaceholderCacheSlots; i++)
145149
{

NTDLS.ExpressionParser/SubExpression.cs

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -416,23 +416,42 @@ private int GetFreestandingNotOperation(out string outFoundOperation)
416416
[MethodImpl(MethodImplOptions.AggressiveInlining)]
417417
private int GetIndexOfOperation(ReadOnlySpan<char> validOperations, out string foundOperation)
418418
{
419-
ReadOnlySpan<char> span = Text.AsSpan();
420-
421-
for (int i = 1; i < span.Length; i++)
419+
if (_parentExpression.State.OperationStepCache.TryGet(out var cachedObj))
422420
{
423-
char c = span[i];
424-
for (int j = 0; j < validOperations.Length; j++)
421+
foundOperation = cachedObj.Operation;
422+
return cachedObj.Index;
423+
}
424+
else
425+
{
426+
ReadOnlySpan<char> span = Text.AsSpan();
427+
428+
for (int i = 1; i < span.Length; i++)
425429
{
426-
if (c == validOperations[j])
430+
char c = span[i];
431+
for (int j = 0; j < validOperations.Length; j++)
427432
{
428-
foundOperation = c.ToString();
429-
return i;
433+
if (c == validOperations[j])
434+
{
435+
_parentExpression.State.OperationStepCache.Store(new OperationStepItem()
436+
{
437+
Index = i,
438+
Operation = c.ToString()
439+
}, true);
440+
foundOperation = c.ToString();
441+
return i;
442+
}
430443
}
431444
}
432-
}
433445

434-
foundOperation = string.Empty;
435-
return -1;
446+
foundOperation = string.Empty;
447+
448+
_parentExpression.State.OperationStepCache.Store(new OperationStepItem()
449+
{
450+
Index = -1
451+
}, true);
452+
453+
return -1;
454+
}
436455
}
437456

438457
[MethodImpl(MethodImplOptions.AggressiveInlining)]

NTDLS.ExpressionParser/Types.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ internal struct ComputedStepItem
1212
public int EndPosition;
1313
}
1414

15+
internal struct OperationStepItem
16+
{
17+
public string Operation;
18+
public int Index;
19+
}
20+
1521
internal struct ScanStepItem
1622
{
1723
public double? Value;

TestHarness/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ 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
17+
var expression = new Expression("(2+3)*4"); //71.8829
1818

1919
var stopwatch = Stopwatch.StartNew();
2020
for (int i = 0; i < 100000; i++)

0 commit comments

Comments
 (0)