Skip to content

Commit a8ddbc3

Browse files
perf: use Stack.Peek to reduce Pop and Push churn (#1708)
Not sure if more changes might help here, perhaps calling `docsStack.Push(TraverseDocOnExitStackMarker);` later and adding an `else` clause might help, that or using a goto. Might be easier to measure an improvement once other changes were made. Did wonder if `ValueListBuilder` will help due to faster `Push` and `Pop`. Co-authored-by: Bela VanderVoort <[email protected]>
1 parent 5d45e5d commit a8ddbc3

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Src/CSharpier.Core/DocPrinter/PropagateBreaks.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Runtime.CompilerServices;
12
using CSharpier.Core.DocTypes;
23

34
namespace CSharpier.Core.DocPrinter;
@@ -27,6 +28,7 @@ void BreakParentGroup()
2728
}
2829
}
2930

31+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3032
bool OnEnter(Doc doc)
3133
{
3234
if (doc is ForceFlat)
@@ -87,22 +89,23 @@ void OnExit(Doc doc)
8789
docsStack.Push(document);
8890
while (docsStack.Count > 0)
8991
{
90-
var doc = docsStack.Pop();
92+
var doc = docsStack.Peek();
9193

9294
if (doc == TraverseDocOnExitStackMarker)
9395
{
96+
docsStack.Pop();
9497
OnExit(docsStack.Pop());
9598
continue;
9699
}
97100

98-
docsStack.Push(doc);
99-
docsStack.Push(TraverseDocOnExitStackMarker);
100-
101101
if (!OnEnter(doc))
102102
{
103+
OnExit(docsStack.Pop());
103104
continue;
104105
}
105106

107+
docsStack.Push(TraverseDocOnExitStackMarker);
108+
106109
if (doc is Concat concat)
107110
{
108111
// push onto stack in reverse order so they are processed in the original order

0 commit comments

Comments
 (0)