ppy / osu

rhythm is just a *click* away!
https://osu.ppy.sh
MIT License
15.15k stars 2.25k forks source link

Fix performance overhead from ternary state bindable callbacks when selection is changing #28387

Closed bdach closed 4 months ago

bdach commented 4 months ago

RFC.

Closes https://github.com/ppy/osu/issues/28369.

The reporter of the issue was incorrect; it's not the beat snap grid that is causing the problem, it's something far stupider than that.

When the current selection changes, EditorSelectionHandler.UpdateTernaryStates() is supposed to update the state of ternary bindables to reflect the reality of the current selection. This in turn will fire bindable change callbacks for said ternary toggles, which heavily use EditorBeatmap.PerformOnSelection().

The thing about that method is that it will attempt to check whether any changes were actually made to avoid producing empty undo states, but to do this, it must serialise out the entire beatmap to a stream and then binary equality check that to determine whether any changes were actually made:

https://github.com/ppy/osu/blob/7b14c77e43e4ee96775a9fcb6843324170fa70bb/osu.Game/Screens/Edit/EditorChangeHandler.cs#L65-L69

As goes without saying, this is very expensive and unnecessary, which leads to stuff like keeping a selection box active while a taiko beatmap is playing under it dog slow. So to attempt to mitigate that, add precondition checks to every single ternary callback of this sort to avoid this serialisation overhead.

And yes, those precondition checks use linq, and that is still faster than not having them.

before after
1717489170 1717489431