Skip to content

Conversation

ApoloApps
Copy link

Add memoryOptimizedForEach and memoryOptimizedForEachIndexed to avoid allocating an Iterator (index-based for loops, not default iterator for loop) and apply them to Wasm Backend for better performance overall. This could also be done for intermediate operations like map, mapIndexed, and others but this is only a little change focused on improving memory consumption in Wasm Backend operations and compilation

… allocating an Iterator (index-based for loops) and apply them to Wasm Backend for better performance overall
@ApoloApps ApoloApps requested review from a team as code owners September 10, 2025 23:06
@ApoloApps ApoloApps requested review from eymar and bnorm September 10, 2025 23:06
@ApoloApps ApoloApps changed the title Avoid Iterator allocation in Wasm Backends () Avoid Iterator allocation in Wasm Backend operations Sep 10, 2025
@JakeWharton
Copy link
Contributor

JakeWharton commented Sep 11, 2025

I would prefer this to be named based on how it behaves, not a relationship to the "normal" forEach.

If you run this new function on non-random access-based list, you actually thrash the cache lines of the CPU making it very much not memory optimized. Heap allocation and virtual dispatch counts are not the only memory that needs considered.

I tend to call these indexBasedForEach which describes the implementation, and hopefully makes it more obvious that it's a Bad Idea™ to use on something like a linked list.

@ApoloApps
Copy link
Author

i tried to mantain consistent naming so thats why i changed to mention the index access in the kdoc. renaming it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants