Commit e4eb916
authored
Optimize Visibility Systems (#22226)
# Objective
- Make visibility systems less slow.
- Saves as much as 1.2ms of CPU time on a (GPU bound) 6.7ms frame,
rendering caldera hotel.
- Fixes #22256
## Solution
- Replace the `EntityHashSet` with a component added directly to
entities. This still allows for correct change detection triggers on
visibility, but avoids hashing. This also enables parallel updates.
## Summary
- This PR lead to a rabbit hole that uncovered #22256
- This PR resolves the regression introduced since 0.17 released
- A more fair comparison is not to main (which has a regression), but to
0.17
- Compared to 0.17.3, this is a 27% speedup to the `PostUpdate` schedule
for caldera hotel on an M4 Max
<img width="558" height="251" alt="image"
src="https://github.com/user-attachments/assets/2d711e3c-b65e-4c3a-9d2a-a587f8f36aae"
/>
## Testing
- Many cubes, many foxes, caldera
- Yellow is new, red is old.
- Note the bimodality in the old traces, this was consistently
repeatable, and seems to have something to do with `EntityHashSet` and
`EntityHashMap`. Worth investigating that further, as I've seen that
bimodal behavior before, and blamed it on pcores vs ecores, but I
verified that is not happening.
- Summary: caldera hotel no longer exhibits bimodal performance with a
pathological mode that runs very slowly. Roughly comparing the ~90th
percentile performance, the optimized code is about 1.2ms faster. This
is particularly significant when you consider this is running on a
device that is already hitting 150fps (GPU bound), so it only has a
frame budget of 6.7ms.
- Notably, visibility checking was the last egregiously performing set
of systems in the common hot path of most bevy apps, and no longer stick
out as obviously slow in a frame profile:
A high level comparison of the change in CPU time by looking at just
PostUpdate between old(red) and new(yellow)
<img width="1738" height="512" alt="image"
src="https://github.com/user-attachments/assets/5252ff0f-d9f3-49d1-a3ce-309bd84d5485"
/>
### caldera hotel - all entities in view
For this test, I didn't touch the camera, so all entities were in view
at all times.
`check_visibility`
<img width="1262" height="397" alt="image"
src="https://github.com/user-attachments/assets/c80c470a-1548-42d2-838c-4e74525a6cb8"
/>
`reset_view_visibility`
<img width="1262" height="407" alt="image"
src="https://github.com/user-attachments/assets/7b550823-f832-41f1-9e42-20cab342b0f2"
/>
`mark_newly_hidden_entities_invisible`
This is 23us slower than the "fast mode" of the existing code on main,
but 180-250us *faster* than the weird "slow mode" on main. The new code
is significantly more stable, and does not exhibit the super slow mode.
<img width="1256" height="418" alt="image"
src="https://github.com/user-attachments/assets/b7cbf57d-a221-468a-9af8-3381981874b2"
/>
### caldera hotel - no entities in view
For this test, I immediately rotated the camera so the hotel was not in
view.
`check_visibility`
This is largely a wash. The old code is 2us faster, but this is likely
in the realm of noise.
<img width="1255" height="372" alt="image"
src="https://github.com/user-attachments/assets/2c795f72-ebea-4b35-ba19-a8eccd4c56fc"
/>
`reset_view_visibility`
This is a big win. The main peak is about 30us faster now, but the major
win is the worst case, which is nearly 500us faster.
<img width="1252" height="398" alt="image"
src="https://github.com/user-attachments/assets/f8a9d99f-a6e1-48db-8639-d52276dba9ab"
/>
`mark_newly_hidden_entities_invisible`
Same story as the other caldera comparison with everything in view, this
is a bit slower than the fast mode, but way faster than the slow mode on
main.
<img width="1248" height="400" alt="image"
src="https://github.com/user-attachments/assets/4564d94e-911a-4b3f-a584-c6e102ef2dd0"
/>1 parent 730b002 commit e4eb916
2 files changed
+96
-126
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
163 | 163 | | |
164 | 164 | | |
165 | 165 | | |
166 | | - | |
| 166 | + | |
| 167 | + | |
167 | 168 | | |
168 | | - | |
169 | | - | |
170 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
171 | 173 | | |
172 | | - | |
| 174 | + | |
| 175 | + | |
173 | 176 | | |
174 | 177 | | |
175 | 178 | | |
176 | | - | |
| 179 | + | |
177 | 180 | | |
178 | | - | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
179 | 198 | | |
180 | 199 | | |
181 | 200 | | |
182 | | - | |
| 201 | + | |
183 | 202 | | |
184 | 203 | | |
185 | 204 | | |
186 | 205 | | |
187 | 206 | | |
188 | | - | |
| 207 | + | |
189 | 208 | | |
190 | 209 | | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
196 | 229 | | |
197 | 230 | | |
198 | 231 | | |
199 | 232 | | |
200 | 233 | | |
201 | | - | |
202 | 234 | | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
203 | 239 | | |
204 | | - | |
205 | | - | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
206 | 245 | | |
207 | 246 | | |
208 | 247 | | |
| |||
369 | 408 | | |
370 | 409 | | |
371 | 410 | | |
372 | | - | |
373 | 411 | | |
374 | 412 | | |
375 | 413 | | |
| |||
522 | 560 | | |
523 | 561 | | |
524 | 562 | | |
525 | | - | |
526 | | - | |
527 | | - | |
528 | | - | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
534 | | - | |
535 | | - | |
536 | | - | |
537 | | - | |
538 | | - | |
539 | | - | |
540 | | - | |
541 | | - | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
547 | 568 | | |
548 | 569 | | |
549 | 570 | | |
| |||
577 | 598 | | |
578 | 599 | | |
579 | 600 | | |
580 | | - | |
581 | 601 | | |
582 | 602 | | |
583 | 603 | | |
| |||
645 | 665 | | |
646 | 666 | | |
647 | 667 | | |
648 | | - | |
649 | | - | |
650 | | - | |
651 | | - | |
652 | | - | |
653 | | - | |
| 668 | + | |
654 | 669 | | |
655 | 670 | | |
656 | 671 | | |
657 | | - | |
658 | | - | |
| 672 | + | |
| 673 | + | |
659 | 674 | | |
660 | 675 | | |
661 | 676 | | |
| |||
670 | 685 | | |
671 | 686 | | |
672 | 687 | | |
673 | | - | |
674 | | - | |
675 | | - | |
676 | | - | |
677 | | - | |
678 | | - | |
679 | | - | |
680 | | - | |
681 | | - | |
682 | | - | |
683 | | - | |
| 688 | + | |
684 | 689 | | |
685 | 690 | | |
686 | 691 | | |
687 | 692 | | |
688 | 693 | | |
689 | | - | |
690 | | - | |
691 | | - | |
692 | | - | |
693 | | - | |
694 | | - | |
695 | | - | |
696 | | - | |
697 | | - | |
698 | | - | |
699 | | - | |
700 | | - | |
701 | | - | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
706 | | - | |
| 694 | + | |
| 695 | + | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
| 704 | + | |
707 | 705 | | |
708 | 706 | | |
709 | 707 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
| |||
188 | 190 | | |
189 | 191 | | |
190 | 192 | | |
191 | | - | |
192 | | - | |
193 | | - | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
194 | 197 | | |
195 | 198 | | |
196 | 199 | | |
| |||
450 | 453 | | |
451 | 454 | | |
452 | 455 | | |
453 | | - | |
454 | | - | |
455 | | - | |
456 | | - | |
457 | | - | |
458 | | - | |
459 | | - | |
460 | | - | |
461 | | - | |
462 | | - | |
463 | | - | |
464 | | - | |
465 | | - | |
466 | | - | |
467 | | - | |
468 | | - | |
469 | | - | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
470 | 463 | | |
471 | 464 | | |
472 | 465 | | |
| |||
504 | 497 | | |
505 | 498 | | |
506 | 499 | | |
507 | | - | |
508 | 500 | | |
509 | 501 | | |
510 | 502 | | |
| |||
587 | 579 | | |
588 | 580 | | |
589 | 581 | | |
590 | | - | |
591 | | - | |
592 | | - | |
| 582 | + | |
593 | 583 | | |
594 | 584 | | |
595 | 585 | | |
596 | 586 | | |
597 | | - | |
598 | | - | |
599 | | - | |
| 587 | + | |
600 | 588 | | |
601 | 589 | | |
602 | 590 | | |
| |||
609 | 597 | | |
610 | 598 | | |
611 | 599 | | |
612 | | - | |
613 | | - | |
614 | | - | |
615 | | - | |
616 | | - | |
617 | | - | |
618 | 600 | | |
619 | 601 | | |
620 | 602 | | |
| |||
683 | 665 | | |
684 | 666 | | |
685 | 667 | | |
686 | | - | |
687 | | - | |
688 | | - | |
| 668 | + | |
689 | 669 | | |
690 | 670 | | |
691 | 671 | | |
692 | | - | |
693 | | - | |
694 | | - | |
| 672 | + | |
695 | 673 | | |
696 | 674 | | |
697 | 675 | | |
698 | 676 | | |
699 | 677 | | |
700 | 678 | | |
701 | 679 | | |
702 | | - | |
703 | | - | |
704 | | - | |
705 | | - | |
706 | | - | |
707 | | - | |
708 | 680 | | |
709 | 681 | | |
710 | 682 | | |
| |||
0 commit comments