GeyserMC / MCProtocolLib

A library for communication with a Minecraft client/server.
MIT License
724 stars 200 forks source link

Optimize stack #828

Closed AlexProgrammerDE closed 2 months ago

Kas-tle commented 3 months ago

Can you explain why these changes are needed?

AlexProgrammerDE commented 3 months ago

As explained on Discord, the main benefits are reduced allocations, using newer and faster Java 11+ NIO APIs and improved error-handling.

AlexProgrammerDE commented 3 months ago

I'm working on a little extra stuff for this PR. We can heavily simplify the entire pipeline by switching to AttributeKeys for controlling encryption/compression from netty. That way we don't need to modify the pipeline after initialization.

AlexProgrammerDE commented 3 months ago

Nah, lets get merged first what is already here. I don't want the PR to get too big.But I roughly know how this should be implemented. Maybe I'll make it into another PR.

Tim203 commented 3 months ago

Do you have some benchmarks of the before and after?

AlexProgrammerDE commented 3 months ago

Do you have some benchmarks of the before and after?

Unfortunately I don't have the tooling right now to test this properly. But I can tell you about why this is faster. For compression/decompression now nio buffers are used. That reduces the array allocations and works more closely with the deflater/inflater classes. That way operations are slightly faster. For encryption the same methods are used, but it's faster because of removal of unnecessary array size allocations. With the encryption algorithm used in mc the in and out size of the algorithm is the same and thus no extra arrays need to be allocated, we can simply just reuse the input buffer.