A Java browser game saw garbage collection drop after some streams were replaced with loops. Should I avoid streams in game code, or is this something to measure case by case?
Are Java streams a problem for a browser game compiled to WebAssembly?
Answers
Measure it case by case. Stream pipelines can create short-lived objects, and a pipeline running for every entity on every frame can put more pressure on the garbage collector than the same work in a loop.
Start with allocation profiling around the update and rendering paths. Compare the stream version with a loop under the same workload, looking at frame times and garbage collection as well as raw throughput. A stream used once during setup is a different concern from one used thousands of times per second.
Reply to this answer
I wouldn't ban streams across the project. They can make transformations easier to read, and a few temporary objects in infrequently used code may not matter. Use a loop where measurements show allocation is hurting, not just because the code contains a stream.
Reply to this answer
If you're still choosing tools, TeaVM is worth a look for compiling Java to WebAssembly. libGDX is another game development option to investigate if you want to target desktop and mobile as well as the web. Check the current build options and compatibility before picking a stack.
Reply to this answer
Allocation isn't the only game issue to watch. If movement feels blocky, the spatial grid and movement logic may deserve attention too. A smoother approach that creates a pile of temporary objects every frame could bring the GC problem right back, so measure both behavior and allocations as you change it.
Reply to this answer
Add your answer
Answers are reviewed before publication.
_x29+ferrulebadge.3:
Also test the browser build itself. Results from a desktop JVM don't necessarily tell you what the WebAssembly build will do.