From 0ed330eeb21353ce024c9ad96698dfa0af499eb8 Mon Sep 17 00:00:00 2001
From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com>
Date: Sun, 8 Nov 2026 16:58:05 +0100
Subject: [PATCH 1/2] Keep the xpath attribute selector's results per scope and
current with the DOM
AngleSharp caches parsed selectors, so one XPathAttrSelector serves every
query and thread of a context. It kept a single scope and result list, so:
- a query after a DOM change returned the old result
- parallel queries on different documents overwrote each other's result
- every element was looked up in a list, quadratic in the number of matches
- Text printed a stray '$': *[xpath>'$//li']
The result is now kept per scope (ConditionalWeakTable), tagged with the
document's MutationVersion and replaced as a whole when the document changed.
Elements are looked up in a HashSet that compares references, because
Node.Equals(INode) compares structure.
---
.../XPathConfigurationTests.cs | 75 +++++++++++++++++++
src/AngleSharp.XPath/XPathAttrSelector.cs | 55 ++++++++++++--
2 files changed, 123 insertions(+), 7 deletions(-)
diff --git a/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs b/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs
index e31b2ee..3a53845 100644
--- a/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs
+++ b/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs
@@ -1,4 +1,6 @@
using NUnit.Framework;
+using AngleSharp.Css.Parser;
+using System.Threading;
using System.Threading.Tasks;
using Shouldly;
@@ -42,4 +44,77 @@ public async Task RunXPathQueryFromDocumentWithSelectSingleSpecialN()
element.TagName.ShouldBe("LI");
element.ClassName.ShouldBe("two");
}
+
+ [Test]
+ public async Task RunXPathQueryAgainAfterAppendingAnElement()
+ {
+ const string source = "
";
+ var config = Configuration.Default.WithXPath();
+ var context = BrowsingContext.New(config);
+ var document = await context.OpenAsync(res => res.Content(source));
+ document.QuerySelectorAll("*[xpath>'//li']").Length.ShouldBe(3);
+ document.QuerySelector("ul").AppendChild(document.CreateElement("li"));
+ document.QuerySelectorAll("*[xpath>'//li']").Length.ShouldBe(4);
+ }
+
+ [Test]
+ public async Task RunXPathQueryAgainAfterChangingAnAttribute()
+ {
+ const string source = "";
+ var config = Configuration.Default.WithXPath();
+ var context = BrowsingContext.New(config);
+ var document = await context.OpenAsync(res => res.Content(source));
+ document.QuerySelectorAll("*[xpath>'//li[@class=\"two\"]']").Length.ShouldBe(1);
+ document.QuerySelector("li").ClassName = "two";
+ document.QuerySelectorAll("*[xpath>'//li[@class=\"two\"]']").Length.ShouldBe(2);
+ }
+
+ [Test]
+ public async Task RunXPathQueryMatchesOnlyTheSelectedOfLookAlikeElements()
+ {
+ const string source = "";
+ var config = Configuration.Default.WithXPath();
+ var context = BrowsingContext.New(config);
+ var document = await context.OpenAsync(res => res.Content(source));
+ var elements = document.QuerySelectorAll("*[xpath>'(//p)[1]']");
+ elements.Length.ShouldBe(1);
+ elements[0].ShouldBeSameAs(document.QuerySelector("p"));
+ }
+
+ [Test]
+ public async Task RunXPathQueryOnTwoDocumentsInParallel()
+ {
+ const int rounds = 200;
+ var config = Configuration.Default.WithXPath();
+ var context = BrowsingContext.New(config);
+ var first = await context.OpenAsync(res => res.Content(""));
+ var second = await context.OpenAsync(res => res.Content(""));
+ using var barrier = new Barrier(2);
+
+ int[] Query(Dom.IDocument document)
+ {
+ var counts = new int[rounds];
+
+ for (var i = 0; i < rounds; i++)
+ {
+ barrier.SignalAndWait();
+ counts[i] = document.QuerySelectorAll("*[xpath>'//li']").Length;
+ }
+
+ return counts;
+ }
+
+ var results = await Task.WhenAll(Task.Run(() => Query(first)), Task.Run(() => Query(second)));
+ results[0].ShouldAllBe(count => count == 3);
+ results[1].ShouldAllBe(count => count == 2);
+ }
+
+ [Test]
+ public void XPathSelectorTextIsTheSelectorAsWritten()
+ {
+ var config = Configuration.Default.WithXPath();
+ var context = BrowsingContext.New(config);
+ var parser = context.GetService();
+ parser.ParseSelector("*[xpath>'//li']").Text.ShouldBe("*[xpath>'//li']");
+ }
}
diff --git a/src/AngleSharp.XPath/XPathAttrSelector.cs b/src/AngleSharp.XPath/XPathAttrSelector.cs
index 9a24002..6de1561 100644
--- a/src/AngleSharp.XPath/XPathAttrSelector.cs
+++ b/src/AngleSharp.XPath/XPathAttrSelector.cs
@@ -3,20 +3,24 @@
using AngleSharp.Dom;
using System;
using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+using System.Threading;
namespace AngleSharp.XPath;
sealed class XPathAttrSelector : ISelector
{
private readonly String _value;
- private IElement _scope;
- private List _result;
+
+ // AngleSharp caches parsed selectors, so one instance serves every query and thread of a
+ // context. Each scope gets its own result, which is replaced as a whole when it is stale.
+ private readonly ConditionalWeakTable _results = new();
public XPathAttrSelector(String value) => _value = value;
public Priority Specificity => Priority.OneClass;
- public String Text => $"[xpath>'${_value}']";
+ public String Text => $"[xpath>'{_value}']";
public void Accept(ISelectorVisitor visitor)
{
@@ -24,12 +28,49 @@ public void Accept(ISelectorVisitor visitor)
public Boolean Match(IElement element, IElement scope)
{
- if (_scope != scope)
+ var holder = _results.GetOrCreateValue(scope);
+ var version = GetMutationVersion(scope);
+ var result = Volatile.Read(ref holder.Result);
+
+ if (result == null || result.Version != version)
+ {
+ result = new Result(version, new HashSet(scope.SelectNodes(_value), ReferenceComparer.Instance));
+ Volatile.Write(ref holder.Result, result);
+ }
+
+ return result.Nodes.Contains(element);
+ }
+
+ // The document's counter advances with every change to its tree, so an equal reading means the
+ // result still holds. Other IDocument implementations have no counter and keep their result.
+ private static Int64 GetMutationVersion(IElement scope) =>
+ scope.Owner is Document document ? document.MutationVersion : 0;
+
+ private sealed class ResultHolder
+ {
+ public Result Result;
+ }
+
+ private sealed class Result
+ {
+ public Result(Int64 version, HashSet nodes)
{
- _scope = scope;
- _result = scope.SelectNodes(_value);
+ Version = version;
+ Nodes = nodes;
}
- return _result.Contains(element);
+ public Int64 Version { get; }
+
+ public HashSet Nodes { get; }
+ }
+
+ // Node.Equals(INode) compares structure, so look-alike nodes must not count as the same one.
+ private sealed class ReferenceComparer : IEqualityComparer
+ {
+ public static readonly ReferenceComparer Instance = new();
+
+ public Boolean Equals(INode x, INode y) => ReferenceEquals(x, y);
+
+ public Int32 GetHashCode(INode obj) => RuntimeHelpers.GetHashCode(obj);
}
}
From 3b1eb11d2762d3f71445221310b70c34541f44c4 Mon Sep 17 00:00:00 2001
From: Michael Thyregod <2386496+michaelthyregod@users.noreply.github.com>
Date: Sun, 8 Nov 2026 18:33:12 +0100
Subject: [PATCH 2/2] Answer one query's elements from the last result, keep
results per scope only when needed
The per-scope table cost a lookup for every element and a table for every new
selector, which made a few cases slower than before. The last result is now
checked first, the table only exists once a second scope shows up, and results
of up to 8 nodes are compared by reference without hashing.
---
.../XPathConfigurationTests.cs | 13 +++
src/AngleSharp.XPath/XPathAttrSelector.cs | 82 +++++++++++++++++--
2 files changed, 87 insertions(+), 8 deletions(-)
diff --git a/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs b/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs
index 3a53845..6080d63 100644
--- a/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs
+++ b/src/AngleSharp.XPath.Tests/XPathConfigurationTests.cs
@@ -1,5 +1,6 @@
using NUnit.Framework;
using AngleSharp.Css.Parser;
+using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Shouldly;
@@ -57,6 +58,18 @@ public async Task RunXPathQueryAgainAfterAppendingAnElement()
document.QuerySelectorAll("*[xpath>'//li']").Length.ShouldBe(4);
}
+ [Test]
+ public async Task RunXPathQueryWithManyMatchesAgainAfterAppendingAnElement()
+ {
+ var source = "" + string.Concat(Enumerable.Repeat("- ", 20)) + "
";
+ var config = Configuration.Default.WithXPath();
+ var context = BrowsingContext.New(config);
+ var document = await context.OpenAsync(res => res.Content(source));
+ document.QuerySelectorAll("*[xpath>'//li']").Length.ShouldBe(20);
+ document.QuerySelector("ul").AppendChild(document.CreateElement("li"));
+ document.QuerySelectorAll("*[xpath>'//li']").Length.ShouldBe(21);
+ }
+
[Test]
public async Task RunXPathQueryAgainAfterChangingAnAttribute()
{
diff --git a/src/AngleSharp.XPath/XPathAttrSelector.cs b/src/AngleSharp.XPath/XPathAttrSelector.cs
index 6de1561..26f02c4 100644
--- a/src/AngleSharp.XPath/XPathAttrSelector.cs
+++ b/src/AngleSharp.XPath/XPathAttrSelector.cs
@@ -13,8 +13,11 @@ sealed class XPathAttrSelector : ISelector
private readonly String _value;
// AngleSharp caches parsed selectors, so one instance serves every query and thread of a
- // context. Each scope gets its own result, which is replaced as a whole when it is stale.
- private readonly ConditionalWeakTable _results = new();
+ // context. The last result answers the elements of one query without a lookup; the table
+ // keeps a result per scope once a second scope shows up, so queries on different scopes
+ // do not keep replacing each other's result.
+ private Result _last;
+ private ConditionalWeakTable _results;
public XPathAttrSelector(String value) => _value = value;
@@ -28,19 +31,49 @@ public void Accept(ISelectorVisitor visitor)
public Boolean Match(IElement element, IElement scope)
{
- var holder = _results.GetOrCreateValue(scope);
var version = GetMutationVersion(scope);
+ var result = Volatile.Read(ref _last);
+
+ if (result == null || result.Scope != scope || result.Version != version)
+ {
+ result = GetResult(scope, version, result);
+ Volatile.Write(ref _last, result);
+ }
+
+ return result.Contains(element);
+ }
+
+ private Result GetResult(IElement scope, Int64 version, Result last)
+ {
+ // A first scope, or the last one after a change: no other scope needs keeping yet
+ if (last == null || (last.Scope == scope && Volatile.Read(ref _results) == null))
+ {
+ return Evaluate(scope, version);
+ }
+
+ var results = Volatile.Read(ref _results);
+
+ if (results == null)
+ {
+ Interlocked.CompareExchange(ref _results, new ConditionalWeakTable(), null);
+ results = Volatile.Read(ref _results);
+ results.GetOrCreateValue(last.Scope).Result = last;
+ }
+
+ var holder = results.GetOrCreateValue(scope);
var result = Volatile.Read(ref holder.Result);
if (result == null || result.Version != version)
{
- result = new Result(version, new HashSet(scope.SelectNodes(_value), ReferenceComparer.Instance));
+ result = Evaluate(scope, version);
Volatile.Write(ref holder.Result, result);
}
- return result.Nodes.Contains(element);
+ return result;
}
+ private Result Evaluate(IElement scope, Int64 version) => new(scope, version, scope.SelectNodes(_value));
+
// The document's counter advances with every change to its tree, so an equal reading means the
// result still holds. Other IDocument implementations have no counter and keep their result.
private static Int64 GetMutationVersion(IElement scope) =>
@@ -53,15 +86,48 @@ private sealed class ResultHolder
private sealed class Result
{
- public Result(Int64 version, HashSet nodes)
+ // Up to this many nodes, comparing references one by one is cheaper than hashing
+ private const Int32 MaxScanned = 8;
+
+ private readonly INode[] _scanned;
+ private readonly HashSet _hashed;
+
+ public Result(IElement scope, Int64 version, List nodes)
{
+ Scope = scope;
Version = version;
- Nodes = nodes;
+
+ if (nodes.Count <= MaxScanned)
+ {
+ _scanned = nodes.ToArray();
+ }
+ else
+ {
+ _hashed = new HashSet(nodes, ReferenceComparer.Instance);
+ }
}
+ public IElement Scope { get; }
+
public Int64 Version { get; }
- public HashSet Nodes { get; }
+ public Boolean Contains(INode node)
+ {
+ if (_hashed != null)
+ {
+ return _hashed.Contains(node);
+ }
+
+ foreach (var scanned in _scanned)
+ {
+ if (ReferenceEquals(scanned, node))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
}
// Node.Equals(INode) compares structure, so look-alike nodes must not count as the same one.