-
-
Notifications
You must be signed in to change notification settings - Fork 942
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Switch from forEach to regular for-loops for about 30% improvem…
…ent in raw update performance (#3472) Replaces uses of `.forEach()` with regular `for` loops. This has significant impact on performance in hot paths such as `Component.updateTree()` and `Component.renderTree()`. ![Updating Components](https://github.com/user-attachments/assets/e183ce4c-5b37-45d9-ad81-a0a35719e0dd) In the graph above, you see 50 runs of `benchmark/update_components_benchmark.dart`. The forEach results are blue, the for-loop results are green. I could see this effect after just replacing the `forEach` calls in `component.dart`. Data [here](https://docs.google.com/spreadsheets/d/e/2PACX-1vRk_yGmLN6o0oqSUWDBh7ODx7B8EIToeahZcZBS3VKHX8AbEnmrgmEqDt98cZLoBjIKQX3MlOc0XwsP/pubhtml). > Aside for posterity: `for i in {1..50}; do flutter test benchmark/main.dart --no-pub -r silent 2>/dev/null >> benchmarks_for_loop.txt; done`, then get the data from the text file. I went ahead and replaced additional `forEach` calls elsewhere in the engine codebase, but there was no additional effect on the benchmark. Still, I kept those changes in. I only replaced `forEach` in places that seemed relatively hot (e.g. `ComponentSet.reorder()`). There are more `forEach` calls in the codebase but those seem fine to me as they aren't likely to be called too often. It should be noted that I needed to update the benchmark to add children to the components. Every `_BenchmarkComponent` now has 10 children. This feels a bit more realistic use of the framework than having a flat array of components with no children. By changing the benchmark code in this way, I made it a bit slower, so I'm not sure if the effect will be seen in the CI/CD. I also tried whether the change will have effect on my game's benchmark (which is a lot more involved and uses `flutter driver` to test the whole game in AOT mode). For the game, the effect is negligible but that was kind of expected since my game spends a significant amount of its CPU time on AI, raycasting, smoke simulation and drawVertices, none of which really depend on the speed of the engine `update()` mechanism. --------- Co-authored-by: Erick <[email protected]>
- Loading branch information
1 parent
c3f9925
commit 9891f4e
Showing
17 changed files
with
91 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters