Skip to content

Commit 13d7623

Browse files
Use Netty ByteBuf Bulk Operations for Faster Deserialization (#40158) (#40339)
* Use bulk methods to read numbers faster from byte buffers
1 parent a30bf27 commit 13d7623

File tree

4 files changed

+101
-2
lines changed

4 files changed

+101
-2
lines changed

modules/transport-netty4/src/main/java/org/elasticsearch/transport/netty4/ByteBufStreamInput.java

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
import java.io.IOException;
2929

3030
/**
31-
* A Netty {@link io.netty.buffer.ByteBuf} based {@link org.elasticsearch.common.io.stream.StreamInput}.
31+
* A Netty {@link ByteBuf} based {@link StreamInput}.
3232
*/
3333
class ByteBufStreamInput extends StreamInput {
3434

@@ -109,6 +109,39 @@ public int read(byte[] b, int off, int len) throws IOException {
109109
return len;
110110
}
111111

112+
@Override
113+
public short readShort() throws IOException {
114+
try {
115+
return buffer.readShort();
116+
} catch (IndexOutOfBoundsException ex) {
117+
EOFException eofException = new EOFException();
118+
eofException.initCause(ex);
119+
throw eofException;
120+
}
121+
}
122+
123+
@Override
124+
public int readInt() throws IOException {
125+
try {
126+
return buffer.readInt();
127+
} catch (IndexOutOfBoundsException ex) {
128+
EOFException eofException = new EOFException();
129+
eofException.initCause(ex);
130+
throw eofException;
131+
}
132+
}
133+
134+
@Override
135+
public long readLong() throws IOException {
136+
try {
137+
return buffer.readLong();
138+
} catch (IndexOutOfBoundsException ex) {
139+
EOFException eofException = new EOFException();
140+
eofException.initCause(ex);
141+
throw eofException;
142+
}
143+
}
144+
112145
@Override
113146
public void reset() throws IOException {
114147
buffer.resetReaderIndex();

plugins/transport-nio/src/main/java/org/elasticsearch/http/nio/ByteBufUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,39 @@ public int read(byte[] b, int off, int len) throws IOException {
226226
return len;
227227
}
228228

229+
@Override
230+
public short readShort() throws IOException {
231+
try {
232+
return buffer.readShort();
233+
} catch (IndexOutOfBoundsException ex) {
234+
EOFException eofException = new EOFException();
235+
eofException.initCause(ex);
236+
throw eofException;
237+
}
238+
}
239+
240+
@Override
241+
public int readInt() throws IOException {
242+
try {
243+
return buffer.readInt();
244+
} catch (IndexOutOfBoundsException ex) {
245+
EOFException eofException = new EOFException();
246+
eofException.initCause(ex);
247+
throw eofException;
248+
}
249+
}
250+
251+
@Override
252+
public long readLong() throws IOException {
253+
try {
254+
return buffer.readLong();
255+
} catch (IndexOutOfBoundsException ex) {
256+
EOFException eofException = new EOFException();
257+
eofException.initCause(ex);
258+
throw eofException;
259+
}
260+
}
261+
229262
@Override
230263
public void reset() throws IOException {
231264
buffer.resetReaderIndex();

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.io.EOFException;
2222
import java.io.IOException;
23+
import java.nio.BufferUnderflowException;
2324
import java.nio.ByteBuffer;
2425

2526
public class ByteBufferStreamInput extends StreamInput {
@@ -76,6 +77,39 @@ public void readBytes(byte[] b, int offset, int len) throws IOException {
7677
buffer.get(b, offset, len);
7778
}
7879

80+
@Override
81+
public short readShort() throws IOException {
82+
try {
83+
return buffer.getShort();
84+
} catch (BufferUnderflowException ex) {
85+
EOFException eofException = new EOFException();
86+
eofException.initCause(ex);
87+
throw eofException;
88+
}
89+
}
90+
91+
@Override
92+
public int readInt() throws IOException {
93+
try {
94+
return buffer.getInt();
95+
} catch (BufferUnderflowException ex) {
96+
EOFException eofException = new EOFException();
97+
eofException.initCause(ex);
98+
throw eofException;
99+
}
100+
}
101+
102+
@Override
103+
public long readLong() throws IOException {
104+
try {
105+
return buffer.getLong();
106+
} catch (BufferUnderflowException ex) {
107+
EOFException eofException = new EOFException();
108+
eofException.initCause(ex);
109+
throw eofException;
110+
}
111+
}
112+
79113
@Override
80114
public void reset() throws IOException {
81115
buffer.reset();

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ public String readString() throws IOException {
415415
return spare.toString();
416416
}
417417

418-
419418
public final float readFloat() throws IOException {
420419
return Float.intBitsToFloat(readInt());
421420
}

0 commit comments

Comments
 (0)