Skip to content

Commit 333357a

Browse files
authored
Return updated offset from putVInt etc (elastic#141713)
The return values of `putVInt` and `putMultiByteVInt` are always used to update the offset in the buffer, but this is already available in the method so we may as well just return it directly.
1 parent 387ac4c commit 333357a

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ public void writeVInt(int i) throws IOException {
227227
if (25 <= Integer.numberOfLeadingZeros(i)) {
228228
writeByte((byte) i);
229229
} else if (MAX_VINT_BYTES <= capacity()) {
230-
position += putMultiByteVInt(buffer, i, position);
230+
position = putMultiByteVInt(buffer, i, position);
231231
} else {
232232
writeVIntWithBoundsChecks(i);
233233
}
@@ -246,9 +246,9 @@ private void writeVIntWithBoundsChecks(int i) throws IOException {
246246
public void writeVIntArray(int[] values) throws IOException {
247247
int position = this.position;
248248
if ((values.length + 1) * MAX_VINT_BYTES <= endPosition - position) {
249-
position += putVInt(buffer, values.length, position);
249+
position = putVInt(buffer, values.length, position);
250250
for (var value : values) {
251-
position += putVInt(buffer, value, position);
251+
position = putVInt(buffer, value, position);
252252
}
253253
this.position = position;
254254
} else {
@@ -264,7 +264,7 @@ private void writeVIntArrayWithBoundsChecks(int[] values) throws IOException {
264264
int lastSafePosition = this.endPosition - MAX_VINT_BYTES;
265265
while (i < values.length) {
266266
while (i < values.length && position <= lastSafePosition) {
267-
position += putVInt(buffer, values[i++], position);
267+
position = putVInt(buffer, values[i++], position);
268268
}
269269
this.position = position;
270270
while (capacity() < MAX_VINT_BYTES && i < values.length) {
@@ -364,7 +364,7 @@ public void writeString(String str) throws IOException {
364364
final int charCount = str.length();
365365
int position = this.position;
366366
if (MAX_VINT_BYTES + charCount * MAX_CHAR_BYTES <= endPosition - position) {
367-
position += putVInt(buffer, charCount, position);
367+
position = putVInt(buffer, charCount, position);
368368
for (int i = 0; i < charCount; i++) {
369369
position = putCharUtf8(buffer, str.charAt(i), position);
370370
}
@@ -383,7 +383,7 @@ public void writeOptionalString(String str) throws IOException {
383383
int position = this.position;
384384
if (1 + MAX_VINT_BYTES + charCount * MAX_CHAR_BYTES <= endPosition - position) {
385385
buffer[position++] = (byte) 1;
386-
position += putVInt(buffer, charCount, position);
386+
position = putVInt(buffer, charCount, position);
387387
for (int i = 0; i < charCount; i++) {
388388
position = putCharUtf8(buffer, str.charAt(i), position);
389389
}
@@ -401,7 +401,7 @@ public void writeGenericString(String str) throws IOException {
401401
int position = this.position;
402402
if (1 + MAX_VINT_BYTES + charCount * MAX_CHAR_BYTES <= endPosition - position) {
403403
buffer[position++] = (byte) 0;
404-
position += putVInt(buffer, charCount, position);
404+
position = putVInt(buffer, charCount, position);
405405
for (int i = 0; i < charCount; i++) {
406406
position = putCharUtf8(buffer, str.charAt(i), position);
407407
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public void writeVInt(int i) throws IOException {
196196
if (bytesNeeded > remainingBytesInPage) {
197197
super.writeVInt(i);
198198
} else {
199-
this.currentOffset = currentOffset + StreamOutputHelper.putMultiByteVInt(this.currentBufferPool, i, currentOffset);
199+
this.currentOffset = StreamOutputHelper.putMultiByteVInt(this.currentBufferPool, i, currentOffset);
200200
}
201201
}
202202

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public static int writeString(String str, OutputStream outputStream) throws IOEx
6464
*/
6565
public static int writeString(String str, byte[] buffer, int prefixLength, OutputStream outputStream) throws IOException {
6666
final int charCount = str.length();
67-
int offset = prefixLength + putVInt(buffer, charCount, prefixLength);
67+
int offset = putVInt(buffer, charCount, prefixLength);
6868
int total = 0;
6969
for (int i = 0; i < charCount; i++) {
7070
final int c = str.charAt(i);
@@ -138,12 +138,12 @@ public static int writeGenericString(String value, OutputStream outputStream) th
138138
* Put the integer {@code i} into the given {@code buffer} starting at the given {@code offset}, formatted as per
139139
* {@link StreamOutput#writeVInt}. Performs no bounds checks: callers must verify that there is enough space in {@code buffer} first.
140140
*
141-
* @return number of bytes written.
141+
* @return updated offset (original offset plus number of bytes written)
142142
*/
143143
public static int putVInt(byte[] buffer, int i, int offset) {
144144
if (Integer.numberOfLeadingZeros(i) >= 25) {
145145
buffer[offset] = (byte) i;
146-
return 1;
146+
return offset + 1;
147147
}
148148
return putMultiByteVInt(buffer, i, offset);
149149
}
@@ -152,17 +152,16 @@ public static int putVInt(byte[] buffer, int i, int offset) {
152152
* Put the integer {@code i} into the given {@code buffer} starting at the given {@code offset}, formatted as per
153153
* {@link StreamOutput#writeVInt}. Performs no bounds checks: callers must verify that there is enough space in {@code buffer} first.
154154
*
155-
* @return number of bytes written.
155+
* @return updated offset (original offset plus number of bytes written)
156156
*/
157157
// extracted from putVInt() to allow the hot single-byte path to be inlined
158158
public static int putMultiByteVInt(byte[] buffer, int i, int offset) {
159-
int index = offset;
160159
do {
161-
buffer[index++] = ((byte) ((i & 0x7f) | 0x80));
160+
buffer[offset++] = ((byte) ((i & 0x7f) | 0x80));
162161
i >>>= 7;
163162
} while ((i & ~0x7F) != 0);
164-
buffer[index++] = (byte) i;
165-
return index - offset;
163+
buffer[offset++] = (byte) i;
164+
return offset;
166165
}
167166

168167
}

0 commit comments

Comments
 (0)