-
Notifications
You must be signed in to change notification settings - Fork 829
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
Changes from 1 commit
62bcd2d
029190b
9698899
813ae4b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -276,6 +276,14 @@ public void writeBytes (byte[] bytes, int offset, int count) throws KryoExceptio | |
} | ||
} | ||
|
||
public void writeBytesFromLong (long bytes, int count) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have renamed to |
||
require(count); | ||
position += count; | ||
for (int i = count - 1; i >= 0; i--) { | ||
byteBuffer.put((byte) (bytes >> (i << 3))); | ||
} | ||
} | ||
|
||
// int: | ||
|
||
public void writeInt (int value) throws KryoException { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
|
@@ -274,6 +273,17 @@ 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 writeBytesFromLong (long bytes, int count) { | ||
require(count); | ||
int p = position; | ||
position = p + count; | ||
for (int i = count - 1; i >= 0; i--) { | ||
buffer[p++] = (byte) (bytes >> (i << 3)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Writing could be one less shift per iteration (untested):
Same for ByteBufferOutput. Reading could match, like:
However, for reading what you have may be better: simpler initializer and similar operations per iteration. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried to do that, but the read side got a bit more complex: long bytes = buffer[p] >= 0 ? 0 : ~(-1L >>> ((8-count) << 3));
for (int i = (count - 1) << 3; i >= 0; i -= 8) {
bytes |= (buffer[p++] & (long) 0xFF) << i;
} And also much slower (while the write side stayed the same when it comes to speed): So in 813ae4b I did something different - I unrolled the loops manually replacing it with some switches and repeated code. This got the reading/writing a little bit faster: |
||
} | ||
} | ||
|
||
// int: | ||
|
||
/** Writes a 4 byte int. */ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is better than my initial idea of
readBytes2
,readBytes3
, etc. Nice!I don't hate the name
readBytesAsLong
, but what if it wasreadLong(int count)
? Should we have areadInt(int count)
for 2-3 bytes?The first line of the method should be:
Same with other methods taking a
count
parameter.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 029190b, and the
readInt(int count)
in 9698899.