Skip to content

Optimize packet event handling #2028

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: 2.0
Choose a base branch
from

Conversation

Jeleyk
Copy link

@Jeleyk Jeleyk commented Feb 27, 2025

Optimize packet event handling

Summary

Added efficient packet type mapping to avoid processing unnecessary checks and optimized packet event handling by only iterating over relevant handlers

I've tested basic scenarios (fly, hitboxes), but I believe this needs a thorough review before merging, as I could have inadvertently disrupted the logic of individual handlers.

Performance improvement

I observed high CPU usage during the processing of large numbers of clientbound packets, especially during high-traffic scenarios. After implementing these optimizations, CPU consumption was significantly reduced.

The main optimization changes the packet handling mechanism to use a hashmap of packet types to handlers instead of iterating through all registered handlers for every packet. This maintains the same logical behavior while dramatically improving performance.

noReminderTicks = 0; // Exempt vehicles
}
protected void registerReceiveHandlers(PacketHandlerRegistry<PacketReceiveEvent> registry) {
registry.registerHandler(event -> {
Copy link
Contributor

@MachineBreaker MachineBreaker Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cant you register different handlers? Dont see why to run against all packets (for the noReminderTicks setters)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is possible to register multiple handlers. I tried to keep the original handler logic, and in this class under the condition (isViaPleaseStopUsingProtocolHacksOnYourServer && player.inVehicle()) we need to set noReminderTicks to 0 on absolutely any incoming packet. I assume this is redundant behavior, but I'm hesitant to change the original handler logic.

Copy link
Author

@Jeleyk Jeleyk Feb 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also had to register handlers for all packets in some other classes, since that was done in the original. Most of the time it was handlers like this:

registry.registerHandler(event -> {
            if (player.gamemode == GameMode.SPECTATOR || isTickPacket(event.getPacketType())) {
                hasInteracted = false;
            }
        });

@Jeleyk Jeleyk force-pushed the optimize-packet-events branch from 5b7a878 to 924085e Compare March 8, 2025 09:52
@Jeleyk Jeleyk marked this pull request as ready for review March 8, 2025 09:52
@Jeleyk Jeleyk requested a review from ManInMyVan March 8, 2025 17:03
@ghost
Copy link

ghost commented Apr 7, 2025

this packethandlerregistry thing seems cool, you should probably PR this to packetevents though so all plugins can benefit.

@ManInMyVan ManInMyVan added the status: rebase required The pull request needs rebasing onto the merge branch label May 4, 2025
@Axionize
Copy link
Contributor

Could you join the Discord? https://discord.gg/Z5q8ubSW

As a project maintainer I'd like to talk to you about this in detail with less lag time.

@Axionize
Copy link
Contributor

Also @Jeleyk do you by chance have a spark profile showing the performance cost of processing these listeners? I want to make sure we're optimizing for the right thing here, as generally in Grim the highest CPU usage comes from PE's wrapping, writing, and decoding of packets and little if none at all from the actual execution of the listeners themselves.

@Jeleyk
Copy link
Author

Jeleyk commented May 12, 2025

I'm already on discord. My tag: jele__. I took these measurements for 2.5 months and I don't have any saved reports, but I can say that the manifestation of high CPU consumption was with a lot of Server2Client packages, and as far as I remember there was an emphasis on iterable stub and similar things with multiple iterations. I can re-do the measurements if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: rebase required The pull request needs rebasing onto the merge branch
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants