Repository navigation
test(xtext): cover concurrent lookups in large name lookup nodes and speed up walking them - #1642
Open
joaodinissf wants to merge 4 commits into
Open
joaodinissf wants to merge 4 commits into
joaodinissf wants to merge 4 commits into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 nodesChunkedListis package-private, soQualifiedNameSegmentTreeLookupConcurrencyTestgoes 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:
findsearches 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):ChunkedListvalidatealways true, ingetandbinarySearchvalidatealways true ingetonlyvalidatealways true inbinarySearchonlyadddoes not take the write locksplitruns before the write lock is takensplitreordered inside the write lockcopydoes not take the read lock (commit 3)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 thecopywithout 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 aChunkedListsearches the chunks and validates a stamp:collectMatchescalledchildren.get(idx)three times per child. It now gets the child once.visitChildren,acceptandgetMappingsused the inherited iterator. For a large node they now iterate over a copy made with the existingChunkedList.toArray, which takes the read lock.ChunkedListitself 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:
get(pattern, …)throughvisitChildren),getMappingsandremoveMappingsthrewConcurrentModificationExceptionin 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.ArrayListduring an insertion is not safe, so the copy is limited toChunkedList.collectMatchesthe 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.
AbstractRecursiveScopecatchesConcurrentModificationExceptionto reset its own cache and retry, which also covered one thrown by a lookup; that happens less often now.testTraversalsWhileLargeNodesGrowpins this:getMappingsand 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 patternsAfter 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, astoArraydoes, andcollectMatchesnow 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
clearbetween two batches ends the read, as it ended the loop by index:copycopies 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
copywithout the read lock fails them (last row of the table).testPatternLookupsWhileLargeNodeIsClearedmatches a pattern over all 20'000 children of a large root while it is cleared and refilled twice. With acopywhich rejects an index past the end, it fails in 100 of 100 executions.A lookup can still fail during a
clearin two windows which predate this PR and exist on master too: betweenfindsearching a child and getting it by its index, and while a root of fewer than 4096 children, anArrayList, 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.
getMappingsremoveMappingsgetMappingsremoveMappingsgetMappingsremoveMappingsgetMappingsremoveMappingsWhat this recovers and what it does not:
getMappingsandremoveMappingsare 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.Checks
getMappings, exact lookups, prefix and recursive patterns, again afterremoveMappingsandremove, 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.copyagrees with anArrayListmodel for every start index and array length tried, across chunk borders and splits.Validation
Full local build on Temurin 21 (
clean verifywith 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-baselinesaccepts it.🤖 Generated with Claude Code