Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -899,8 +899,8 @@ private char newJump(int jump) {
if (longJumps == null) {
longJumps = new int[16]; // create table on first use
} else if (longJumpCount == longJumps.length) {
// expand the table by 50% to fit additional entries
longJumps = Arrays.copyOf(longJumps, longJumpCount + (longJumpCount >> 1));
// expand the table by 50%, ensuring progress even after compaction to one entry
longJumps = Arrays.copyOf(longJumps, longJumpCount + Math.max(1, longJumpCount >> 1));
}
longJumps[longJumpCount] = jump;
return (char) (longJumpCount++ | LONG_JUMP_MARKER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
class ClassNameTrieTest {
Expand Down Expand Up @@ -262,6 +263,30 @@ void tooManyLongJumps() {
IllegalArgumentException.class, () -> builder.put("" + ((char) 67) + ((char) 2), 1));
}

@ParameterizedTest
@ValueSource(booleans = {false, true})
void growsLongJumpTableAfterCompaction(boolean newBuilder) {
ClassNameTrie.Builder builder = new ClassNameTrie.Builder();
String padding = padding(7700);
for (char branch = 'a'; branch <= 'h'; branch++) {
builder.put("aa" + branch + padding, 1);
}
builder.put("ab", 2); // first long jump skips the padded sub-trie
ClassNameTrie compacted = builder.buildTrie(); // compacts the long-jump table to one entry
if (newBuilder) {
builder = new ClassNameTrie.Builder(compacted);
}

builder.put("ac", 3); // needs a second long jump

ClassNameTrie trie = builder.buildTrie();
for (char branch = 'a'; branch <= 'h'; branch++) {
assertEquals(1, trie.apply("aa" + branch + padding));
}
assertEquals(2, trie.apply("ab"));
assertEquals(3, trie.apply("ac"));
}

@Test
void failedInsertionPreservesExistingMappings() {
String padding = padding(7700);
Expand Down
Loading