hohav / peppi

Rust parser for Slippi SSBM replay files
MIT License
37 stars 9 forks source link

Optimized Event-handler match arm order #59

Closed Walnut356 closed 1 year ago

Walnut356 commented 1 year ago

If rust pattern matching checks arms in the given order, this should be an easy way to save tens of thousands of comparisons per replay, hundreds of thousands for doubles/IC's.

Eventually Rust should be getting something like the [[likely]]/[[unlikely]] decorators in c++ soon™, but until then manually reordering accomplishes the same thing.

Events are re-ordered by how often they occur per replay (per-character frame events > frame start/end > game start/end/gecko) and those are ordered by how long ago they were added to the file spec, since it's more likely that a replay will contain old features than new ones.

For a 2.5 minute (9,000 frames) 1v1 game, that should save 3 comparisons per frame for each player on pre and post frame events, which is 54,000 comparisons. If this is a 3.0.0+ replay, you're also saving 2 comparisons per frame on frame end events which is an additional 18,000 comparisons. Not much, but for a change this small it's kinda nice.

NickCondron commented 1 year ago

This is probably a good change, but I didn't see any performance improvement when running with my benchmarks. I think the branch predictor probably optimizes this all away. There might still be branch mispredictions between the common events (pre, post, start, end) that we could avoid with a more structured approach.

And fyi run rustfmt before committing so it will catch that trailing whitespace.

Walnut356 commented 1 year ago

Ah yeah, i'm too used to python. Now that I think on it, the most likely branch misses are probably from item frames anyway since those are much more "random".