Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid allocation in BigDecimal serializer with alternative way to write bytes. #1016

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/com/esotericsoftware/kryo/io/ByteBufferInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,30 @@ public void readBytes (byte[] bytes, int offset, int count) throws KryoException
}
}

public int readInt (int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
position += count;
ByteBuffer byteBuffer = this.byteBuffer;
switch (count) {
case 1:
return byteBuffer.get();
case 2:
return byteBuffer.get() << 8
| byteBuffer.get() & 0xFF;
case 3:
return byteBuffer.get() << 16
| (byteBuffer.get() & 0xFF) << 8
| byteBuffer.get() & 0xFF;
case 4:
return byteBuffer.get() << 24
| (byteBuffer.get() & 0xFF) << 16
| (byteBuffer.get() & 0xFF) << 8
| byteBuffer.get() & 0xFF;
}
throw new IllegalStateException(); // impossible
}

// int:

public int readInt () throws KryoException {
Expand Down
27 changes: 27 additions & 0 deletions src/com/esotericsoftware/kryo/io/ByteBufferOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,33 @@ public void writeBytes (byte[] bytes, int offset, int count) throws KryoExceptio
}
}

public void writeInt (int bytes, int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
position += count;
ByteBuffer byteBuffer = this.byteBuffer;
switch (count) {
case 1:
byteBuffer.put((byte)bytes);
break;
case 2:
byteBuffer.put((byte)(bytes >> 8));
byteBuffer.put((byte)bytes);
break;
case 3:
byteBuffer.put((byte)(bytes >> 16));
byteBuffer.put((byte)(bytes >> 8));
byteBuffer.put((byte)bytes);
break;
case 4:
byteBuffer.put((byte)(bytes >> 24));
byteBuffer.put((byte)(bytes >> 16));
byteBuffer.put((byte)(bytes >> 8));
byteBuffer.put((byte)bytes);
break;
}
}

// int:

public void writeInt (int value) throws KryoException {
Expand Down
38 changes: 38 additions & 0 deletions src/com/esotericsoftware/kryo/io/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,44 @@ public void readBytes (byte[] bytes, int offset, int count) throws KryoException
}
}

/** Reads count bytes and returns them as int, the last byte read will be the lowest byte in the int. */
public int readInt (int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
int p = position;
position = p + count;
switch (count) {
case 1:
return buffer[p];
case 2:
return buffer[p] << 8
| buffer[p+1] & 0xFF;
case 3:
return buffer[p] << 16
| (buffer[p+1] & 0xFF) << 8
| buffer[p+2] & 0xFF;
case 4:
return buffer[p] << 24
| (buffer[p+1] & 0xFF) << 16
| (buffer[p+2] & 0xFF) << 8
| buffer[p+3] & 0xFF;
}
throw new IllegalStateException(); // impossible
}

/** Reads count bytes and returns them as long, the last byte read will be the lowest byte in the long. */
public long readLong (int count) {
if (count < 0 || count > 8) throw new IllegalArgumentException("count must be >= 0 and <= 8: " + count);
if (count <= 4) {
return readInt(count);
} else {
require(count);
long highBytes = ((long) readInt(count - 4)) << 32;
long lowBytes = ((long) readInt(4)) & (1L << 32) - 1;
return highBytes | lowBytes;
}
}

// int:

/** Reads a 4 byte int. */
Expand Down
43 changes: 42 additions & 1 deletion src/com/esotericsoftware/kryo/io/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package com.esotericsoftware.kryo.io;

import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.KryoBufferOverflowException;
import com.esotericsoftware.kryo.util.Pool.Poolable;
import com.esotericsoftware.kryo.util.Util;

Expand Down Expand Up @@ -274,6 +273,48 @@ public void writeBytes (byte[] bytes, int offset, int count) throws KryoExceptio
}
}

/** Writes count bytes from long, the last byte written is the lowest byte from the long.
* Note the number of bytes is not written. */
public void writeInt (int bytes, int count) {
if (count < 0 || count > 4) throw new IllegalArgumentException("count must be >= 0 and <= 4: " + count);
require(count);
int p = position;
position = p + count;
switch (count) {
case 1:
buffer[p] = (byte)bytes;
break;
case 2:
buffer[p] = (byte)(bytes >> 8);
buffer[p+1] = (byte)bytes;
break;
case 3:
buffer[p] = (byte)(bytes >> 16);
buffer[p+1] = (byte)(bytes >> 8);
buffer[p+2] = (byte)bytes;
break;
case 4:
buffer[p] = (byte)(bytes >> 24);
buffer[p+1] = (byte)(bytes >> 16);
buffer[p+2] = (byte)(bytes >> 8);
buffer[p+3] = (byte)bytes;
break;
}
}

/** Writes count bytes from long, the last byte written is the lowest byte from the long.
* Note the number of bytes is not written. */
public void writeLong (long bytes, int count) {
if (count < 0 || count > 8) throw new IllegalArgumentException("count must be >= 0 and <= 8: " + count);
if (count <= 4) {
writeInt((int) bytes, count);
} else {
require(count);
writeInt((int) (bytes >> 32), count - 4);
writeInt((int) bytes, 4);
}
}

// int:

/** Writes a 4 byte int. */
Expand Down
Loading