From e9ca031d7652ea8dee9f7f3671a607ed21aaa6b3 Mon Sep 17 00:00:00 2001 From: at055612 <22818309+at055612@users.noreply.github.com> Date: Thu, 14 Dec 2023 12:05:32 +0000 Subject: [PATCH] Add minor perf optimisation ByteBufferPoolImpl4 --- .../bytebuffer/ByteBufferPoolImpl4.java | 17 ++++++++++-- .../bytebuffer/TestByteBufferPoolImpl4.java | 26 +++++++++++++++++++ unreleased_changes/20231214_120431_673__0.md | 19 ++++++++++++++ 3 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 unreleased_changes/20231214_120431_673__0.md diff --git a/stroom-lmdb/src/main/java/stroom/bytebuffer/ByteBufferPoolImpl4.java b/stroom-lmdb/src/main/java/stroom/bytebuffer/ByteBufferPoolImpl4.java index d18e04b70b2..513f87327e4 100644 --- a/stroom-lmdb/src/main/java/stroom/bytebuffer/ByteBufferPoolImpl4.java +++ b/stroom-lmdb/src/main/java/stroom/bytebuffer/ByteBufferPoolImpl4.java @@ -75,6 +75,10 @@ public class ByteBufferPoolImpl4 implements ByteBufferPool { // If no count is provided for a buffer size in the config then this value is used. private static final int DEFAULT_MAX_BUFFERS_PER_QUEUE = 50; + // The offset for a one byte buffer + private static final int ONE_BYTE_BUFFER_OFFSET = 0; + // The offset for a ten byte buffer + private static final int TEN_BYTE_BUFFER_OFFSET = 1; // In each of these collections, the index/offset is the log10 of the buffer size, // i.e. 1 => 0, 10 => 1, 100 => 2 etc. @@ -523,8 +527,17 @@ private ByteBuffer createNewBufferIfAllowed(final int offset) { return byteBuffer; } - private int getOffset(final int minCapacity) { - return (int) Math.ceil(Math.log10(minCapacity)); + // Pkg private for testing + static int getOffset(final int minCapacity) { + if (minCapacity <= 10) { + // Minor optimisation as a lot of the requests for buffers will be for int/longs + // so this saves a bit of maths. + return minCapacity <= 1 + ? ONE_BYTE_BUFFER_OFFSET + : TEN_BYTE_BUFFER_OFFSET; + } else { + return (int) Math.ceil(Math.log10(minCapacity)); + } } private int getNextOffset(final int offset) { diff --git a/stroom-lmdb/src/test/java/stroom/bytebuffer/TestByteBufferPoolImpl4.java b/stroom-lmdb/src/test/java/stroom/bytebuffer/TestByteBufferPoolImpl4.java index e1a4fdfa418..2091cc1f11a 100644 --- a/stroom-lmdb/src/test/java/stroom/bytebuffer/TestByteBufferPoolImpl4.java +++ b/stroom-lmdb/src/test/java/stroom/bytebuffer/TestByteBufferPoolImpl4.java @@ -1,7 +1,11 @@ package stroom.bytebuffer; +import stroom.test.common.TestUtil; + import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -13,6 +17,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; +import java.util.stream.Stream; class TestByteBufferPoolImpl4 { @@ -328,4 +333,25 @@ void testIsPowerOf10() { i *= 10; } } + + @TestFactory + Stream testGetOffset() { + return TestUtil.buildDynamicTestStream() + .withInputAndOutputType(int.class) + .withSingleArgTestFunction(ByteBufferPoolImpl4::getOffset) + .withSimpleEqualityAssertion() + .addCase(-1, 0) + .addCase(0, 0) + .addCase(1, 0) + .addCase(2, 1) + .addCase(10, 1) + .addCase(11, 2) + .addCase(100, 2) + .addCase(101, 3) + .addCase(1000, 3) + .addCase(1001, 4) + .addCase(10000, 4) + .addCase(10001, 5) + .build(); + } } diff --git a/unreleased_changes/20231214_120431_673__0.md b/unreleased_changes/20231214_120431_673__0.md new file mode 100644 index 00000000000..d608bf23426 --- /dev/null +++ b/unreleased_changes/20231214_120431_673__0.md @@ -0,0 +1,19 @@ +* Add minor performance optimisation to the byte buffer pool used by the reference data store. + + +```sh +# ONLY the top line will be included as a change entry in the CHANGELOG. +# The entry should be in GitHub flavour markdown and should be written on a SINGLE +# line with no hard breaks. You can have multiple change files for a single GitHub issue. +# The entry should be written in the imperative mood, i.e. 'Fix nasty bug' rather than +# 'Fixed nasty bug'. +# +# Examples of acceptable entries are: +# +# +# * Issue **123** : Fix bug with an associated GitHub issue in this repository +# +# * Issue **namespace/other-repo#456** : Fix bug with an associated GitHub issue in another repository +# +# * Fix bug with no associated GitHub issue. +```