Skip to content

Commit 3de5f66

Browse files
authored
Merge pull request #73 from maxmind/greg/no-synchronized
Remove synchronized
2 parents 95f4c1f + 39d0631 commit 3de5f66

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ CHANGELOG
1010
control byte in the data section rather than an
1111
`ArrayIndexOutOfBoundsException`. Reported by Edwin Delgado H. GitHub
1212
#68.
13+
* In order to improve performance when lookups are done from multiple
14+
threads, a use of `synchronized` has been removed. GitHub #65 & #69.
15+
* `jackson-databind` has been upgraded to 2.11.0.
1316

1417
1.3.1 (2020-03-03)
1518
------------------

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>com.fasterxml.jackson.core</groupId>
4747
<artifactId>jackson-databind</artifactId>
48-
<version>2.10.3</version>
48+
<version>2.11.0</version>
4949
</dependency>
5050
</dependencies>
5151
<build>

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

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,22 @@ final class BufferHolder {
5858
* Returns a duplicate of the underlying ByteBuffer. The returned ByteBuffer
5959
* should not be shared between threads.
6060
*/
61-
synchronized ByteBuffer get() {
61+
ByteBuffer get() {
62+
// The Java API docs for buffer state:
63+
//
64+
// Buffers are not safe for use by multiple concurrent threads. If a buffer is to be used by more than
65+
// one thread then access to the buffer should be controlled by appropriate synchronization.
66+
//
67+
// As such, you may think that this should be synchronized. This used to be the case, but we had several
68+
// complaints about the synchronization causing contention, e.g.:
69+
//
70+
// * https://github.com/maxmind/MaxMind-DB-Reader-java/issues/65
71+
// * https://github.com/maxmind/MaxMind-DB-Reader-java/pull/69
72+
//
73+
// Given that we are not modifying the original ByteBuffer in any way and all currently known and most
74+
// reasonably imaginable implementations of duplicate() only do read operations on the original buffer object,
75+
// the risk of not synchronizing this call seems relatively low and worth taking for the performance benefit
76+
// when lookups are being done from many threads.
6277
return this.buffer.duplicate();
6378
}
6479
}

0 commit comments

Comments
 (0)