Skip to content

Commit 8c02da5

Browse files
authored
Optimize ReadOnlyTernaryTree for System.Private.Xml (#60076)
* change generator * generate code * Refactoring FindElementProperty, FindAttributeProperty * fix method FindCaseInsensitiveString scope * move enum TernaryTreeByte and fix scope * fix naming in enum TernaryTreeByte * use ROS in FindElementProperty, FindAttributeProperty * genarate code * minor refactoring HtmlRawTextWriterGenerator.ttinclude * generate code Co-authored-by: kronic <kronic@softland.ru>
1 parent 60edc0b commit 8c02da5

File tree

6 files changed

+156
-185
lines changed

6 files changed

+156
-185
lines changed

src/libraries/System.Private.Xml/src/System.Private.Xml.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@
140140
<Compile Include="System\Xml\Core\ConformanceLevel.cs" />
141141
<Compile Include="System\Xml\Core\DtdProcessing.cs" />
142142
<Compile Include="System\Xml\Core\EntityHandling.cs" />
143-
<Compile Include="System\Xml\Core\HtmlTernaryTree.cs" />
144143
<Compile Include="System\Xml\Core\IDtdInfo.cs" />
145144
<Compile Include="System\Xml\Core\IDtdParser.cs" />
146145
<Compile Include="System\Xml\Core\IDtdParserAsync.cs" />

src/libraries/System.Private.Xml/src/System/Xml/Core/HtmlEncodedRawTextWriter.cs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
// Instead, modify HtmlRawTextWriterGenerator.ttinclude
66

77
#nullable disable
8-
using System;
98
using System.IO;
10-
using System.Text;
11-
using System.Xml;
12-
using System.Xml.Schema;
139
using System.Diagnostics;
14-
using MS.Internal.Xml;
1510

1611
namespace System.Xml
1712
{
@@ -27,9 +22,6 @@ internal class HtmlEncodedRawTextWriter : XmlEncodedRawTextWriter
2722
private string _mediaType;
2823
private bool _doNotEscapeUriAttributes;
2924

30-
protected static TernaryTreeReadOnly _elementPropertySearch;
31-
protected static TernaryTreeReadOnly _attributePropertySearch;
32-
3325
private const int StackIncrement = 10;
3426

3527
public HtmlEncodedRawTextWriter(TextWriter writer, XmlWriterSettings settings) : base(writer, settings)
@@ -112,7 +104,7 @@ public override void WriteStartElement(string prefix, string localName, string n
112104

113105
if (_trackTextContent && _inTextContent != false) { ChangeTextContentMark(false); }
114106

115-
_currentElementProperties = (ElementProperties)_elementPropertySearch.FindCaseInsensitiveString(localName);
107+
_currentElementProperties = TernaryTreeReadOnly.FindElementProperty(localName);
116108
base._bufChars[_bufPos++] = (char)'<';
117109
base.RawText(localName);
118110
base._attrEndPos = _bufPos;
@@ -309,7 +301,7 @@ public override void WriteStartAttribute(string prefix, string localName, string
309301

310302
if ((_currentElementProperties & (ElementProperties.BOOL_PARENT | ElementProperties.URI_PARENT | ElementProperties.NAME_PARENT)) != 0)
311303
{
312-
_currentAttributeProperties = (AttributeProperties)_attributePropertySearch.FindCaseInsensitiveString(localName) &
304+
_currentAttributeProperties = TernaryTreeReadOnly.FindAttributeProperty(localName) &
313305
(AttributeProperties)_currentElementProperties;
314306

315307
if ((_currentAttributeProperties & AttributeProperties.BOOLEAN) != 0)
@@ -447,13 +439,6 @@ private void Init(XmlWriterSettings settings)
447439
Debug.Assert((int)ElementProperties.BOOL_PARENT == (int)AttributeProperties.BOOLEAN);
448440
Debug.Assert((int)ElementProperties.NAME_PARENT == (int)AttributeProperties.NAME);
449441

450-
if (_elementPropertySearch == null)
451-
{
452-
// _elementPropertySearch should be init last for the mutli thread safe situation.
453-
_attributePropertySearch = new TernaryTreeReadOnly(HtmlTernaryTree.htmlAttributes);
454-
_elementPropertySearch = new TernaryTreeReadOnly(HtmlTernaryTree.htmlElements);
455-
}
456-
457442
_elementScope = new ByteStack(StackIncrement);
458443
_uriEscapingBuffer = new byte[5];
459444
_currentElementProperties = ElementProperties.DEFAULT;
@@ -660,7 +645,7 @@ private unsafe void WriteUriAttributeText(char* pSrc, char* pSrcEnd)
660645
pDstEnd = pDstBegin + _bufLen;
661646
}
662647

663-
while (pDst < pDstEnd && (XmlCharType.IsAttributeValueChar((char)(ch = *pSrc)) && ch < 0x80))
648+
while (pDst < pDstEnd && XmlCharType.IsAttributeValueChar((char)(ch = *pSrc)) && ch < 0x80)
664649
{
665650
*pDst++ = (char)ch;
666651
pSrc++;
@@ -839,7 +824,7 @@ public override void WriteStartElement(string prefix, string localName, string n
839824
{
840825
Debug.Assert(prefix.Length == 0);
841826

842-
base._currentElementProperties = (ElementProperties)_elementPropertySearch.FindCaseInsensitiveString(localName);
827+
base._currentElementProperties = TernaryTreeReadOnly.FindElementProperty(localName);
843828

844829
if (_endBlockPos == base._bufPos && (base._currentElementProperties & ElementProperties.BLOCK_WS) != 0)
845830
{

src/libraries/System.Private.Xml/src/System/Xml/Core/HtmlRawTextWriterGenerator.ttinclude

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@
66
// Instead, modify HtmlRawTextWriterGenerator.ttinclude
77

88
#nullable disable
9-
using System;
109
using System.IO;
11-
using System.Text;
12-
using System.Xml;
13-
using System.Xml.Schema;
1410
using System.Diagnostics;
15-
using MS.Internal.Xml;
1611

1712
namespace System.Xml
1813
{
@@ -28,9 +23,6 @@ namespace System.Xml
2823
private string _mediaType;
2924
private bool _doNotEscapeUriAttributes;
3025

31-
protected static TernaryTreeReadOnly _elementPropertySearch;
32-
protected static TernaryTreeReadOnly _attributePropertySearch;
33-
3426
private const int StackIncrement = 10;
3527

3628
<# if (WriterType == RawTextWriterType.Encoded) {
@@ -115,7 +107,7 @@ namespace System.Xml
115107

116108
#><#= SetTextContentMark(4, false) #>
117109

118-
_currentElementProperties = (ElementProperties)_elementPropertySearch.FindCaseInsensitiveString(localName);
110+
_currentElementProperties = TernaryTreeReadOnly.FindElementProperty(localName);
119111
base.<#= BufferName #>[_bufPos++] = (<#= BufferType #>)'<';
120112
base.RawText(localName);
121113
base._attrEndPos = _bufPos;
@@ -312,7 +304,7 @@ namespace System.Xml
312304

313305
if ((_currentElementProperties & (ElementProperties.BOOL_PARENT | ElementProperties.URI_PARENT | ElementProperties.NAME_PARENT)) != 0)
314306
{
315-
_currentAttributeProperties = (AttributeProperties)_attributePropertySearch.FindCaseInsensitiveString(localName) &
307+
_currentAttributeProperties = TernaryTreeReadOnly.FindAttributeProperty(localName) &
316308
(AttributeProperties)_currentElementProperties;
317309

318310
if ((_currentAttributeProperties & AttributeProperties.BOOLEAN) != 0)
@@ -450,13 +442,6 @@ namespace System.Xml
450442
Debug.Assert((int)ElementProperties.BOOL_PARENT == (int)AttributeProperties.BOOLEAN);
451443
Debug.Assert((int)ElementProperties.NAME_PARENT == (int)AttributeProperties.NAME);
452444

453-
if (_elementPropertySearch == null)
454-
{
455-
// _elementPropertySearch should be init last for the mutli thread safe situation.
456-
_attributePropertySearch = new TernaryTreeReadOnly(HtmlTernaryTree.htmlAttributes);
457-
_elementPropertySearch = new TernaryTreeReadOnly(HtmlTernaryTree.htmlElements);
458-
}
459-
460445
_elementScope = new ByteStack(StackIncrement);
461446
_uriEscapingBuffer = new byte[5];
462447
_currentElementProperties = ElementProperties.DEFAULT;
@@ -576,9 +561,9 @@ namespace System.Xml
576561
}
577562

578563
<# if (WriterType == RawTextWriterType.Utf8) {#>
579-
while (pDst < pDstEnd && (_xmlCharType.IsAttributeValueChar((char)(ch = *pSrc)) && ch <= 0x7F))
564+
while (pDst < pDstEnd && XmlCharType.IsAttributeValueChar((char)(ch = *pSrc)) && ch <= 0x7F)
580565
<# } else { #>
581-
while (pDst < pDstEnd && _xmlCharType.IsAttributeValueChar((char)(ch = *pSrc)))
566+
while (pDst < pDstEnd && XmlCharType.IsAttributeValueChar((char)(ch = *pSrc)))
582567
<# } #>
583568
{
584569
*pDst++ = (<#= BufferType #>)ch;
@@ -667,7 +652,7 @@ namespace System.Xml
667652
pDstEnd = pDstBegin + _bufLen;
668653
}
669654

670-
while (pDst < pDstEnd && (_xmlCharType.IsAttributeValueChar((char)(ch = *pSrc)) && ch < 0x80))
655+
while (pDst < pDstEnd && XmlCharType.IsAttributeValueChar((char)(ch = *pSrc)) && ch < 0x80)
671656
{
672657
*pDst++ = (<#= BufferType #>)ch;
673658
pSrc++;
@@ -793,7 +778,7 @@ namespace System.Xml
793778
//
794779
// 4). SE SC same as above EE a). check stored blockPro <A></A>
795780
// b). true: indentLevel no change
796-
internal class <#= ClassNameIndent #> : <#= ClassName #>
781+
internal sealed class <#= ClassNameIndent #> : <#= ClassName #>
797782
{
798783
//
799784
// Fields
@@ -848,7 +833,7 @@ namespace System.Xml
848833
{
849834
Debug.Assert(prefix.Length == 0);
850835

851-
base._currentElementProperties = (ElementProperties)_elementPropertySearch.FindCaseInsensitiveString(localName);
836+
base._currentElementProperties = TernaryTreeReadOnly.FindElementProperty(localName);
852837

853838
if (_endBlockPos == base._bufPos && (base._currentElementProperties & ElementProperties.BLOCK_WS) != 0)
854839
{

src/libraries/System.Private.Xml/src/System/Xml/Core/HtmlTernaryTree.cs

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/libraries/System.Private.Xml/src/System/Xml/Core/HtmlUtf8RawTextWriter.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@
55
// Instead, modify HtmlRawTextWriterGenerator.ttinclude
66

77
#nullable disable
8-
using System;
98
using System.IO;
10-
using System.Text;
11-
using System.Xml;
12-
using System.Xml.Schema;
139
using System.Diagnostics;
14-
using MS.Internal.Xml;
1510

1611
namespace System.Xml
1712
{
@@ -27,9 +22,6 @@ internal class HtmlUtf8RawTextWriter : XmlUtf8RawTextWriter
2722
private string _mediaType;
2823
private bool _doNotEscapeUriAttributes;
2924

30-
protected static TernaryTreeReadOnly _elementPropertySearch;
31-
protected static TernaryTreeReadOnly _attributePropertySearch;
32-
3325
private const int StackIncrement = 10;
3426

3527
public HtmlUtf8RawTextWriter(Stream stream, XmlWriterSettings settings) : base(stream, settings)
@@ -103,7 +95,7 @@ public override void WriteStartElement(string prefix, string localName, string n
10395
{
10496
Debug.Assert(prefix.Length == 0);
10597

106-
_currentElementProperties = (ElementProperties)_elementPropertySearch.FindCaseInsensitiveString(localName);
98+
_currentElementProperties = TernaryTreeReadOnly.FindElementProperty(localName);
10799
base._bufBytes[_bufPos++] = (byte)'<';
108100
base.RawText(localName);
109101
base._attrEndPos = _bufPos;
@@ -294,7 +286,7 @@ public override void WriteStartAttribute(string prefix, string localName, string
294286

295287
if ((_currentElementProperties & (ElementProperties.BOOL_PARENT | ElementProperties.URI_PARENT | ElementProperties.NAME_PARENT)) != 0)
296288
{
297-
_currentAttributeProperties = (AttributeProperties)_attributePropertySearch.FindCaseInsensitiveString(localName) &
289+
_currentAttributeProperties = TernaryTreeReadOnly.FindAttributeProperty(localName) &
298290
(AttributeProperties)_currentElementProperties;
299291

300292
if ((_currentAttributeProperties & AttributeProperties.BOOLEAN) != 0)
@@ -424,13 +416,6 @@ private void Init(XmlWriterSettings settings)
424416
Debug.Assert((int)ElementProperties.BOOL_PARENT == (int)AttributeProperties.BOOLEAN);
425417
Debug.Assert((int)ElementProperties.NAME_PARENT == (int)AttributeProperties.NAME);
426418

427-
if (_elementPropertySearch == null)
428-
{
429-
// _elementPropertySearch should be init last for the mutli thread safe situation.
430-
_attributePropertySearch = new TernaryTreeReadOnly(HtmlTernaryTree.htmlAttributes);
431-
_elementPropertySearch = new TernaryTreeReadOnly(HtmlTernaryTree.htmlElements);
432-
}
433-
434419
_elementScope = new ByteStack(StackIncrement);
435420
_uriEscapingBuffer = new byte[5];
436421
_currentElementProperties = ElementProperties.DEFAULT;
@@ -809,7 +794,7 @@ public override void WriteStartElement(string prefix, string localName, string n
809794
{
810795
Debug.Assert(prefix.Length == 0);
811796

812-
base._currentElementProperties = (ElementProperties)_elementPropertySearch.FindCaseInsensitiveString(localName);
797+
base._currentElementProperties = TernaryTreeReadOnly.FindElementProperty(localName);
813798

814799
if (_endBlockPos == base._bufPos && (base._currentElementProperties & ElementProperties.BLOCK_WS) != 0)
815800
{

0 commit comments

Comments
 (0)