Sure, here's how I got there, and how I think it could map onto your setup. One thing first: I didn't make a single match faster. One match still takes about 1.7 seconds of pure engine work on my PC. The ~7 matches per second comes from cutting everything around the match and running 16 matches at the same time.@nyutso - quick question. How did you speed up the match engine to run 7 matches per second?
WHAT I DID
1. I measured first. At the start, a lot of time per match went into setup, not the match itself: starting a process, loading the engine, preparing the match. Once I fixed that, about 96% of the time was the engine itself, which is where you want to be.
2. Load once, play many. This was the biggest win, from about 3.4 to about 7.5 matches per second. Instead of a new process for every match, each worker loads the engine and prepares the match once. Before each new match it puts back only the memory that changed during the previous one, so every match starts from exactly the same state.
3. Removed slow detours. A few game functions are handled by my own code. One of them was called around 14,000 times per match. Rewriting it as a tiny piece of machine code gave about another 13%.
4. 16 workers in parallel. I tested different worker counts and kept the one where adding more stopped helping. Pinning workers to specific CPU cores actually made it slower, so I don't. Each worker is replaced after 400 matches so memory doesn't build up, and a crashed or stuck match only kills its own worker, not the whole run.
5. After every change, I checked that the results were exactly the same: same seed, same match, bit for bit. A faster lab that gives different matches is worthless.
HOW I THINK YOU COULD DO IT
From your repo, it looks like each match gets its own host process and you run up to 4 at a time. If that's right, most of your gain is probably in step 2.
- Measure first: how much of one match is process start, loading the engine bundle, preparing the saved match, and how much is the actual match? If setup is a big share, that's your target.
- On macOS you might have an easier route than mine: fork(). Let the host do all the setup up to just before kick-off, then fork a child for each match. The child applies the edits and the seed, plays the match, writes the result and exits. Copy-on-write gives every match the same clean starting state for free. You'd need to check that it behaves under Rosetta and that the engine doesn't rely on threads that fork won't copy. If fork doesn't work, you can do what I did: save the original bytes of the memory the match changes and write them back before each match.
- Look at what your host does during the match itself. Anything called thousands of times per match (callbacks, logging, reading engine memory through system calls) is worth measuring.
- Then try more than 4 workers and stop when it stops scaling. Your final number will depend on your Mac's cores and on Rosetta, so it may not be exactly 7 per second, but the goal is the same: nearly all of each worker's time should be spent in the engine.
- Build a determinism check before you start (a fixed set of matches and seeds whose output must stay identical) and run it after every change.
Happy to compare notes if you try it.