Skip to content

Commit 01c4b08

Browse files
committed
Started optimizing tree-lookup code using mmap
1 parent 772dfcd commit 01c4b08

3 files changed

Lines changed: 24 additions & 25 deletions

File tree

src/main/java/com/maxmind/maxminddb/Decoder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,8 +351,6 @@ private long[] sizeFromCtrlByte(int ctrlByte, long offset)
351351
}
352352

353353
private byte[] getByteArray(int length) {
354-
byte[] bytes = new byte[length];
355-
this.mmap.get(bytes);
356-
return bytes;
354+
return Util.getByteArray(this.mmap, length);
357355
}
358356
}

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

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public Reader(File database) throws MaxMindDbException, IOException {
5858

5959
this.metadata = new Metadata(metadataDecoder.decode(start).getNode());
6060

61-
this.decoder = new Decoder(this.mmap, this.metadata.searchTreeSize + DATA_SECTION_SEPARATOR_SIZE);
61+
this.decoder = new Decoder(this.mmap, this.metadata.searchTreeSize
62+
+ DATA_SECTION_SEPARATOR_SIZE);
6263

6364
if (this.DEBUG) {
6465
Log.debug(this.metadata.toString());
@@ -74,7 +75,7 @@ public JsonNode get(InetAddress address) throws MaxMindDbException,
7475
return null;
7576
}
7677

77-
this.fc.position(pointer);
78+
this.mmap.position((int) pointer);
7879
return this.resolveDataPointer(pointer);
7980
}
8081

@@ -142,41 +143,33 @@ long record = nodes[bit];
142143

143144
private long[] readNode(long nodeNumber) throws IOException,
144145
MaxMindDbException {
145-
ByteBuffer buffer = ByteBuffer
146-
.wrap(new byte[this.metadata.nodeByteSize]);
147-
this.fc.position(nodeNumber * this.metadata.nodeByteSize);
146+
this.mmap.position((int) nodeNumber * this.metadata.nodeByteSize);
148147

149-
this.fc.read(buffer);
148+
byte[] buffer = Util
149+
.getByteArray(this.mmap, this.metadata.nodeByteSize);
150150

151151
if (this.DEBUG) {
152152
Log.debug("Node bytes", buffer);
153153
}
154154
return this.splitNodeIntoRecords(buffer);
155155
}
156156

157-
private long[] splitNodeIntoRecords(ByteBuffer bytes)
158-
throws MaxMindDbException {
157+
private long[] splitNodeIntoRecords(byte[] bytes) throws MaxMindDbException {
159158
long[] nodes = new long[2];
160159
switch (this.metadata.recordSize) {
161160
case 24:
162-
nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 0,
163-
3));
164-
nodes[1] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 3,
165-
6));
161+
nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes, 0, 3));
162+
nodes[1] = Util.decodeLong(Arrays.copyOfRange(bytes, 3, 6));
166163
return nodes;
167164
case 28:
168-
nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 0,
169-
3));
170-
nodes[1] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 4,
171-
7));
172-
nodes[0] = ((0xF0 & bytes.get(3)) << 20) | nodes[0];
173-
nodes[1] = ((0x0F & bytes.get(3)) << 24) | nodes[1];
165+
nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes, 0, 3));
166+
nodes[1] = Util.decodeLong(Arrays.copyOfRange(bytes, 4, 7));
167+
nodes[0] = ((0xF0 & bytes[3]) << 20) | nodes[0];
168+
nodes[1] = ((0x0F & bytes[3]) << 24) | nodes[1];
174169
return nodes;
175170
case 32:
176-
nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 0,
177-
4));
178-
nodes[1] = Util.decodeLong(Arrays.copyOfRange(bytes.array(), 4,
179-
8));
171+
nodes[0] = Util.decodeLong(Arrays.copyOfRange(bytes, 0, 4));
172+
nodes[1] = Util.decodeLong(Arrays.copyOfRange(bytes, 4, 8));
180173
return nodes;
181174
default:
182175
throw new MaxMindDbException("Unknown record size: "

src/main/java/com/maxmind/maxminddb/Util.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.maxmind.maxminddb;
22

3+
import java.nio.ByteBuffer;
4+
35
class Util {
46

57
public Util() {
@@ -22,4 +24,10 @@ static long decodeLong(byte[] bytes) {
2224
return i;
2325
}
2426

27+
static byte[] getByteArray(ByteBuffer buffer, int length) {
28+
byte[] bytes = new byte[length];
29+
buffer.get(bytes);
30+
return bytes;
31+
}
32+
2533
}

0 commit comments

Comments
 (0)