@@ -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