Skip to content

Commit

Permalink
Merge branch '7.2' of github.com:gchq/stroom into 7.2
Browse files Browse the repository at this point in the history
  • Loading branch information
stroomdev66 committed Dec 14, 2023
2 parents 1d44bcd + edbef06 commit a8a72ea
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 {

Expand Down Expand Up @@ -328,4 +333,25 @@ void testIsPowerOf10() {
i *= 10;
}
}

@TestFactory
Stream<DynamicTest> 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();
}
}
19 changes: 19 additions & 0 deletions unreleased_changes/20231214_120431_673__0.md
Original file line number Diff line number Diff line change
@@ -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.
```

0 comments on commit a8a72ea

Please sign in to comment.