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 @@ -293,7 +293,8 @@ public static Big construct(List<Map.Entry<String,Integer>> entries)
LinkedHashMap<Integer,String> byId = new LinkedHashMap<Integer,String>();

// First: calculate size of primary hash area
final int size = findSize(byId.size());
// NOTE: must size from `entries`; `byId` is still empty at this point
final int size = findSize(entries.size());
final int mask = size-1;
// and allocate enough to contain primary/secondary, expand for spillovers as need be
int alloc = size + (size>>1);
Expand Down Expand Up @@ -329,6 +330,10 @@ public static Big construct(List<Map.Entry<String,Integer>> entries)
return new Big(byId, mask, spills, keys, indices);
}

// Accessors for testing of hash area sizing
int hashArea() { return _hashMask+1; }
int spillCount() { return _spillCount; }

@Override
public String findEnumByIndex(int index) {
return _enumsById.get(index);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package tools.jackson.dataformat.protobuf.schema;

import java.util.LinkedHashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;

/**
* Tests for {@link EnumLookup}, in particular that the "big" variant sizes its
* hash area from the actual entry count (it used to always size for 0 entries,
* pushing nearly everything into the linear-scan spill area).
*/
public class EnumLookupTest
{
@Test
public void testBigLookupResolvesAllEntries()
{
final int count = 100;
EnumLookup lookup = EnumLookup.construct(_enumDef(count));
for (int i = 0; i < count; ++i) {
String name = "VALUE_"+i;
assertEquals(i, lookup.findEnumIndex(name), name);
assertEquals(name, lookup.findEnumByIndex(i));
}
assertEquals(-1, lookup.findEnumIndex("NO_SUCH_VALUE"));
assertEquals(count, lookup.getEnumValues().size());
}

@Test
public void testBigLookupHashAreaSizedFromEntryCount()
{
final int count = 100;
EnumLookup.Big lookup = assertInstanceOf(EnumLookup.Big.class,
EnumLookup.construct(_enumDef(count)));
// With correct sizing the primary hash area holds 128 slots; the old code
// sized for 0 entries (8 primary + 4 secondary slots), so nearly every
// entry ended up in the linearly-scanned overflow area.
assertEquals(128, lookup.hashArea());
if (lookup.spillCount() >= (count / 2)) {
throw new AssertionError("Too many spill-over entries: "+lookup.spillCount());
}
}

private ProtobufEnum _enumDef(int count) {
Map<String,Integer> values = new LinkedHashMap<>();
for (int i = 0; i < count; ++i) {
values.put("VALUE_"+i, i);
}
return new ProtobufEnum("BigEnum", values, true);
}
}
Loading