Skip to content

Commit 222e820

Browse files
committed
JVM VarHandle for short, char
1 parent 06d17ac commit 222e820

File tree

4 files changed

+42
-17
lines changed

4 files changed

+42
-17
lines changed

src/main/java/com/trivago/fastutilconcurrentwrapper/io/BAIS.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,18 +160,24 @@ public boolean readBoolean () {
160160
/// @see DataInputStream#readShort
161161
/// (short)((read() << 8)|(read() & 0xFF))
162162
@Override
163-
public short readShort() {
164-
return (short)((read() << 8)|(read() & 0xFF));
163+
public short readShort () {
164+
short v = JBytes.DirectByteArrayAccess.getShort(buf, pos);
165+
pos += 2;
166+
return v;
165167
}
166168

167169
@Override
168170
public int readUnsignedShort() {
169-
return ((read() & 0xFF) << 8)|(read() & 0xFF);
171+
int v = JBytes.DirectByteArrayAccess.getUnsignedShort(buf, pos);
172+
pos += 2;
173+
return v;
170174
}
171175

172176
@Override
173177
public char readChar() {
174-
return (char)(((read() & 0xFF) << 8)|(read() & 0xFF));
178+
char v = JBytes.DirectByteArrayAccess.getChar(buf, pos);
179+
pos += 2;
180+
return v;
175181
}
176182

177183
@Override
@@ -205,12 +211,16 @@ public long readLong () {
205211

206212
@Override
207213
public float readFloat () {
208-
return Float.intBitsToFloat(readInt());
214+
float v = JBytes.DirectByteArrayAccess.getFloat(buf, pos);
215+
pos += 4;
216+
return v;
209217
}
210218

211219
@Override
212220
public double readDouble () {
213-
return Double.longBitsToDouble(readLong());
221+
double v = JBytes.DirectByteArrayAccess.getDouble(buf, pos);
222+
pos += 8;
223+
return v;
214224
}
215225

216226
@Override @Deprecated

src/main/java/com/trivago/fastutilconcurrentwrapper/io/BAOS.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,19 @@ public void writeByte(int v) {
194194
}
195195

196196
@Override
197-
public void writeShort(int v) {
198-
write(v >> 8);
199-
write(v);
197+
public void writeShort (int v) {
198+
grow(2);
199+
JBytes.DirectByteArrayAccess.setShort(buf, position, (short) v);
200+
position += 2;
201+
if (count < position) count = position;
200202
}
201203

202204
@Override
203-
public void writeChar(int v) {
204-
write(v >> 8);
205-
write(v);
205+
public void writeChar (int v) {
206+
grow(2);
207+
JBytes.DirectByteArrayAccess.setChar(buf, position, (char) v);
208+
position += 2;
209+
if (count < position) count = position;
206210
}
207211

208212
@Override
@@ -234,12 +238,18 @@ public void writeLong (long v) {
234238

235239
@Override
236240
public void writeFloat(float v) {
237-
writeInt(Float.floatToIntBits(v));
241+
grow(4);
242+
JBytes.DirectByteArrayAccess.setFloat(buf, position, v);
243+
position += 4;
244+
if (count < position) count = position;
238245
}
239246

240247
@Override
241248
public void writeDouble(double v) {
242-
writeLong(Double.doubleToLongBits(v));
249+
grow(8);
250+
JBytes.DirectByteArrayAccess.setDouble(buf, position, v);
251+
position += 8;
252+
if (count < position) count = position;
243253
}
244254

245255
/**

src/test/java/com/trivago/fastutilconcurrentwrapper/io/BAOSTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,8 @@ void _oos () throws IOException, ClassNotFoundException {
998998
}
999999

10001000
/// 500k: 5_760 ms, op/s=86_802
1001+
/// VarHandle int&long: 2_333 ms, op/s=214_281
1002+
/// all: 2_281, op/s=219_195
10011003
@Test
10021004
void _benchmark () {
10031005
IntStream.range(0, 2).forEach(__->{

src/test/java/com/trivago/fastutilconcurrentwrapper/io/FastByteArrayStreamsTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ void testEOF () throws UTFDataFormatException {
154154
assertEquals(-1, r.readByte());
155155
assertEquals(0xff, r.readUnsignedByte());
156156
assertTrue(r.readBoolean());//?
157-
assertEquals(-1, r.readShort());
158-
assertEquals(0xFFFF, r.readUnsignedShort());
159-
assertEquals(0xFFFF, r.readChar());
157+
//assertEquals(-1, r.readShort());
158+
assertThrows(ArrayIndexOutOfBoundsException.class, r::readShort);
159+
//assertEquals(0xFFFF, r.readUnsignedShort());
160+
assertThrows(ArrayIndexOutOfBoundsException.class, r::readUnsignedShort);
161+
//assertEquals(0xFFFF, r.readChar());
162+
assertThrows(ArrayIndexOutOfBoundsException.class, r::readChar);
160163
//assertEquals(-1, r.readInt());
161164
assertThrows(ArrayIndexOutOfBoundsException.class, r::readInt);
162165
//assertEquals(-1, r.readLong());

0 commit comments

Comments
 (0)