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

Atlas Core: #1523

Closed
wants to merge 1 commit into from
Closed

Atlas Core: #1523

wants to merge 1 commit into from

Conversation

manolama
Copy link
Contributor

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.

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.
@manolama manolama added this to the 1.7.0 milestone Feb 25, 2023
* 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
Copy link
Contributor

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.

Copy link
Contributor Author

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 = {
Copy link
Contributor

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 {
Copy link
Contributor

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.

Copy link
Contributor Author

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 = {
Copy link
Contributor

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)

Copy link
Contributor Author

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 = {
Copy link
Contributor

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)

@manolama manolama closed this Feb 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants