Skip to content

Conversation

xsebek
Copy link
Member

@xsebek xsebek commented Jul 30, 2025

Tested with

cabal run swarm -- --debug=all --scenario Creative +RTS -s -N10  # try setting number of threads

@byorgey
Copy link
Member

byorgey commented Jul 31, 2025

This is cool, and it does seem to make world scrolling a bit smoother. In particular, I used to notice a big stutter every 64 steps as it loaded new tiles; I don't notice that any more.

However, I do not consider this to close #2529. Even with this PR I still notice some small stuttering every 4 steps as it has to render a bunch of new view chunks. At the very least we should try using parMap in a similar way on the newly rendered view chunks (although that is probably a bit trickier since which ones actually get rendered is mediated by the brick widget cache). And even if we do both of those things, I am still not convinced that it will result in scrolling as smooth as it could possibly be. We would still be waiting until the last possible second to do all the work; spreading out the work among multiple cores will definitely help, but it would be even better to spread out the work in time as well. The idea behind #2529 is to speculatively pre-load and pre-render world tiles and view chunks in a separate thread, so that we can spread out the work of loading and rendering over time.

@xsebek
Copy link
Member Author

xsebek commented Aug 24, 2025

  • Add metrics

I tried to add metrics and actually measure how long does it take us to load tiles. There were a couple of blockers, which made this task bigger than I initially expected:

  • I wanted to try the fused-effects and create a new effect for metrics, so that I don't just spread IO everywhere. I got stuck on the zoom functions and it took me while to figure out the long errors.
  • Some operations used the pure functions and were not caching tiles. Adding effects was not trivial and once I gave up and used IO for one function (mtlEntityAt), suddenly half of step functions needed IO transitively.
    • I also wrote "mock effects" for tests, so the tests and benchmarks don't need IO if they are not using metrics.
  • I was getting lost at which pure world functions were missing stateful caching counterparts, so I decided to split the module to World folder and re-export. But there it got mixed up with world DSL. So I moved that into its own folder as well.
  • The world tiles cache is lazy. The world state also seemed lazy?! It took me a while to force the computation and get believable metrics.

@byorgey I will split out smaller PRs from this one and get it back to one file change, don't worry. 😄

mergify bot pushed a commit that referenced this pull request Aug 26, 2025
* move World DSL to `Swarm.Game.World.DSL`
* re-export from `swarm-scenario/Swarm/Game/World/DSL.hs`
* split off from #2555
mergify bot pushed a commit that referenced this pull request Aug 27, 2025
* split World into smaller modules and re-export
* also re-export `Coords` for convenience
* split from #2555 
* depends on #2567 

The idea is to have Tile and Function modules separate from World, instead of having to scroll past them.

Then World functions get split into a Pure interface and a monadic/stateful one that should match (some are missing) and be preferred as it will implicitly use cache (it is currently not very used). 

In the next PR, the monadic interface will be improved with metrics...
mergify bot pushed a commit that referenced this pull request Sep 22, 2025
* add metric effect to lookup and update functions
* replace MTL style entity lookup with fused-effects version
  * add fake time effect for tests
* refactor the robot metrics to use the new metric fused-effect
* refactor copy-paste zoom* functions to call one function that accepts a lens
* split from #2555

This is the last part that I wanted to split off from #2555. I rewrote most of it and I think I arrived at a cleaner solution for the MTL function and the signatures of Step functions.
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.

Preload world tiles and predraw view chunks in a separate thread

2 participants