From d504dd6a05c94375ae0c3616351e9b530fa1b97a Mon Sep 17 00:00:00 2001 From: Ruben Porras Campo Date: Fri, 20 Nov 2026 12:50:58 +0100 Subject: [PATCH] perf: insert into large name lookup nodes in sublinear time 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) --- .../QualifiedNameSegmentTreeLookupTest.java | 161 ++++++++ .../META-INF/MANIFEST.MF | 2 +- com.avaloq.tools.ddk.xtext/pom.xml | 2 +- .../tools/ddk/xtext/naming/ChunkedList.java | 348 ++++++++++++++++++ .../QualifiedNameSegmentTreeLookup.java | 35 +- 5 files changed, 542 insertions(+), 6 deletions(-) create mode 100644 com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/ChunkedList.java diff --git a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java index c42cf67eb..e41b2690a 100644 --- a/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java +++ b/com.avaloq.tools.ddk.xtext.test/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookupTest.java @@ -18,14 +18,18 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.ObjectInputFilter; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; +import java.util.Random; import java.util.Set; import org.apache.logging.log4j.LogManager; @@ -43,6 +47,8 @@ public class QualifiedNameSegmentTreeLookupTest { private static final Logger LOGGER = LogManager.getLogger(QualifiedNameSegmentTreeLookupTest.class); + private static final URI SHARED = URI.createURI("scheme:/shared"); + private final QualifiedNameSegmentTreeLookup lookup = new QualifiedNameSegmentTreeLookup(URI.class, true); @Test @@ -255,6 +261,161 @@ public void testPatternMatchesWithSharedValues() { } } + @Test + public void testLargeNodes() { + for (boolean shareValues : new boolean[] {true, false}) { + QualifiedNameSegmentTreeLookup tree = new QualifiedNameSegmentTreeLookup(URI.class, shareValues); + Map> expected = new HashMap<>(); + putLargeNodes(tree, expected, 1); + assertLookup(tree, expected); + + QualifiedNameSegmentTreeLookup copy = new QualifiedNameSegmentTreeLookup(URI.class, shareValues); + copy.initializeFrom(tree); + assertLookup(copy, expected); + + Set shared = new HashSet<>(); + expected.forEach((name, values) -> { + if (values.contains(SHARED)) { + shared.add(name); + } + }); + assertEquals(shared, new HashSet<>(tree.getMappings(SHARED))); + + QualifiedName removed = shared.iterator().next(); + tree.remove(removed, SHARED); + expected.get(removed).remove(SHARED); + shared.remove(removed); + assertLookup(tree, expected); + assertEquals(shared, new HashSet<>(tree.getMappings(SHARED))); + + tree.removeMappings(SHARED); + expected.values().forEach(values -> values.remove(SHARED)); + assertLookup(tree, expected); + assertEquals(0, tree.getMappings(SHARED).size()); + } + } + + @Test + public void testLargeNodesLoadStore() throws IOException, ClassNotFoundException { + QualifiedNameSegmentTreeLookup tree = new QualifiedNameSegmentTreeLookup(URI.class, true); + Map> expected = new HashMap<>(); + putLargeNodes(tree, expected, 1); + + QualifiedNameLookup readBack = writeAndRead(tree); + assertLookup(readBack, expected); + + // insert into the large nodes read back, and store them again + putLargeNodes(readBack, expected, 2); + assertLookup(readBack, expected); + assertLookup(writeAndRead(readBack), expected); + } + + /** + * Serializes and deserializes the given lookup, accepting no other lists than {@link ArrayList} in the serialized form. + * + * @param tree + * lookup to serialize, must not be {@code null} + * @return the deserialized lookup, never {@code null} + */ + @SuppressWarnings("unchecked") + private QualifiedNameLookup writeAndRead(final QualifiedNameLookup tree) throws IOException, ClassNotFoundException { + ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); + try (ObjectOutputStream objectOutStream = new ObjectOutputStream(byteOutStream)) { + objectOutStream.writeObject(tree); + } + try (ObjectInputStream inStream = new ObjectInputStream(new ByteArrayInputStream(byteOutStream.toByteArray()))) { + inStream.setObjectInputFilter(info -> info.serialClass() != null && List.class.isAssignableFrom(info.serialClass()) + && info.serialClass() != ArrayList.class ? ObjectInputFilter.Status.REJECTED : ObjectInputFilter.Status.UNDECIDED); + return (QualifiedNameLookup) inStream.readObject(); + } + } + + /** + * Puts names in random order which give the root and the node {@code big} several times more children than a node keeps in an array list. Every seventh + * name is also mapped to {@link #SHARED}. + * + * @param tree + * lookup to fill, must not be {@code null} + * @param expected + * expected mappings to update, must not be {@code null} + * @param seed + * seed of the random names and order + */ + private void putLargeNodes(final QualifiedNameLookup tree, final Map> expected, final long seed) { + Random random = new Random(seed); + List names = new ArrayList<>(); + for (int i = 0; i < 20_000; i++) { + QualifiedName top = name("n" + token(random)); + names.add(top); + if (i % 10 == 0) { + names.add(top.append("c" + token(random))); + } + } + for (int i = 0; i < 10_000; i++) { + names.add(name("big." + token(random))); + } + for (int i = 0; i < 100; i++) { + names.add(name("z" + token(random))); + } + Collections.shuffle(names, random); + for (int i = 0; i < names.size(); i++) { + QualifiedName qn = names.get(i); + tree.put(qn, uri(qn)); + expected.computeIfAbsent(qn, k -> new HashSet<>()).add(uri(qn)); + if (i % 7 == 0) { + tree.put(qn, SHARED); + expected.get(qn).add(SHARED); + } + } + } + + private static String token(final Random random) { + return Integer.toString(random.nextInt(1_000_000_000), Character.MAX_RADIX); + } + + /** + * Asserts that exact and pattern lookups find the expected values; the expected pattern matches are computed with {@link QualifiedNamePattern#matches}. + * + * @param tree + * lookup to query, must not be {@code null} + * @param expected + * expected mappings, must not be {@code null} + */ + private void assertLookup(final QualifiedNameLookup tree, final Map> expected) { + for (Map.Entry> entry : expected.entrySet()) { + assertEquals(entry.getValue(), new HashSet<>(tree.get(entry.getKey())), entry.getKey().toString()); + } + for (String missing : List.of("big", "n", "y", "zzzzzzzzzz", "big.zzzzzzzzzz", "big.0.0")) { + assertNull(tree.get(name(missing)), missing); + } + + // z* and z** end at the upper bound node, y* and zzzzzzzzzz* match nothing + List patterns = new ArrayList<>(); + for (String pattern : List.of("n*", "n**", "big.*", "big.**", "z*", "z**", "y*", "zzzzzzzzzz*", "big.zzzzzzzzzz*")) { + patterns.add(pattern(pattern)); + } + for (char c : "05afkpuz".toCharArray()) { + for (String pattern : List.of("n" + c + "*", "n" + c + "**", "big." + c + "*", "big." + c + "**")) { + patterns.add(pattern(pattern)); + } + } + expected.keySet().stream().limit(10).forEach(name -> { + patterns.add(pattern(name)); + patterns.add(pattern(name.toString() + "*")); + patterns.add(pattern(name.toString() + "**")); + }); + for (QualifiedNamePattern pattern : patterns) { + Set matches = new HashSet<>(); + expected.forEach((name, values) -> { + if (pattern.matches(name)) { + matches.addAll(values); + } + }); + assertEquals(matches, tree.get(pattern, true), pattern.toString()); + assertEquals(matches, new HashSet<>(tree.get(pattern, false)), pattern.toString()); + } + } + // when sharing values, foo, foo.bar, foo.bar.baz and foo.baz share one value array private QualifiedNameSegmentTreeLookup createTreeWithSharedValues(final boolean shareValues, final URI v1, final URI v2, final URI v3) { QualifiedNameSegmentTreeLookup tree = new QualifiedNameSegmentTreeLookup(URI.class, shareValues); diff --git a/com.avaloq.tools.ddk.xtext/META-INF/MANIFEST.MF b/com.avaloq.tools.ddk.xtext/META-INF/MANIFEST.MF index fb7c23e82..3bb6da660 100644 --- a/com.avaloq.tools.ddk.xtext/META-INF/MANIFEST.MF +++ b/com.avaloq.tools.ddk.xtext/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: com.avaloq.tools.ddk.xtext Bundle-SymbolicName: com.avaloq.tools.ddk.xtext;singleton:=true -Bundle-Version: 17.4.7.qualifier +Bundle-Version: 17.4.8.qualifier Bundle-Vendor: Avaloq Group AG Bundle-RequiredExecutionEnvironment: JavaSE-21 Bundle-ActivationPolicy: lazy diff --git a/com.avaloq.tools.ddk.xtext/pom.xml b/com.avaloq.tools.ddk.xtext/pom.xml index bb5dd06de..da2a73f77 100644 --- a/com.avaloq.tools.ddk.xtext/pom.xml +++ b/com.avaloq.tools.ddk.xtext/pom.xml @@ -6,7 +6,7 @@ 18.0.1-SNAPSHOT ../ddk-parent - 17.4.7-SNAPSHOT + 17.4.8-SNAPSHOT com.avaloq.tools.ddk com.avaloq.tools.ddk.xtext eclipse-plugin diff --git a/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/ChunkedList.java b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/ChunkedList.java new file mode 100644 index 000000000..8a834409e --- /dev/null +++ b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/ChunkedList.java @@ -0,0 +1,348 @@ +/******************************************************************************* + * Copyright (c) 2026 Avaloq Group AG and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Avaloq Group AG - initial API and implementation + *******************************************************************************/ +package com.avaloq.tools.ddk.xtext.naming; + +import java.util.AbstractList; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Objects; +import java.util.RandomAccess; +import java.util.concurrent.locks.StampedLock; +import java.util.function.ToIntFunction; + + +/** + * A list which stores its elements in chunks of at most B = {@value #CHUNK_CAPACITY} elements. Inserting an element shifts the following elements of its + * chunk and the start indexes of the following chunks, instead of all following elements as {@link ArrayList#add(int, Object)} does. Inserting into a list of + * {@code n} elements thus takes O(B + n/B) instead of O(n) time, and building a large sorted list by binary search and insertion is no longer quadratic. + * {@link #get(int)} takes O(log(n/B)) time. + *

+ * Supports {@link #get(int)}, {@link #add(int, Object)}, {@link #size()}, {@link #clear()}, {@link #toArray()}, iteration and a + * {@link #binarySearch(ToIntFunction) binary search} which is faster than one with {@link #get(int)}. Other modifications are not supported. + *

+ * The list must be modified by one thread at a time. {@link #get(int)}, {@link #binarySearch(ToIntFunction)} and {@link #toArray()} may run concurrently + * with a modification. They then see the list before or after it, never a half-done modification, so that they do not fail where reads of an + * {@link ArrayList} in practice do not fail either. + * + * @param + * element type + */ +final class ChunkedList extends AbstractList implements RandomAccess { + + /** Maximum number of elements of a chunk. */ + static final int CHUNK_CAPACITY = 1024; + + private static final int HALF_CHUNK = CHUNK_CAPACITY / 2; + + /** Number of chunks there is room for beyond the initial ones. */ + private static final int SPARE_CHUNKS = 8; + + /** Number of reads without a lock before a read which a concurrent modification keeps invalidating takes the read lock and so blocks the writer. */ + private static final int OPTIMISTIC_READ_ATTEMPTS = 16; + + private final StampedLock lock = new StampedLock(); + + /** The chunks; only the first {@link #chunkCount} are used. Each holds at least one element unless the list is empty. */ + private Object[][] chunks; + + /** The index in the list of the first element of each chunk. */ + private int[] starts; + + private int chunkCount; + + private int elementCount; + + /** + * Creates a list with the elements of the given collection, in the order of its iterator. + * + * @param source + * the elements, must not be {@code null} + */ + ChunkedList(final Collection source) { + Object[] elements = source.toArray(); + int count = Math.max(1, (elements.length + CHUNK_CAPACITY - 1) / CHUNK_CAPACITY); + // the list may be published without synchronization; since the lock is final, a reader sees it released and so the fields initialized + long stamp = lock.writeLock(); + try { + chunks = new Object[count + SPARE_CHUNKS][]; + starts = new int[count + SPARE_CHUNKS]; + for (int c = 0; c < count; c++) { + int from = c * CHUNK_CAPACITY; + chunks[c] = Arrays.copyOfRange(elements, from, Math.min(from + CHUNK_CAPACITY, elements.length), Object[].class); + starts[c] = from; + } + chunkCount = count; + elementCount = elements.length; + } finally { + lock.unlockWrite(stamp); + } + } + + @Override + public int size() { + return elementCount; + } + + @Override + @SuppressWarnings("unchecked") + public E get(final int index) { + for (int attempt = 0; attempt < OPTIMISTIC_READ_ATTEMPTS; attempt++) { + long stamp = lock.tryOptimisticRead(); + int currentSize = elementCount; + Object element = find(index); + if (lock.validate(stamp)) { + Objects.checkIndex(index, currentSize); + return (E) element; + } + Thread.onSpinWait(); + } + long stamp = lock.readLock(); + try { + Objects.checkIndex(index, elementCount); + return (E) find(index); + } finally { + lock.unlockRead(stamp); + } + } + + /** + * Returns the element at the given index. Since {@link #get(int)} calls this without a lock first, it must not fail on a state which a concurrent + * modification left inconsistent. + * + * @param index + * the index of the element + * @return the element, or any value if the state is inconsistent or the index out of range + */ + private Object find(final int index) { + Object[][] currentChunks = chunks; + int[] currentStarts = starts; + int high = Math.min(chunkCount, Math.min(currentChunks.length, currentStarts.length)) - 1; + if (high < 0) { + return null; + } + int low = 0; + while (low < high) { + int mid = (low + high + 1) >>> 1; + if (currentStarts[mid] <= index) { + low = mid; + } else { + high = mid - 1; + } + } + Object[] chunk = currentChunks[low]; + int offset = index - currentStarts[low]; + return chunk != null && offset >= 0 && offset < chunk.length ? chunk[offset] : null; + } + + /** + * Searches this list, which must be sorted consistently with the given comparison, in O(log(n)) time like + * {@link java.util.Collections#binarySearch(java.util.List, Object, java.util.Comparator)}. Unlike a search with {@link #get(int)}, it searches the chunks + * first and then one chunk, instead of the chunks again for every element it compares. + * + * @param comparison + * compares an element with the searched key: negative if the element is smaller, zero if equal and positive if greater, must not be {@code null} + * @return index of an element equal to the key, or {@code (-(insertion point) - 1)} + */ + int binarySearch(final ToIntFunction comparison) { + for (int attempt = 0; attempt < OPTIMISTIC_READ_ATTEMPTS; attempt++) { + long stamp = lock.tryOptimisticRead(); + int result = search(comparison, stamp); + if (lock.validate(stamp)) { + return result; + } + Thread.onSpinWait(); + } + long stamp = lock.readLock(); + try { + return search(comparison, stamp); + } finally { + lock.unlockRead(stamp); + } + } + + /** + * Implements {@link #binarySearch(ToIntFunction)}. Since that calls this without a lock first, it must not fail on a state which a concurrent modification + * left inconsistent. In a consistent state, no element is {@code null}. It validates the stamp before comparing an element, so that the comparison sees + * the element as it was added, and not an element which a concurrent modification is adding. + * + * @param comparison + * compares an element with the searched key, must not be {@code null} + * @param stamp + * the stamp of the optimistic read or the read lock + * @return index of an element equal to the key, or {@code (-(insertion point) - 1)}, or any value if the state is inconsistent + */ + @SuppressWarnings("unchecked") + private int search(final ToIntFunction comparison, final long stamp) { + Object[][] currentChunks = chunks; + int[] currentStarts = starts; + int count = Math.min(chunkCount, Math.min(currentChunks.length, currentStarts.length)); + if (count == 0) { + return -1; + } + // the last chunk whose first element is not greater than the key + int low = 0; + int high = count - 1; + while (low < high) { + int mid = (low + high + 1) >>> 1; + Object[] chunk = currentChunks[mid]; + Object first = chunk != null && chunk.length > 0 ? chunk[0] : null; + if (first == null || !lock.validate(stamp)) { + return -1; + } + int cmp = comparison.applyAsInt((E) first); + if (cmp < 0) { + low = mid; + } else if (cmp > 0) { + high = mid - 1; + } else { + return currentStarts[mid]; + } + } + Object[] chunk = currentChunks[low]; + int start = currentStarts[low]; + int chunkSize = (low + 1 < count ? currentStarts[low + 1] : elementCount) - start; + if (chunk == null || chunkSize < 0 || chunkSize > chunk.length) { + return -1; + } + int lowIdx = 0; + int highIdx = chunkSize - 1; + while (lowIdx <= highIdx) { + int mid = (lowIdx + highIdx) >>> 1; + Object element = chunk[mid]; + if (element == null || !lock.validate(stamp)) { + return -1; + } + int cmp = comparison.applyAsInt((E) element); + if (cmp < 0) { + lowIdx = mid + 1; + } else if (cmp > 0) { + highIdx = mid - 1; + } else { + return start + mid; + } + } + return -(start + lowIdx + 1); + } + + @Override + public void add(final int index, final E element) { + if (index < 0 || index > elementCount) { + throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + elementCount); //$NON-NLS-1$ //$NON-NLS-2$ + } + long stamp = lock.writeLock(); + try { + int c = index == elementCount ? chunkCount - 1 : chunkIndex(index); + int offset = index - starts[c]; + int chunkSize = chunkSize(c); + if (chunkSize == chunks[c].length) { + if (chunkSize < CHUNK_CAPACITY) { + chunks[c] = Arrays.copyOf(chunks[c], CHUNK_CAPACITY); + } else { + split(c); + if (offset > HALF_CHUNK) { + c++; + offset -= HALF_CHUNK; + } + chunkSize = chunkSize(c); + } + } + Object[] chunk = chunks[c]; + System.arraycopy(chunk, offset, chunk, offset + 1, chunkSize - offset); + chunk[offset] = element; + for (int i = c + 1; i < chunkCount; i++) { + starts[i]++; + } + elementCount++; + modCount++; + } finally { + lock.unlockWrite(stamp); + } + } + + @Override + public void clear() { + long stamp = lock.writeLock(); + try { + chunks = new Object[1 + SPARE_CHUNKS][]; + chunks[0] = new Object[0]; + starts = new int[1 + SPARE_CHUNKS]; + chunkCount = 1; + elementCount = 0; + modCount++; + } finally { + lock.unlockWrite(stamp); + } + } + + @Override + public Object[] toArray() { + long stamp = lock.readLock(); + try { + Object[] result = new Object[elementCount]; + for (int c = 0; c < chunkCount; c++) { + System.arraycopy(chunks[c], 0, result, starts[c], chunkSize(c)); + } + return result; + } finally { + lock.unlockRead(stamp); + } + } + + /** + * Returns the index of the chunk holding the element at the given index. + * + * @param index + * the index of an element, {@code 0 <= index < size()} + * @return the index of the chunk + */ + private int chunkIndex(final int index) { + int low = 0; + int high = chunkCount - 1; + while (low < high) { + int mid = (low + high + 1) >>> 1; + if (starts[mid] <= index) { + low = mid; + } else { + high = mid - 1; + } + } + return low; + } + + private int chunkSize(final int c) { + return (c + 1 < chunkCount ? starts[c + 1] : elementCount) - starts[c]; + } + + /** + * Splits the given full chunk into two chunks of half its size. + * + * @param c + * the index of a chunk holding {@value #CHUNK_CAPACITY} elements + */ + private void split(final int c) { + if (chunkCount == chunks.length) { + int newLength = chunkCount + (chunkCount >> 1) + 1; + chunks = Arrays.copyOf(chunks, newLength); + starts = Arrays.copyOf(starts, newLength); + } + System.arraycopy(chunks, c + 1, chunks, c + 2, chunkCount - c - 1); + System.arraycopy(starts, c + 1, starts, c + 2, chunkCount - c - 1); + Object[] left = chunks[c]; + Object[] right = new Object[CHUNK_CAPACITY]; + System.arraycopy(left, HALF_CHUNK, right, 0, CHUNK_CAPACITY - HALF_CHUNK); + Arrays.fill(left, HALF_CHUNK, CHUNK_CAPACITY, null); + chunks[c + 1] = right; + starts[c + 1] = starts[c] + HALF_CHUNK; + chunkCount++; + } + +} diff --git a/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookup.java b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookup.java index fdd60ee48..9af22e411 100644 --- a/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookup.java +++ b/com.avaloq.tools.ddk.xtext/src/com/avaloq/tools/ddk/xtext/naming/QualifiedNameSegmentTreeLookup.java @@ -64,6 +64,12 @@ private static class SegmentNode implements Externalizable { protected static final int DEFAULT_CHILD_CAPACITY = 4; + /** + * Number of children from which a node keeps them in a {@link ChunkedList}. Inserting into an {@link ArrayList} shifts all following children, which makes + * filling nodes with several 100'000 children quadratic. + */ + protected static final int LARGE_CHILD_COUNT = 4096; + private String segment; protected Object[] values; protected List children; @@ -260,7 +266,7 @@ void merge(final QualifiedName name, int segIdx, final Object[] newValues) { // if (idx < 0) { SegmentNode child = new SegmentNode(seg); idx = -(idx + 1); - children.add(idx, child); + addChild(idx, child); if (name.getSegmentCount() == segIdx) { child.values = newValues; return; @@ -274,6 +280,21 @@ void merge(final QualifiedName name, int segIdx, final Object[] newValues) { // } } + /** + * Inserts the given child, keeping the children in a {@link ChunkedList} once there are {@value #LARGE_CHILD_COUNT} of them. + * + * @param idx + * index at which to insert the child + * @param child + * child to insert, must not be {@code null} + */ + protected void addChild(final int idx, final SegmentNode child) { + if (children.size() >= LARGE_CHILD_COUNT && !(children instanceof ChunkedList)) { + children = new ChunkedList<>(children); + } + children.add(idx, child); + } + /** * Accepts the given visitor and uses it to visit all children in depth-first search order. * @@ -319,7 +340,11 @@ List> getMappings(final Object value) { * key, corresponding to {@link SegmentNode#segment}, to search for * @return index of found node or (-(insertion point) - 1) */ + @SuppressWarnings("PMD.LooseCoupling") // a ChunkedList searches faster itself than through get protected int binarySearch(final List list, final String key) { + if (list instanceof ChunkedList chunkedList) { + return chunkedList.binarySearch(node -> node.segment.compareTo(key)); + } int low = 0; int high = list.size() - 1; @@ -361,7 +386,8 @@ public void writeExternal(final ObjectOutput out) throws IOException { } } } - out.writeObject(children); + // the serialized form keeps a plain ArrayList + out.writeObject(children instanceof ChunkedList ? new ArrayList<>(children) : children); } @SuppressWarnings("unchecked") @@ -382,7 +408,8 @@ public void readExternal(final ObjectInput in) throws IOException, ClassNotFound } } } - children = (List) in.readObject(); + List list = (List) in.readObject(); + children = list != null && list.size() >= LARGE_CHILD_COUNT ? new ChunkedList<>(list) : list; } } @@ -435,7 +462,7 @@ void merge(final QualifiedName name, int segIdx, final Object[] newValues) { // if (idx < 0) { SegmentNode child = new ValueSharingSegmentNode(seg); idx = -(idx + 1); - children.add(idx, child); + addChild(idx, child); if (name.getSegmentCount() == segIdx) { child.values = Arrays.equals(values, newValues) ? values : newValues; return;