RobustToolbox icon indicating copy to clipboard operation
RobustToolbox copied to clipboard

Try improve RaiseEvent performance

Open ElectroJr opened this issue 6 months ago • 0 comments

This PR makes some minor changes to try improve event raising performance a smidge, and hopefully improve comp state related event raising by a decent amount and bringing it closer to C# events. Requires https://github.com/space-wizards/space-station-14/pull/37349 to fix content tests.

Benchmarks

Using content's RaiseEventBenchmark on my machine:

Master

Method Mean Error StdDev Ratio
RaiseEvent 71.780 ns 0.1565 ns 0.1464 ns 1.00
RaiseCompEvent 27.231 ns 0.0300 ns 0.0266 ns 0.38
RaiseICompEvent 34.490 ns 0.0606 ns 0.0506 ns 0.48
RaiseCSharpEvent 1.925 ns 0.0012 ns 0.0011 ns 0.03

This PR

Method Mean Error StdDev Ratio
RaiseEvent 64.236 ns 0.1828 ns 0.1710 ns 1.00
RaiseCompEvent 20.827 ns 0.0462 ns 0.0410 ns 0.32
RaiseICompEvent 27.832 ns 0.0658 ns 0.0615 ns 0.43
RaiseNetEvent 4.076 ns 0.0248 ns 0.0220 ns 0.06
RaiseCSharpEvent 1.895 ns 0.0011 ns 0.0009 ns 0.03

The RaiseNetEvent benchmark is added in https://github.com/space-wizards/space-station-14/pull/37349

Changes

  • Replacing IEntityManager in EventBus with EntityManager
  • Making the entity system proxy methods use EventBus instead of IEventBus
  • Make event subscriptions use a single delegate
    • Currently some subscriptions chain two delegates, one of which casts an IComponent object to TComp, and another that casts the event Unit to the actual event type. Now it just uses one delegate that does both.
  • Added a new internal method for retrieving a NetId-indexed DirectedEventHandler?[]
    • This is intended to help improve perf for raising ComponentGetState and ComponentHandleState events by replacing two frozen dictionary lookups with an array index. Compare the RaiseICompEvent benchmark with RaiseNetEvent.
    • This only really makes sense for these events with a net-id index, because for any other event this would probably be a pretty sparse array.

ElectroJr avatar May 11 '25 13:05 ElectroJr