Skip to content

Commit 41c7ff6

Browse files
authored
Merge pull request #9397 from h3xds1nz/remove-boxing-stacks-in-bamlreader
Replace boxing Stacks in BamlReader, decrease allocs, improve performance
2 parents 5cec571 + d680df8 commit 41c7ff6

File tree

1 file changed

+22
-22
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup

1 file changed

+22
-22
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/BamlReader.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public BamlReader(Stream bamlStream)
182182
_properties = new ArrayList();
183183
_haveUnprocessedRecord = false;
184184
_deferableContentBlockDepth = -1;
185-
_nodeStack = new Stack();
185+
_nodeStack = new Stack<BamlNodeInfo>();
186186
_reverseXmlnsTable = new Dictionary<String, List<String>>();
187187
}
188188

@@ -1447,9 +1447,9 @@ private BamlKeyInfo ProcessKeyTree()
14471447
// track of when we have entered a constructor parameter section and when
14481448
// we have written out the first parameter to handle adding commas between
14491449
// constructor parameters.
1450-
Stack readProperty = new Stack();
1451-
Stack readConstructor = new Stack();
1452-
Stack readFirstConstructor = new Stack();
1450+
Stack<bool> readProperty = new();
1451+
Stack<bool> readConstructor = new();
1452+
Stack<bool> readFirstConstructor = new();
14531453
readProperty.Push(false); // Property has not yet been read
14541454
readConstructor.Push(false); // Constructor section has not been read
14551455
readFirstConstructor.Push(false); // First constructor parameter has not been read
@@ -1493,8 +1493,8 @@ private BamlKeyInfo ProcessKeyTree()
14931493

