Skip to content

Conversation

@camshaft
Copy link
Contributor

@camshaft camshaft commented Oct 13, 2023

Description of changes:

This PR focuses on improving varint encoding a bit by:

  • improving the table function to be completely branchless (see https://godbolt.org/z/9xrxd1osd)
  • Adding a special case when we have at least 8 bytes in the target buffer and just doing a single 64 bit mov, rather than having to branch based on our integer size:
    // optimize for the case where we have at least 8 bytes left and just write a full u64 to
    // the buffer, but incrementing the offset by `self.len`.
    //
    // ignore this optimization under miri, since we're technically reading beyond the slice
    // that the encoder gives us, which miri complains about.
    if encoder.remaining_capacity() >= 8 && !cfg!(miri) {
    self.encode_oversized(encoder);
    } else {
    self.encode_maybe_undersized(encoder);
    }

I've also cleaned the code up a bit by moving the table functions into a separate module with an named struct, rather than a (u64, usize, u64).

Performance has improved by about 30-40%

varint/encode/array/1   time:   [1.6209 ns 1.6211 ns 1.6212 ns]
                        change: [-40.046% -39.123% -37.678%] (p = 0.00 < 0.05)
varint/encode/slice/1   time:   [2.4321 ns 2.4329 ns 2.4341 ns]
                        change: [-33.393% -33.335% -33.266%] (p = 0.00 < 0.05)
varint/encode/array_16/1
                        time:   [38.505 ns 38.515 ns 38.529 ns]
                        change: [-31.424% -31.257% -31.005%] (p = 0.00 < 0.05)
varint/encode/slice_16/1
                        time:   [38.676 ns 38.790 ns 38.889 ns]
                        change: [-38.523% -38.430% -38.354%] (p = 0.00 < 0.05)

Testing:

The existing tests should prevent any regressions in behavior.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@camshaft camshaft force-pushed the camshaft/varint-optimizations branch from 1234544 to a508bef Compare October 13, 2023 00:45
@WesleyRosenblum
Copy link
Contributor

2000! 🚀

@camshaft camshaft marked this pull request as ready for review October 16, 2023 16:14
@camshaft camshaft force-pushed the camshaft/varint-optimizations branch from a508bef to b847852 Compare October 20, 2023 18:43
@WesleyRosenblum
Copy link
Contributor

Can you summarize (maybe in the PR description) how the optimization works and why it achieves 30-40% better perf?

@camshaft camshaft merged commit 30027ee into main Oct 20, 2023
@camshaft camshaft deleted the camshaft/varint-optimizations branch October 20, 2023 22:41
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.

2 participants