Skip to content

Commit eb73ecd

Browse files
committed
More
1 parent 6fb1afa commit eb73ecd

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

server/src/main/java/org/elasticsearch/common/io/stream/RecyclerBytesStreamOutput.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,18 @@ public void writeUTF8String(String str) throws IOException {
118118
return;
119119
}
120120

121+
// Ensure we have at least enough capacity for ASCII representation and max vint
122+
if ((charCount + 5) > (pageSize - currentPageOffset)) {
123+
ensureCapacity(charCount);
124+
}
125+
121126
// Optimistically write length assuming all ASCII (1 byte per char)
122127
long startPosition = position();
123-
writeVInt(charCount);
124-
125-
// Ensure we have at least enough capacity for ASCII representation
126-
if (charCount > (pageSize - currentPageOffset)) {
127-
ensureCapacity(charCount);
128+
if (Integer.numberOfLeadingZeros(charCount) >= 25) {
129+
bytesRefBytes[bytesRefOffset + currentPageOffset] = (byte) charCount;
130+
++currentPageOffset;
131+
} else {
132+
currentPageOffset += putMultiByteVInt(bytesRefBytes, charCount, bytesRefOffset + currentPageOffset);
128133
}
129134

130135
if (writeAsciiChars(str, charCount) == false) {
@@ -156,7 +161,6 @@ private boolean writeAsciiChars(String str, int charCount) {
156161
currentPageOffset += charsToWrite;
157162
charIndex += charsToWrite;
158163

159-
// Check if we need to move to next page AFTER writing
160164
if (currentPageOffset == pageSize && charIndex < charCount) {
161165
nextPage();
162166
}

server/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public static int putVInt(byte[] buffer, int i, int off) {
254254
return putMultiByteVInt(buffer, i, off);
255255
}
256256

257-
private static int putMultiByteVInt(byte[] buffer, int i, int off) {
257+
protected static int putMultiByteVInt(byte[] buffer, int i, int off) {
258258
int index = off;
259259
do {
260260
buffer[index++] = ((byte) ((i & 0x7f) | 0x80));

0 commit comments

Comments
 (0)