Skip to content

Commit f4c9bd0

Browse files
committed
Move nodeByteSize and searchTreeSize to Reader
They were not public and are used in hot paths.
1 parent ecb2b7d commit f4c9bd0

2 files changed

Lines changed: 9 additions & 17 deletions

File tree

src/main/java/com/maxmind/db/Metadata.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,4 @@ public record Metadata(
5151
public Date buildDate() {
5252
return new Date(buildEpoch.longValue() * 1000);
5353
}
54-
55-
/**
56-
* @return the nodeByteSize
57-
*/
58-
int nodeByteSize() {
59-
return recordSize / 4;
60-
}
61-
62-
/**
63-
* @return the searchTreeSize
64-
*/
65-
long searchTreeSize() {
66-
return nodeCount * nodeByteSize();
67-
}
6854
}

src/main/java/com/maxmind/db/Reader.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public final class Reader implements Closeable {
2323

2424
private final long ipV4Start;
2525
private final Metadata metadata;
26+
private final int nodeByteSize;
27+
private final long searchTreeSize;
2628
private final AtomicReference<BufferHolder> bufferHolderReference;
2729
private final NodeCache cache;
2830
private final ConcurrentHashMap<Class<?>, CachedConstructor<?>> constructors;
@@ -157,6 +159,10 @@ private Reader(BufferHolder bufferHolder, String name, NodeCache cache) throws I
157159
var metadataDecoder = new Decoder(this.cache, buffer, start);
158160
this.metadata = metadataDecoder.decode(start, Metadata.class);
159161

162+
// Calculate and cache these values as they are used in hot paths
163+
this.nodeByteSize = this.metadata.recordSize() / 4;
164+
this.searchTreeSize = this.metadata.nodeCount() * this.nodeByteSize;
165+
160166
this.ipV4Start = this.findIpV4StartNode(buffer);
161167

162168
this.constructors = new ConcurrentHashMap<>();
@@ -382,7 +388,7 @@ long readNode(Buffer buffer, long nodeNumber, int index)
382388
throws InvalidDatabaseException {
383389
// index is the index of the record within the node, which
384390
// can either be 0 or 1.
385-
var baseOffset = nodeNumber * this.metadata.nodeByteSize();
391+
var baseOffset = nodeNumber * this.nodeByteSize;
386392

387393
var recordSize = this.metadata.recordSize();
388394
return switch (recordSize) {
@@ -413,7 +419,7 @@ <T> T resolveDataPointer(
413419
Class<T> cls
414420
) throws IOException {
415421
long resolved = (pointer - this.metadata.nodeCount())
416-
+ this.metadata.searchTreeSize();
422+
+ this.searchTreeSize;
417423

418424
if (resolved >= buffer.capacity()) {
419425
throw new InvalidDatabaseException(
@@ -426,7 +432,7 @@ <T> T resolveDataPointer(
426432
var decoder = new Decoder(
427433
this.cache,
428434
buffer,
429-
this.metadata.searchTreeSize() + DATA_SECTION_SEPARATOR_SIZE,
435+
this.searchTreeSize + DATA_SECTION_SEPARATOR_SIZE,
430436
this.constructors
431437
);
432438
return decoder.decode(resolved, cls);

0 commit comments

Comments
 (0)