-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Remove some overhead from XmlNode.SelectSingleNode #54299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
It's currently implemented by delegating to SelectSingleNodes and returning the first one. While the list is lazily-populated, this still entails creating an `XPathNodeList`, creating a `List<XmlNode>`, storing the enumerated into the list, and then returning the element from the list, which is then thrown away. With just a few lines, we can cut through all of that.
No need for these to be allocated classes. They can just live on the stack where they're created.
This shows up on the hot path of parsing the xpath expression. We can use a span to avoid bounds checking.
NextChar is used by lots of routines to advance to the next character. We can streamline it to avoid the bounds check when indexing into the string.
The typical case is there isn't any whitespace, so inline that fast check. This was showing up as a few percentage of a simple scenario.
|
Tagging subscribers to this area: @buyaa-n, @krwq Issue DetailsUsing the example from https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=net-5.0#System_Xml_XmlNode_SelectSingleNode_System_String_ :
|
src/libraries/System.Private.Xml/src/System/Xml/XPath/Internal/XPathScanner.cs
Show resolved
Hide resolved
buyaa-n
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Using the example from https://docs.microsoft.com/en-us/dotnet/api/system.xml.xmlnode.selectsinglenode?view=net-5.0#System_Xml_XmlNode_SelectSingleNode_System_String_ :