Write VarInt as fast as possible by unrolling the loop#363
Open
AkmalFairuz wants to merge 1 commit intoSandertv:masterfrom
Open
Write VarInt as fast as possible by unrolling the loop#363AkmalFairuz wants to merge 1 commit intoSandertv:masterfrom
AkmalFairuz wants to merge 1 commit intoSandertv:masterfrom
Conversation
Owner
|
I understand your point of making varints as fast as possible, but I have quite some concerns over the worthiness of doing this. The performance gains are minimal for the majority of the varints (0.3-0.4ns/varint for 1/2 bytes) and the code for a relatively simple feature blows up to over 400 lines. That does not sound like a compromise worth making, especially given the fact that varints, to my knowledge, are not a performance bottleneck in the first place. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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