-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use more efficient stack for FlattenSpansInReverse
- Loading branch information
1 parent
63a4cfa
commit 4e3fc99
Showing
3 changed files
with
113 additions
and
60 deletions.
There are no files selected for viewing
107 changes: 107 additions & 0 deletions
107
....Language/src/Legacy/LegacySyntaxNodeExtensions.ChildSyntaxListReversedEnumeratorStack.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.AspNetCore.Razor.Language.Syntax; | ||
|
||
namespace Microsoft.AspNetCore.Razor.Language.Legacy; | ||
|
||
internal static partial class LegacySyntaxNodeExtensions | ||
{ | ||
/// <summary> | ||
/// This is similar to <see cref="SyntaxNode.ChildSyntaxListEnumeratorStack"/>. | ||
/// However, instead of enumerating descendant nodes in a top-down, left-to-right | ||
/// fashion, the process is reversed; it operates right-to-left and bottom-up. | ||
/// </summary> | ||
private struct ChildSyntaxListReversedEnumeratorStack : IDisposable | ||
{ | ||
private const int MaxArraySize = 256; | ||
|
||
private static readonly ObjectPool<ChildSyntaxList.Reversed.Enumerator[]> s_stackPool = new(() => new ChildSyntaxList.Reversed.Enumerator[16]); | ||
|
||
private ChildSyntaxList.Reversed.Enumerator[] _stack; | ||
private int _stackPtr; | ||
|
||
public ChildSyntaxListReversedEnumeratorStack(SyntaxNode node) | ||
{ | ||
_stack = s_stackPool.Allocate(); | ||
_stackPtr = -1; | ||
|
||
PushRightmostChildren(node); | ||
} | ||
|
||
private void PushRightmostChildren(SyntaxNode node) | ||
{ | ||
var current = node; | ||
do | ||
{ | ||
var children = current.ChildNodes(); | ||
if (children.Count == 0) | ||
{ | ||
break; | ||
} | ||
|
||
if (++_stackPtr == _stack.Length) | ||
{ | ||
Array.Resize(ref _stack, _stack.Length * 2); | ||
} | ||
|
||
_stack[_stackPtr] = children.Reverse().GetEnumerator(); | ||
|
||
current = children.Last(); | ||
} | ||
while (current is not null); | ||
} | ||
|
||
private bool TryMoveNextAndGetCurrent([NotNullWhen(true)] out SyntaxNode? node) | ||
{ | ||
if (_stackPtr < 0) | ||
{ | ||
node = null; | ||
return false; | ||
} | ||
|
||
ref var enumerator = ref _stack[_stackPtr]; | ||
|
||
if (!enumerator.MoveNext()) | ||
{ | ||
node = null; | ||
return false; | ||
} | ||
|
||
node = enumerator.Current; | ||
return true; | ||
} | ||
|
||
public bool TryGetNextNode([NotNullWhen(true)] out SyntaxNode? node) | ||
{ | ||
while (!TryMoveNextAndGetCurrent(out node)) | ||
{ | ||
_stackPtr--; | ||
|
||
if (_stackPtr < 0) | ||
{ | ||
node = null; | ||
return false; | ||
} | ||
} | ||
|
||
PushRightmostChildren(node); | ||
return true; | ||
} | ||
|
||
public bool IsEmpty | ||
=> _stackPtr < 0; | ||
|
||
public void Dispose() | ||
{ | ||
// Return only reasonably-sized stacks to the pool. | ||
if (_stack.Length < MaxArraySize) | ||
{ | ||
Array.Clear(_stack, 0, _stack.Length); | ||
s_stackPool.Free(_stack); | ||
} | ||
} | ||
} | ||
} |
51 changes: 0 additions & 51 deletions
51
...er/Microsoft.AspNetCore.Razor.Language/src/Legacy/LegacySyntaxNodeExtensions.NodeStack.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters