Skip to content

test(xtext): cover concurrent lookups in large name lookup nodes and speed up walking them - #1642

Open
joaodinissf wants to merge 4 commits into
masterfrom
test/name-lookup-concurrency
Open

joaodinissf wants to merge 4 commits into
masterfrom
test/name-lookup-concurrency

Conversation

@joaodinissf

@joaodinissf joaodinissf commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

Depends on #1641. This branch is based on #1641's head and targets master, so the first commit shown is #1641's own; please review only the three commits after it. Until #1641 is merged, its diff is part of this one. Each of the three commits can be dropped together with the ones after it, without touching those before it.

1. test(xtext): lookups during inserts into large nodes

ChunkedList is package-private, so QualifiedNameSegmentTreeLookupConcurrencyTest goes through the public lookup API: four reader threads against one inserting thread, started with a latch, with a fixed number of insertions and no sleeps.

A lookup during an insertion is not exact in general: find searches a child and then gets it by its index, and a child inserted in between shifts the children after it. The tests assert what holds nevertheless:

  • testLookupsWhileNodesBecomeLarge: no lookup fails. The root and a node below it grow from 4088 to 24'088 children, crossing the 4096 threshold, while the readers look up the names inserted last, exactly and by pattern. A name which sorts before the insertion point keeps its index and must be found with its own values.
  • testExactLookupsWhileChunksSplit: a lookup read back from its serialized form has full chunks. Every chunk is split once while the readers look up names of the half which moves and which sort before the inserted name; they must be found with their own values.

How well this detects a broken lock, measured by running each test method 20 times against modified copies of ChunkedList (executions which failed):

Modification of ChunkedList nodes become large chunks split traversals
validate always true, in get and binarySearch 20/20 20/20 0/20
validate always true in get only 0/20 1/20 0/20
validate always true in binarySearch only 19/20 20/20 0/20
add does not take the write lock 20/20 20/20 20/20
split runs before the write lock is taken 20/20 20/20 7/20
writes of split reordered inside the write lock 0/20 0/20 0/20
copy does not take the read lock (commit 3) 2/20 3/20 0/20

The reordered writes are expected to pass: readers discard whatever they read during a write and bound every index by the arrays they captured, so the order of writes inside the lock cannot be observed. The traversal test (commit 2) reads through toArray, so it is not meant to detect the optimistic reads. The test added with commit 3, testPatternLookupsWhileLargeNodeIsCleared, catches the copy without the read lock in 10 of 20 executions. On the unmodified code none of the four methods failed in 100 executions. They take about 0.3 s, 0.4 s, 0.1 s and 0.03 s here.

2. perf(xtext): walk large nodes without a search per child

#1641 made reads which walk a large node slower, because every children.get(i) on a ChunkedList searches the chunks and validates a stamp:

  • collectMatches called children.get(idx) three times per child. It now gets the child once.
  • visitChildren, accept and getMappings used the inherited iterator. For a large node they now iterate over a copy made with the existing ChunkedList.toArray, which takes the read lock. ChunkedList itself is unchanged; there is no new lock code.

Behaviour change

Only for nodes with 4096 or more children, and only while another thread inserts into the node being visited:

  • Before, recursive pattern lookups (get(pattern, …) through visitChildren), getMappings and removeMappings threw ConcurrentModificationException in that case. Now they do not: they see the children which the node had at one moment, sorted and complete. A child inserted into that node afterwards is not visited; the nodes below are read when the traversal reaches them.
  • Each such traversal allocates a temporary array with one reference per child of the large node.
  • Small nodes are unchanged: they are iterated directly and a concurrent insertion still fails the iteration. Copying them would cost nothing measurable, but copying an ArrayList during an insertion is not safe, so the copy is limited to ChunkedList.
  • In collectMatches the three reads of one index could return different nodes during an insertion; now the node compared with the upper bound is the one visited.

No caller in this repository relies on the exception. AbstractRecursiveScope catches ConcurrentModificationException to reset its own cache and retry, which also covered one thrown by a lookup; that happens less often now.

testTraversalsWhileLargeNodesGrow pins this: getMappings and a recursive pattern over two growing large nodes must not fail and must find everything that was there before. It fails 20 of 20 times without the change and passed 100 of 100 with it.

3. perf(xtext): read ranges of large nodes for prefix patterns