14941494
case BamlRecordType.PropertyComplexStart:
14951495
ReadPropertyComplexStartRecord();
1496-
nodeInfo = (BamlNodeInfo)_nodeStack.Pop();
1497-
if ((bool)readProperty.Pop())
1496+
nodeInfo = _nodeStack.Pop();
1497+
if (readProperty.Pop())
14981498
{
14991499
markupString += ", ";
15001500
}
@@ -1520,12 +1520,12 @@ private BamlKeyInfo ProcessKeyTree()
15201520
// If the text contains '{' or '}' then we have to escape these
15211521
// so that it won't be interpreted as a MarkupExtension
15221522
string escapedString = EscapeString(((BamlTextRecord)_currentBamlRecord).Value);
1523-
if ((bool)readFirstConstructor.Peek())
1523+
if (readFirstConstructor.Peek())
15241524
{
15251525
markupString += ", ";
15261526
}
15271527
markupString += escapedString;
1528-
if ((bool)readConstructor.Peek())
1528+
if (readConstructor.Peek())
15291529
{
15301530
readFirstConstructor.Pop();
15311531
readFirstConstructor.Push(true);
@@ -1534,11 +1534,11 @@ private BamlKeyInfo ProcessKeyTree()
15341534

15351535
case BamlRecordType.ElementStart:
15361536
// Process commas between constructor parameters
1537-
if ((bool)readFirstConstructor.Peek())
1537+
if (readFirstConstructor.Peek())
15381538
{
15391539
markupString += ", ";
15401540
}
1541-
if ((bool)readConstructor.Peek())
1541+
if (readConstructor.Peek())
15421542
{
15431543
readFirstConstructor.Pop();
15441544
readFirstConstructor.Push(true);
@@ -1585,11 +1585,11 @@ private BamlKeyInfo ProcessKeyTree()
15851585

15861586
case BamlRecordType.ConstructorParameterType:
15871587
// Process commas between constructor parameters
1588-
if ((bool)readFirstConstructor.Peek())
1588+
if (readFirstConstructor.Peek())
15891589
{
15901590
markupString += ", ";
15911591
}
1592-
if ((bool)readConstructor.Peek())
1592+
if (readConstructor.Peek())
15931593
{
15941594
readFirstConstructor.Pop();
15951595
readFirstConstructor.Push(true);
@@ -1603,7 +1603,7 @@ private BamlKeyInfo ProcessKeyTree()
16031603
{
16041604
string value = ((BamlPropertyRecord)_currentBamlRecord).Value;
16051605
BamlPropertyInfo propertyInfo = ReadPropertyRecordCore(value);
1606-
if ((bool)readProperty.Pop())
1606+
if (readProperty.Pop())
16071607
{
16081608
markupString += ", ";
16091609
}
@@ -1615,7 +1615,7 @@ private BamlKeyInfo ProcessKeyTree()
16151615
case BamlRecordType.PropertyCustom:
16161616
{
16171617
BamlPropertyInfo propertyInfo = GetPropertyCustomRecordInfo();
1618-
if ((bool)readProperty.Pop())
1618+
if (readProperty.Pop())
16191619
{
16201620
markupString += ", ";
16211621
}
@@ -1628,7 +1628,7 @@ private BamlKeyInfo ProcessKeyTree()
16281628
{
16291629
string value = MapTable.GetStringFromStringId(((BamlPropertyStringReferenceRecord)_currentBamlRecord).StringId);
16301630
BamlPropertyInfo propertyInfo = ReadPropertyRecordCore(value);
1631-
if ((bool)readProperty.Pop())
1631+
if (readProperty.Pop())
16321632
{
16331633
markupString += ", ";
16341634
}
@@ -1642,7 +1642,7 @@ private BamlKeyInfo ProcessKeyTree()
16421642
string value = GetTypeValueString(((BamlPropertyTypeReferenceRecord)_currentBamlRecord).TypeId);
16431643
string attributeName = MapTable.GetAttributeNameFromId(
16441644
((BamlPropertyTypeReferenceRecord)_currentBamlRecord).AttributeId);
1645-
if ((bool)readProperty.Pop())
1645+
if (readProperty.Pop())
16461646
{
16471647
markupString += ", ";
16481648
}
@@ -1656,7 +1656,7 @@ private BamlKeyInfo ProcessKeyTree()
16561656
string value = GetExtensionValueString((BamlPropertyWithExtensionRecord)_currentBamlRecord);
16571657
string attributeName = MapTable.GetAttributeNameFromId(
16581658
((BamlPropertyWithExtensionRecord)_currentBamlRecord).AttributeId);
1659-
if ((bool)readProperty.Pop())
1659+
if (readProperty.Pop())
16601660
{
16611661
markupString += ", ";
16621662
}
@@ -1842,7 +1842,7 @@ private void ReadDocumentEndRecord()
18421842
{
18431843
// Pop information off the node stack to ensure we have matched all the
18441844
// start and end nodes and have nothing left but the start document node.
1845-
BamlNodeInfo nodeInfo = (BamlNodeInfo)_nodeStack.Pop();
1845+
BamlNodeInfo nodeInfo = _nodeStack.Pop();
18461846
if (nodeInfo.RecordType != BamlRecordType.DocumentStart)
18471847
{
18481848
throw new InvalidOperationException(SR.Format(SR.BamlScopeError,
@@ -2025,7 +2025,7 @@ private void ReadElementEndRecord()
20252025
// Pop information off the node stack that tells us what element this
20262026
// is the end of. Check to make sure the record on the stack is for a
20272027
// start element.
2028-
BamlNodeInfo nodeInfo = (BamlNodeInfo)_nodeStack.Pop();
2028+
BamlNodeInfo nodeInfo = _nodeStack.Pop();
20292029
if (nodeInfo.RecordType != BamlRecordType.ElementStart)
20302030
{
20312031
throw new InvalidOperationException(SR.Format(SR.BamlScopeError,
@@ -2112,7 +2112,7 @@ private void ReadPropertyComplexEndRecord()
21122112
// Pop information off the node info stack that tells us what the starting
21132113
// record was for this ending record. Check to make sure it is the
21142114
// correct type. If not, throw an exception.
2115-
BamlNodeInfo nodeInfo = (BamlNodeInfo)_nodeStack.Pop();
2115+
BamlNodeInfo nodeInfo = _nodeStack.Pop();
21162116
BamlRecordType expectedType;
21172117
switch (nodeInfo.RecordType)
21182118
{
@@ -2240,7 +2240,7 @@ private void ReadConstructorEnd()
22402240
// Pop information off the node stack that tells us what element this
22412241
// is the end of. Check to make sure the record on the stack is for a
22422242
// start element.
2243-
BamlNodeInfo nodeInfo = (BamlNodeInfo)_nodeStack.Pop();
2243+
BamlNodeInfo nodeInfo = _nodeStack.Pop();
22442244
if (nodeInfo.RecordType != BamlRecordType.ConstructorParametersStart)
22452245
{
22462246
throw new InvalidOperationException(SR.Format(SR.BamlScopeError,
@@ -2842,7 +2842,7 @@ private BamlMapTable MapTable
28422842
private BamlAttributeUsage _attributeUsage;
28432843

28442844
// Stack of node information about the element tree being built.
2845-
private Stack _nodeStack;
2845+
private readonly Stack<BamlNodeInfo> _nodeStack;
28462846

28472847
// Context information used when reading baml file. This contains the XamlTypeMapper used
28482848
// for resolving binary property information into strings.

0 commit comments

Comments
 (0)