Skip to content

Commit 382da4d

Browse files
committed
Merge pull request #23 from phraktle/perf-opt
minor code cleanup
2 parents c40000e + db9bbf9 commit 382da4d

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

sample/Benchmark.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public class Benchmark {
2121
public static void main(String[] args) throws IOException, InvalidDatabaseException {
2222
File file = new File(args.length > 0 ? args[0] : "GeoLite2-City.mmdb");
2323
System.out.println("No caching");
24-
loop("Warming up", file, WARMUPS, new NoCache());
25-
loop("Benchmarking", file, BENCHMARKS, new NoCache());
24+
loop("Warming up", file, WARMUPS, NoCache.getInstance());
25+
loop("Benchmarking", file, BENCHMARKS, NoCache.getInstance());
2626

2727
System.out.println("With caching");
2828
loop("Warming up", file, WARMUPS, new CHMCache());

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,22 @@
55
import com.fasterxml.jackson.databind.JsonNode;
66

77
/**
8-
* A no-op cache.
8+
* A no-op cache singleton.
99
*/
1010
public class NoCache implements NodeCache {
1111

12+
private static final NoCache INSTANCE = new NoCache();
13+
14+
private NoCache() {
15+
}
16+
1217
@Override
1318
public JsonNode get(int key, Loader loader) throws IOException {
1419
return loader.load(key);
1520
}
1621

22+
public static NoCache getInstance() {
23+
return INSTANCE;
24+
}
25+
1726
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public final class Reader implements Closeable {
2020
(byte) 0xCD, (byte) 0xEF, 'M', 'a', 'x', 'M', 'i', 'n', 'd', '.',
2121
'c', 'o', 'm'};
2222

23-
private static final NodeCache NO_CACHE = new NoCache();
24-
2523
private final int ipV4Start;
2624
private final Metadata metadata;
2725
private final AtomicReference<BufferHolder> bufferHolderReference;
@@ -52,7 +50,7 @@ public enum FileMode {
5250
* @throws IOException if there is an error opening or reading from the file.
5351
*/
5452
public Reader(File database) throws IOException {
55-
this(database, NO_CACHE);
53+
this(database, NoCache.getInstance());
5654
}
5755

5856
/**
@@ -76,7 +74,7 @@ public Reader(File database, NodeCache cache) throws IOException {
7674
* @throws IOException if there is an error reading from the Stream.
7775
*/
7876
public Reader(InputStream source) throws IOException {
79-
this(source, NO_CACHE);
77+
this(source, NoCache.getInstance());
8078
}
8179

8280
/**
@@ -101,7 +99,7 @@ public Reader(InputStream source, NodeCache cache) throws IOException {
10199
* @throws IOException if there is an error opening or reading from the file.
102100
*/
103101
public Reader(File database, FileMode fileMode) throws IOException {
104-
this(database, fileMode, NO_CACHE);
102+
this(database, fileMode, NoCache.getInstance());
105103
}
106104

107105
/**

src/test/java/com/maxmind/db/PointerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class PointerTest {
1717
public void testWithPointers() throws IOException {
1818
File file = ReaderTest.getFile("maps-with-pointers.raw");
1919
BufferHolder ptf = new BufferHolder(file, FileMode.MEMORY);
20-
Decoder decoder = new Decoder(new NoCache(), ptf.get(), 0);
20+
Decoder decoder = new Decoder(NoCache.getInstance(), ptf.get(), 0);
2121

2222
ObjectMapper om = new ObjectMapper();
2323

0 commit comments

Comments
 (0)