After commit 2, a prefix pattern still got each child of a large node by its index in collectMatches, one chunk search per child. ChunkedList.copy(target, from) copies a range under the read lock, as toArray does, and collectMatches now reads the children of a large node in batches of 32, doubling up to one chunk (1024), since a lookup often stops after a few children. Small nodes are read as before.

Each batch is a consistent copy of a range. Between two batches an insertion can shift the children, so that one is read twice or skipped, as with the reads by index before. A clear between two batches ends the read, as it ended the loop by index: copy copies nothing from the size on, and rejects only a negative index.

The tests above now also look up names by recursive pattern while the nodes grow; a copy without the read lock fails them (last row of the table). testPatternLookupsWhileLargeNodeIsCleared matches a pattern over all 20'000 children of a large root while it is cleared and refilled twice. With a copy which rejects an index past the end, it fails in 100 of 100 executions.

A lookup can still fail during a clear in two windows which predate this PR and exist on master too: between find searching a child and getting it by its index, and while a root of fewer than 4096 children, an ArrayList, is cleared. The test keeps the root large and clears it twice between long reads, so that these stay rare: it did not fail in 1000 executions with the fix.

Numbers

One large root, single-segment names, Temurin 21, same machine, minimum to maximum of 4 runs; times in ms unless noted.

1'000'000 random names master #1641 commit 2 this PR
insert all 19.7–19.8 s 0.89–0.94 s 0.95–1.04 s 0.88–0.99 s
exact lookup of all 628–711 770–879 796–856 789–846
1296 prefix patterns, matching all 40–47 56–67 49–54 40–46
getMappings 31–37 48–56 27–38 27–38
removeMappings 19–22 44–48 20–23 19–22
heap retained after GC 101 MB 100–101 MB 100–101 MB 100–101 MB
400'000 random names master #1641 commit 2 this PR
insert all 3.21–3.31 s 0.23–0.25 s 0.23–0.29 s 0.23–0.26 s
exact lookup of all 170–182 211–237 199–243 192–237
prefix patterns 17–21 23–28 21–25 17–21
getMappings 8–15 15–20 8–17 12–15
removeMappings 4.5–8.1 9.4–14.9 3.7–9.3 5.6–6.7
Names inserted in sorted order master #1641 commit 2 this PR
1'000'000: insert all 86–173 92–110 80–105 94–121
1'000'000: exact lookup of all 79–101 89–118 90–121 90–130
1'000'000: prefix patterns 63–90 89–146 99–133 63–83
1'000'000: getMappings 7–35 29–51 21–29 11–42
1'000'000: removeMappings 3.3–16 21–39 9.4–12 5.6–15
400'000: getMappings 6.4–11.6 10.3–15.5 8.6–13.6 7.8–9.0
400'000: removeMappings 1.5–3.5 6.9–9.1 2.5–4.8 2.4–4.9

What this recovers and what it does not:

  • Prefix patterns, getMappings and removeMappings are back at master's level on a node with 1'000'000 or 400'000 children; for sorted insertion the walk paths are still somewhat noisy, as the traversal itself is cheap there.
  • Exact lookups are untouched and stay where perf: insert into large name lookup nodes in sublinear time #1641 put them, about 15 to 25 % slower than master on a node with 1'000'000 children.
  • Small nodes are unchanged: 3000 names, and 400'000 names in a tree whose nodes all have at most 500 children, measure the same in all four versions.

Checks

  • All results of everything which visits children (getMappings, exact lookups, prefix and recursive patterns, again after removeMappings and remove, and after more insertions) agree with a model on trees with small and large nodes, including nodes of exactly 4095, 4096 and 4097 children, and are identical, in the same order, on master, perf: insert into large name lookup nodes in sublinear time #1641, commit 2 and this PR.
  • ChunkedList.copy agrees with an ArrayList model for every start index and array length tried, across chunk borders and splits.
  • The serialized form is byte-identical to master's, and each version reads the other's output.

Validation

