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

fix calculate checkSum when using Java9IntHash #4140

Merged
merged 8 commits into from
Dec 7, 2023
Merged
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,13 @@ public int resume(int current, ByteBuf buffer, int offset, int len) {
} else {
byte[] b = TL_BUFFER.get();
int toRead = len;
int loopOffset = offset;
while (toRead > 0) {
int length = Math.min(toRead, b.length);
buffer.slice(offset, len).readBytes(b, 0, length);
buffer.slice(loopOffset, length).readBytes(b, 0, length);
negCrc = resume(negCrc, b, 0, length);
toRead -= length;
loopOffset += length;
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I want you to move the return to every branch, that wouldn't leave any trap in the future. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving return to every logic branch would make ~ everywhere(just like below). So I think current implementation is better to read:

public int resume(int current, ByteBuf buffer, int offset, int len) {
    if (buffer.hasMemoryAddress()) {
        return ~resume(~current, buffer.memoryAddress(), offset, len);
    }
    if (buffer.hasArray()) {
        int arrayOffset = buffer.arrayOffset() + offset;
        return ~resume(~current, buffer.array(), arrayOffset, len);
    }
    if (buffer instanceof CompositeByteBuf) {
       CompositeByteBuf compositeByteBuf = (CompositeByteBuf) buffer;
       int loopedCurrent = current;
       for (int i = 0; i < compositeByteBuf.numComponents(); i ++) {
           loopedCurrent = resume(loopedCurrent, compositeByteBuf.component(i));
       }
       return loopedCurrent;
    }
    int negCrc = ~current;
    byte[] b = TL_BUFFER.get();
    int toRead = len;
    int loopOffset = offset;
    while (toRead > 0) {
        int length = Math.min(toRead, b.length);
        buffer.slice(loopOffset, length).readBytes(b, 0, length);
        negCrc = resume(negCrc, b, 0, length);
        toRead -= length;
        loopOffset += length;
    }
    return ~negCrc;
}

Expand Down
Loading