-
Notifications
You must be signed in to change notification settings - Fork 289
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
Atlas Core: #1523
Atlas Core: #1523
Conversation
Add some methods too the ArrayHelper object for dealing with byte arrays. Add setLong(3) and getLong(2) to to set and read longs from arbitrary positions in byte arrays. Add memcmp(2) and memcmpMaybeNull(2) to evaluate the difference between the contents of two byte arrays. Add byteArraysEquals(2) as a simple helper to determine if the length and content of two byte arrays are the same using memcmpMaybeNull(2). Borrowed from https://github.com/OpenTSDB/opentsdb/blob/9c1b775657254414e7c1b768c0f6804778ed6ff2/common/src/main/java/net/opentsdb/utils/Bytes.java.
* True if both arrays are null or have identical content and length. Otherwise | ||
* false. | ||
*/ | ||
def byteArraysEquals(a: Array[Byte], b: Array[Byte]): Boolean = memcmpMaybeNull(a, b) == 0 |
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.
Why not use Arrays.equals(byte[], byte[])? They seem to have the same behavior and the built in can potentially benefit from things like intrinsics.
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.
Yeah I can use the new unsigned, thanks!
* 0 if the two arrays are identical, otherwise the difference between the | ||
* first two different bytes, otherwise the different between their lengths. | ||
*/ | ||
private def memcmp(a: Array[Byte], b: Array[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.
In jdk 9+ there is Arrays.compareUnsigned(byte[], byte[]). I think that should have the same ordering as used here.
return 0 // in order to NPE if either a or b is null. | ||
} | ||
var diff = 0 | ||
breakable { |
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.
I would recommend avoiding Breaks.breakable
. It is syntactic sugar to have something like the java break
when using lambdas, but relies on exceptions to achieve the control flow which isn't particularly efficient.
A better option here would be to use a while loop instead of a for comprehension.
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.
Oh ew, silly Scala. Thanks.
* @param offset | ||
* An offset into the byte array. | ||
*/ | ||
def setLong(b: Array[Byte], n: Long, offset: Int): Unit = { |
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.
Any reason not to just use ByteBuffer?
ByteBuffer.wrap(b).putLong(offset, n)
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.
I'll use the ByteBuffer for now, have to verify the compiler will actually skip the object creation if our throughput grows.
* @return | ||
* The signed long read from the array. | ||
*/ | ||
def getLong(b: Array[Byte], offset: Int): Long = { |
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.
ByteBuffer.wrap(b).getLong(offset)
Add some methods too the ArrayHelper object for dealing with byte arrays. Add setLong(3) and getLong(2) to to set and read longs from arbitrary positions in byte arrays.
Add memcmp(2) and memcmpMaybeNull(2) to evaluate the difference between the contents of two byte arrays.
Add byteArraysEquals(2) as a simple helper to determine if the length and content of two byte arrays are the same using memcmpMaybeNull(2). Borrowed from https://github.com/OpenTSDB/opentsdb/blob/9c1b775657254414e7c1b768c0f6804778ed6ff2/common/src/main/java/net/opentsdb/utils/Bytes.java.