Skip to content

Commit

Permalink
Avoid per-XmlSchemaValidator PositionInfo allocation (#54356)
Browse files Browse the repository at this point in the history
It can easily use a static singleton, as the instance is immutable and exists purely to provide a "I don't have line info" / (0,0) value if anyone queries for position.
  • Loading branch information
stephentoub committed Jun 28, 2021
1 parent e928680 commit 91599ff
Showing 1 changed file with 3 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public sealed class XmlSchemaValidator
private object _validationEventSender;
private readonly XmlNameTable _nameTable;
private IXmlLineInfo _positionInfo;
private IXmlLineInfo _dummyPositionInfo;
private static readonly IXmlLineInfo s_dummyPositionInfo = new PositionInfo();

private XmlResolver? _xmlResolver;
private Uri? _sourceUri;
Expand Down Expand Up @@ -198,7 +198,6 @@ public XmlSchemaValidator(XmlNameTable nameTable, XmlSchemaSet schemas, IXmlName
[MemberNotNull(nameof(_validationStack))]
[MemberNotNull(nameof(_attPresence))]
[MemberNotNull(nameof(_positionInfo))]
[MemberNotNull(nameof(_dummyPositionInfo))]
[MemberNotNull(nameof(_validationEventSender))]
[MemberNotNull(nameof(_currentState))]
[MemberNotNull(nameof(_textValue))]
Expand All @@ -219,8 +218,7 @@ private void Init()
_attPresence = new Hashtable();
Push(XmlQualifiedName.Empty);

_dummyPositionInfo = new PositionInfo(); //Dummy position info, will return (0,0) if user does not set the LineInfoProvider property
_positionInfo = _dummyPositionInfo;
_positionInfo = s_dummyPositionInfo; //Dummy position info, will return (0,0) if user does not set the LineInfoProvider property
_validationEventSender = this;
_currentState = ValidatorState.None;
_textValue = new StringBuilder(100);
Expand Down Expand Up @@ -282,7 +280,7 @@ public IXmlLineInfo LineInfoProvider
{
if (value == null)
{ //If value is null, retain the default dummy line info
_positionInfo = _dummyPositionInfo;
_positionInfo = s_dummyPositionInfo;
}
else
{
Expand Down

0 comments on commit 91599ff

Please sign in to comment.