bevy icon indicating copy to clipboard operation
bevy copied to clipboard

Optimize rendering slow-down at high entity counts

Open TheRawMeatball opened this issue 3 years ago • 8 comments
trafficstars

Objective

  • Improve #3953

Solution

  • The very specific circumstances under which the render world is reset meant that the flush_as_invalid function could be replaced with one that had a noop as its init method.
  • This removes a double-writing issue leading to greatly increased performance.

Running the reproduction code in the linked issue, this change nearly doubles the framerate.

TheRawMeatball avatar Jul 31 '22 11:07 TheRawMeatball

Neat. This is the most dramatic one of these I've seen.

just the reserve_and_flush span (repro from issue) image

rparrett avatar Jul 31 '22 23:07 rparrett

Wow yeah thats a big win right there. Pretty sure this is valid. I'll give this a proper think asap.

cart avatar Aug 01 '22 00:08 cart

Changed again to a fully custom flush method that directly uses memset, this is getting even larger gains.

TheRawMeatball avatar Aug 01 '22 07:08 TheRawMeatball

Currently UB, working on a fix.

TheRawMeatball avatar Aug 01 '22 12:08 TheRawMeatball

Reverted the memset, so this works again, but others investigating why the memset version failed via UB would be appreciated.

TheRawMeatball avatar Aug 01 '22 13:08 TheRawMeatball

UB fixed with help from @PROMETHIA-27

TheRawMeatball avatar Aug 01 '22 13:08 TheRawMeatball

b02a1d741c02174269311fa36210e15c740ba048 (repro from issue) just the reserve_and_flush span Screen Shot 2022-08-01 at 6 41 39 AM

rparrett avatar Aug 01 '22 13:08 rparrett

many_sprites frame time image

many_spites frame time (run 2) image

many_cubes sphere frame time image

In all cases, yellow / this trace is this PR, red is main. Guessing we're just in the noise a bit and this is not a quiet machine at the moment. All traces were ~30 seconds though.

rparrett avatar Aug 01 '22 14:08 rparrett

I'm excited for this PR as it seems like it fixes part of the performance issues here! I'm still worried that this isn't a complete fix though. ~~This code is still reserving entities even if they have been despawned which leads to memory leakage(increased memory usage over time) and possibly over time slow performance. I can think of quite a few scenarios where users would spawn high entity counts and despawn said entities. A shmup would be one such example where you would be spawning hundreds or even thousands of bullets which get depsawned.~~ Looking at it closer it looks like it just reserves the number of entities that currently exist. I still think performance could be an issue for me, but I think a better solution might be to have a separate world.

StarArawn avatar Aug 11 '22 12:08 StarArawn

Just checking on the general status of this PR. I noticed it was removed from the 0.8.1 release. Curious as to when we might expect this to be part of mainline. Thanks!

mbolt35 avatar Aug 27 '22 23:08 mbolt35

Noticeable performance improvement for my game as well - uses extremely high entity count, yellow is this PR, red is 0.8.0: bench

hankjordan avatar Sep 03 '22 18:09 hankjordan

bors r+

cart avatar Oct 24 '22 19:10 cart