Write VarInt as fast as possible by unrolling the loop #363
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
VarInt is a hot path in the Minecraft Bedrock network protocol, so we need to make it as fast as possible. After some testing and benchmarking, I found that unrolling the loop makes VarInt writes faster. In most cases, we usually write 1-2-byte VarInts. For 2-byte VarInts, the speed is about the same, but for 1-byte VarInts it’s around 13% faster. As you can see from the benchmark results, writing VarInts of 3 bytes or more becomes significantly faster!.
Test & Benchmark Code: https://gist.github.com/AkmalFairuz/daadc682603ae5f1d0f77b7fd748853e
Inspired by: https://steinborn.me/posts/performance/how-fast-can-you-write-a-varint/
Benchmark Result