Full local build on Temurin 21 (clean verify with Checkstyle, PMD, CPD and SpotBugs): success, 496 tests (the 492 of #1641 and the 4 new ones), 2 skipped, no Checkstyle, PMD, CPD or SpotBugs findings. The bundle version stays 17.4.8 as set by #1641; compare-version-with-baselines accepts it.

🤖 Generated with Claude Code

rubenporras and others added 4 commits November 20, 2026 13:29
QualifiedNameSegmentTreeLookup kept the children of a node in a sorted
ArrayList, so inserting a child shifted all following children. In the
global index of a full build the root has about 400'000 children and a
few nodes below it up to 114'000, which made filling the index
quadratic: put took 47 % of the build thread in the first build phase.

A node now keeps its children in a ChunkedList once it has 4096 of
them: chunks of at most 1024 children and the index of the first child
of each chunk. An insertion shifts the children of one chunk and the
start indexes of the following chunks, and a binary search searches
the chunks first and then one chunk. Smaller nodes keep the ArrayList.

The serialized form is unchanged: writeExternal writes the children of
a large node as an ArrayList, and readExternal turns a large list read
back into a ChunkedList for later insertions.

The lookup is not synchronized, and in the second build phase other
threads read it while the build thread inserts. Reading a ChunkedList
during an insertion must not fail where reading an ArrayList in
practice does not, so the chunked list writes under a StampedLock and
reads optimistically, taking the read lock only if a read keeps
overlapping insertions.

Inserting 1'000'000 random single-segment names takes 2.2 to 2.8 s
instead of 23 to 25 s, 400'000 names 0.6 to 1.0 s instead of 2.9 to
3.8 s; exact lookups take the same time as before.

Assisted-by: GitHub Copilot (Claude Opus 5.5)
A node of QualifiedNameSegmentTreeLookup with 4096 or more children
keeps them in a ChunkedList, which other threads read while the build
thread inserts. ChunkedList is package-private, so the test goes through
the lookup, with four reader threads against one inserting thread.

A lookup during an insertion is not exact in general: it searches a
child and then gets it by its index, and a child inserted in between
shifts the children after it. The test asserts what holds nevertheless:

- No lookup fails. The root and a node below it grow from 4088 children
  to 24'088 while the readers look up, exactly and by pattern, the names
  inserted last.
- A name which sorts before all names inserted later keeps its index and
  is found with its own values. Besides the names before the insertion
  point of the first test, a second test reads a lookup back from its
  serialized form, which gives full chunks, and splits every chunk once
  while the readers look up names of the half which moves.

Run 20 times each against copies of ChunkedList with a broken lock, at
least one of the two tests failed every time when optimistic reads were
not validated (in get, in binarySearch or in both), when add did not
take the write lock, and when a chunk was split before taking it. On the
unmodified code neither failed in 100 runs. They take about half a
second each.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Getting a child of a large node by its index searches the chunks of its
ChunkedList and validates a stamp, so reads which walk a large node were
slower than with an ArrayList.

collectMatches gets each child once, not three times. visitChildren,
accept and getMappings iterate over a copy of the children of a large
node, made with ChunkedList.toArray under its read lock. ChunkedList is
unchanged.

For one node with 1'000'000 children in random order getMappings takes
28 to 35 ms and removeMappings 19 to 22 ms, as with an ArrayList (25 to
34 and 21 to 22 ms), where iterating over the ChunkedList took 49 to 56
and 46 to 49 ms. Prefix patterns over such a node take 50 to 58 ms, not
56 to 141 ms; with an ArrayList they took 40 to 50 ms, since one search
per child remains. Exact lookups and small nodes are not affected.

While another thread inserts into a large node, a recursive pattern
lookup, getMappings and removeMappings see the children it had at one
moment and no longer fail with a ConcurrentModificationException; the
new test fails without this. A child inserted into that node afterwards
is not visited. Small nodes are iterated directly as before, where a
concurrent insertion fails the iteration: copying an ArrayList during an
insertion is not safe.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
collectMatches got each child of a large node by its index, one chunk
search per child. ChunkedList.copy copies a range under the read lock,
as toArray does, and collectMatches now reads the children of a large
node in batches of 32, doubling up to one chunk, since a lookup often
stops after a few children. Small nodes are read as before.

Each batch is a consistent copy of a range; between two batches an
insertion can shift the children, as with the reads by index before.
A clear between two batches ends the read, as the loop by index did:
copy copies nothing from the size on.

With 1'000'000 random single-segment names on one node (Temurin 21,
min-max of 4 runs), 1296 prefix patterns take 40-46 ms, against 40-47 ms
on master and 49-54 ms without this commit. Exact lookups, getMappings
and removeMappings are unchanged.

The concurrency tests now also look up names by recursive pattern while
the nodes grow, and match a pattern while a large node is cleared and
refilled; a copy without the read lock, or one which rejects an index
past the end, fails them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants