GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Branchless varint decoding by franz1981 · Pull Request #14050 · netty/netty Conversation
Contributor
Motivation: varint decoding fall shortly when input length is not predictable Modification: Implements branchless and batchy varint decoding Result: Better performance when inputs length is not predictable
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
|
These are the results of the benchmark on my Ryzen box The interesting ones are the the This last point on varint bytes could be not very realistic if most of the protobuf messages have "similar" length NOTE Due to the highly accurate branch predictor of my Ryzen box, the number of inputs which makes the branch-misses to become relevant is ~128K, but as usual, YMMV. NOTE 2: not very proud of this benchmark, because, due to the variable nature of input(s), avgs times can be less meaningful - and the same applies to perf counters (if |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
6117214 to
d029cb3
Compare
Motivation: varint decoding fall shortly when input length is not predictable Modification: Implements branchless and batchy varint decoding Result: Better performance when inputs length is not predictable
d029cb3 to
57defc5
Compare
| return readRawVarint40(buffer, wholeOrMore); | ||
| } | ||
| int bitsToKeep = Integer.numberOfTrailingZeros(firstOneOnStop) + 1; | ||
| buffer.skipBytes(bitsToKeep >> 3); |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
I've noticed something very fun here: if I use readerIndex(int) it get slightly slower :(
which is very weird, because (FYI @chrisvest ), skipBytes check accessibility, while readerIndex(int), nope!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
How much use is the protobuf codec seeing? I thought grpc-java implemented their own. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
IDK, it was a weekend fun PR, this one, so, I didn't verified yet :P In case, I should send this PR elsewhere? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
The interesting bit, as proposed by @jasonk000 in a different chat (informal and nerdy), is that this same approach could be reused on utf8 decoding too, although it needs special handling of the Latin case (single byte) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| // this is not brilliant, we have a data dependency here | ||
| int wholeWithContinuations = (wholeOrMore << shiftToHighlightTheWhole) >> shiftToHighlightTheWhole; | ||
| // mix them up as per varint spec while dropping the continuation bits | ||
| int result = wholeWithContinuations & 0x7F | |
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This can be done in two steps instead of four.
wwc = (wwc & 0x7F007F) | ((wwc & 0x7F007F00) >> 1);
wwc = (wwc & 0x3FFF) | ((wwc & 0x3FFF0000) >> 2);
Probably no difference, just for the kicks.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
There's a "small" but real improvement, worthy to use this, IMO, many thanks!
Let me know if the tons of comments make sense!
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
In theory this would also be a fit for the PEXT instruction exposed via Integer.compress since Java 19. But even when using Java 19+, that's risky because e.g. Zen 2 doesn't have proper hardware for that instruction, and other architectures fall back to a more expensive implementation.
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
The reason will be displayed to describe this comment to others. Learn more.
This is very nice, actually - let me check on the JDK main
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
These are the changes after @bonzini smart suggestion! which shows a relevant improvement, instead; it is likely that although I relied on not have dependency between the 4 bytes masks (between each others), I've introduced dep on how to obtain a clean version (the absolute value) of each, while this version won't do it (the IPC is now approaching 4.9/5 on my Ryzen, while it was at 4.7), well done! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
would be great if any Apple users could give it a shot |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Ah, the same bit packing trick can also be applied to readRawVarint40. Also, I think this: can indeed be written with a lot fewer data dependencies: The idea is that thisVarintMask has 0s above the first one of firstOneOnStop, and 1s at and below it. For example if firstOneOnStop is 0x800080 (where the last 0x80 is the only one that matters), then thisVarintMask is 0xFF. On x86, computing thisVarintMask is even a single BLSMSK instruction! (And indeed my train of thought started with "there must be a BMI instruction for that and maybe you can write it in Java") |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Thanks @bonzini I didn't knew of the BLSMSK instruction (!!!): I had this previous comment before d029cb3#diff-1550582c1bcfe50538698be3c39964078cca007dc5acb1503d54416a659a80faR86-R91 which should be similar (because I still need the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Another possibility is this small optimization on |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
And @bonzini was very right, this new version is further improving perf a bit: making this very similar in perf than the original method, for predictable inputs as well |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
this one doesn't seem to improve (the opposite), but as usual: my Ryzen story, I have to read the ASM first, but thanks for #14050 (comment): my previous branch at d029cb3#diff-1550582c1bcfe50538698be3c39964078cca007dc5acb1503d54416a659a80faR86-R91 was broken, because I didn't used the |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
Motivation: varint decoding fall shortly when input length is not predictable Modification: Implements branchless and batchy varint decoding Result: Better performance when inputs length is not predictable
|
Thanks, @franz1981 ! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…prefix is incomplete (#17550) Motivation: Since #14050 two incomplete length prefix cases are no longer treated as "need more data" by `ProtobufVarint32FrameDecoder`: - If only 3 bytes are readable and all of them have the continuation bit set, `readRawVarint24` returned the partial value and left the readerIndex advanced. `decode()` then used the low 21 bits of the real length. When these bits are 0, i.e. the message length is an exact multiple of 2 MiB, an empty frame was emitted, the prefix bytes were consumed and the rest of the stream was decoded from the wrong position. A single 2 MiB message split after the third prefix byte was delivered as about 2 million bogus frames without any exception. - If exactly 4 bytes are readable and all of them have the continuation bit set, `readRawVarint40` threw a `CorruptedFrameException` although the fifth byte may simply not have been received yet. Before #14050 both cases reset the readerIndex and returned 0. Modifications: - `readRawVarint24` resets the readerIndex and returns 0 when the third byte still has the continuation bit set. - `readRawVarint40` returns 0 without moving the readerIndex when only 4 bytes are readable, and only throws when the fifth byte is malformed. - Add tests that split 1 to 5 byte length prefixes at every position, including lengths that are exact multiples of 2 MiB. Result: `ProtobufVarint32FrameDecoder` produces the same frames regardless of how the length prefix is fragmented across reads. Fixes #17549.
…hen the length prefix is incomplete (#17553) Auto-port of #17550 to 4.1 Cherry-picked commit: 335aed7 --- Motivation: Since #14050 two incomplete length prefix cases are no longer treated as "need more data" by `ProtobufVarint32FrameDecoder`: - If only 3 bytes are readable and all of them have the continuation bit set, `readRawVarint24` returned the partial value and left the readerIndex advanced. `decode()` then used the low 21 bits of the real length. When these bits are 0, i.e. the message length is an exact multiple of 2 MiB, an empty frame was emitted, the prefix bytes were consumed and the rest of the stream was decoded from the wrong position. A single 2 MiB message split after the third prefix byte was delivered as about 2 million bogus frames without any exception. - If exactly 4 bytes are readable and all of them have the continuation bit set, `readRawVarint40` threw a `CorruptedFrameException` although the fifth byte may simply not have been received yet. Before #14050 both cases reset the readerIndex and returned 0. Modifications: - `readRawVarint24` resets the readerIndex and returns 0 when the third byte still has the continuation bit set. - `readRawVarint40` returns 0 without moving the readerIndex when only 4 bytes are readable, and only throws when the fifth byte is malformed. - Add tests that split 1 to 5 byte length prefixes at every position, including lengths that are exact multiples of 2 MiB. Result: `ProtobufVarint32FrameDecoder` produces the same frames regardless of how the length prefix is fragmented across reads. Fixes #17549. --------- Co-authored-by: 오규찬 (B팀 서버) <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
…hen the length prefix is incomplete (#17554) Auto-port of #17550 to 5.0 Cherry-picked commit: 335aed7 --- Motivation: Since #14050 two incomplete length prefix cases are no longer treated as "need more data" by `ProtobufVarint32FrameDecoder`: - If only 3 bytes are readable and all of them have the continuation bit set, `readRawVarint24` returned the partial value and left the readerIndex advanced. `decode()` then used the low 21 bits of the real length. When these bits are 0, i.e. the message length is an exact multiple of 2 MiB, an empty frame was emitted, the prefix bytes were consumed and the rest of the stream was decoded from the wrong position. A single 2 MiB message split after the third prefix byte was delivered as about 2 million bogus frames without any exception. - If exactly 4 bytes are readable and all of them have the continuation bit set, `readRawVarint40` threw a `CorruptedFrameException` although the fifth byte may simply not have been received yet. Before #14050 both cases reset the readerIndex and returned 0. Modifications: - `readRawVarint24` resets the readerIndex and returns 0 when the third byte still has the continuation bit set. - `readRawVarint40` returns 0 without moving the readerIndex when only 4 bytes are readable, and only throws when the fifth byte is malformed. - Add tests that split 1 to 5 byte length prefixes at every position, including lengths that are exact multiples of 2 MiB. Result: `ProtobufVarint32FrameDecoder` produces the same frames regardless of how the length prefix is fragmented across reads. Fixes #17549. --------- Co-authored-by: 오규찬 (B팀 서버) <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
…hen the length prefix is incomplete (netty#17553) Auto-port of netty#17550 to 4.1 Cherry-picked commit: 335aed7 --- Motivation: Since netty#14050 two incomplete length prefix cases are no longer treated as "need more data" by `ProtobufVarint32FrameDecoder`: - If only 3 bytes are readable and all of them have the continuation bit set, `readRawVarint24` returned the partial value and left the readerIndex advanced. `decode()` then used the low 21 bits of the real length. When these bits are 0, i.e. the message length is an exact multiple of 2 MiB, an empty frame was emitted, the prefix bytes were consumed and the rest of the stream was decoded from the wrong position. A single 2 MiB message split after the third prefix byte was delivered as about 2 million bogus frames without any exception. - If exactly 4 bytes are readable and all of them have the continuation bit set, `readRawVarint40` threw a `CorruptedFrameException` although the fifth byte may simply not have been received yet. Before netty#14050 both cases reset the readerIndex and returned 0. Modifications: - `readRawVarint24` resets the readerIndex and returns 0 when the third byte still has the continuation bit set. - `readRawVarint40` returns 0 without moving the readerIndex when only 4 bytes are readable, and only throws when the fifth byte is malformed. - Add tests that split 1 to 5 byte length prefixes at every position, including lengths that are exact multiples of 2 MiB. Result: `ProtobufVarint32FrameDecoder` produces the same frames regardless of how the length prefix is fragmented across reads. Fixes netty#17549. --------- Co-authored-by: 오규찬 (B팀 서버) <[email protected]> Co-authored-by: Norman Maurer <[email protected]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.