Skip to content

Commit

Permalink
Speedup slicing from CompositeBytesReference (elastic#104861)
Browse files Browse the repository at this point in the history
A few things here:
1. we don't allocate when slicing so no need for the non-zero-cost bounds checks from readArraySize, just read a vint.
2. we can skip creating an array in the slice method for the common single buffer case, this shows up quite hot in the profile
3. Binary search isn't all that useful for finding the end offset in the sub references when we already have the start offset.
Even for very long buffers and long slices forward searching should be faster on modern CPUs.
  • Loading branch information
original-brownbear authored Jan 29, 2024
1 parent b205407 commit 6608a87
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,17 @@ public BytesReference slice(int from, int length) {
// for slices we only need to find the start and the end reference
// adjust them and pass on the references in between as they are fully contained
final int to = from + length;
final int limit = getOffsetIndex(to - 1);
final int start = getOffsetIndex(from);
final BytesReference[] inSlice = new BytesReference[1 + (limit - start)];
for (int i = 0, j = start; i < inSlice.length; i++) {
inSlice[i] = references[j++];
int limit = start;
for (int i = start + 1; i < offsets.length && offsets[i] < to; i++) {
limit = i;
}
int inSliceOffset = from - offsets[start];
if (inSlice.length == 1) {
return inSlice[0].slice(inSliceOffset, length);
if (start == limit) {
return references[start].slice(inSliceOffset, length);
}
final BytesReference[] inSlice = new BytesReference[1 + (limit - start)];
System.arraycopy(references, start, inSlice, 0, inSlice.length);
// now adjust slices in front and at the end
inSlice[0] = inSlice[0].slice(inSliceOffset, inSlice[0].length() - inSliceOffset);
inSlice[inSlice.length - 1] = inSlice[inSlice.length - 1].slice(0, to - offsets[limit]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private ReleasableBytesReference retainAndSkip(int len) throws IOException {

@Override
public ReleasableBytesReference readReleasableBytesReference() throws IOException {
final int len = readArraySize();
final int len = readVInt();
return retainAndSkip(len);
}

Expand Down

0 comments on commit 6608a87

Please sign in to comment.