Skip to content

Conversation

@stephentoub
Copy link
Member

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_ :

Method Toolchain Mean Ratio Allocated
SelectSingleNode \main\CoreRun.exe 1,238.4 ns 1.00 2,312 B
SelectSingleNode \pr\CoreRun.exe 1,120.9 ns 0.91 2,080 B
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.
@ghost ghost added the area-System.Xml label Jun 16, 2021
@ghost
Copy link

ghost commented Jun 16, 2021

Tagging subscribers to this area: @buyaa-n, @krwq
See info in area-owners.md if you want to be subscribed.

Issue Details

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_ :

Method Toolchain Mean Ratio Allocated
SelectSingleNode \main\CoreRun.exe 1,238.4 ns 1.00 2,312 B
SelectSingleNode \pr\CoreRun.exe 1,120.9 ns 0.91 2,080 B
Author: stephentoub
Assignees: -
Labels:

area-System.Xml

Milestone: -
Copy link
Contributor

@buyaa-n buyaa-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@stephentoub stephentoub merged commit bbc3366 into dotnet:main Jun 17, 2021
@stephentoub stephentoub deleted the selectsinglenode branch June 17, 2021 15:27
@ghost ghost locked as resolved and limited conversation to collaborators Jul 17, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

3 participants