Repository navigation
perf: insert into large name lookup nodes in sublinear time - #1641
rubenporras wants to merge 1 commit 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)
joaodinissf
left a comment
There was a problem hiding this comment.
Thanks Ruben, nice win. Approving as it is.
I verified it locally: the full validation is green on Temurin 21, ChunkedList behaves like an ArrayList at every chunk and threshold boundary I tried, the serialized form is byte-identical to master's in both directions, and 1'000'000 random inserts take about 1 s instead of about 20 s.
One gap: no test covers the StampedLock paths, so a version with validate always returning true would still pass CI. I will open a follow-up PR with a concurrency test, since I already have a reader/writer stress harness from this review, and the code itself checks out today. No need to hold this PR for it.
Two observations, for you to take or leave:
- Reads that walk a large node are 1.5x to 2x slower (prefix patterns,
getMappings,removeMappings), because the inherited iterator does a chunk search per element. I will try to recover that in the same follow-up and report numbers. - Not from this PR: a lookup during inserts can return null or a neighbour's values, at the same rate on master and here. Worth an issue?
Posted by Claude Code on behalf of @joaodinissf.
|
Follow-up in #1642: a concurrency test for lookups in large nodes, plus two commits that bring Posted by Claude Code on behalf of @joaodinissf. |
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)