Commit 41f3ee7
authored
Improve time complexity of get_component_mut (#22572)
# Objective
- Addresses #22483.
## Solution
Add a bloom filter to `has_conflicts` as a pre-check to see if we need
to check a given access against every other access or not. If the access
doesn't hit any component or resource a previous access does, we know we
don't need to check it element-by-element. Well-formed calls to
get_components_mut should then be linear in time taken to check for
conflicts, as they would always pass the pre-check.
The filter used is exported in the `bevy_utils` API as it is not
specific to components or resources.
## Testing
Tested via `cargo test`, where the get_component_mut tests pass or panic
as expected.
The get_component_mut benchmarks show >=20% improvements at as little as
5 components compared to the existing fallback for smaller sets. The
larger, 32 component benchmark added shows massive improvements. In
general, the new pre-check filter means we're only about 4-5x slower
than not checking at all.
```
ecs::world::world_get::world_query_get_components_mut/10_components_50000_entities
time: [6.5252 ms 6.5314 ms 6.5388 ms]
change: [−34.225% −33.954% −33.747%] (p = 0.00 < 0.05)
Performance has improved.
[...]
ecs::world::world_get::world_query_get_components_mut/32_components
time: [21.608 ms 21.696 ms 21.828 ms]
change: [−92.284% −92.236% −92.186%] (p = 0.00 < 0.05)
Performance has improved.
```
| bench | mean (prev, with fallback threshold) | mean (post, always
complex) | delta |
|-|-|-|-|
| 2_components | 770.73 us | 925.90 us | +19.62% |
| unchecked_2_components | 288.66 us | 288.71 us | -0.84% |
| 5_components | 2.960 ms | 2.286 ms | -22.77% |
| unchecked_5_components | 505.45 us | 521.56 us | +2.14% |
| 10_components | 9.889 ms | 6.531 ms | -33.95% |
| unchecked_10_components | 1.452 ms | 1.538 ms | +5.48% |
| 32_components | 279.44 ms | 21.696 ms | -92.24% |
| unchecked_32_components | 4.460 ms | 4.458 ms | -0.05%1 parent 543b305 commit 41f3ee7
File tree
6 files changed
+485
-51
lines changed- benches/benches/bevy_ecs/world
- crates
- bevy_ecs/src/query
- bevy_utils/src
6 files changed
+485
-51
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
39 | 39 | | |
40 | 40 | | |
41 | 41 | | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
420 | 420 | | |
421 | 421 | | |
422 | 422 | | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | | - | |
| 46 | + | |
47 | 47 | | |
48 | 48 | | |
49 | 49 | | |
| |||
0 commit comments