FM27 Match Lab (macOS): Tool to simulate matches outside the game

@nyutso - quick question. How did you speed up the match engine to run 7 matches per second?
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.

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.
 
Thanks that is super helpful. I'm in the middle of trying to decompile the ME code so I can understand better how it works and why some attributes don't matter even if they are called hundreds of thousands of times during a match. So that is where all my credits are going, but I will try you suggestions and report once I get my weekly reset.
 
I'm in the middle of trying to decompile the ME code so I can understand better how it works and why some attributes don't matter even if they are called hundreds of thousands of times during a match
Are you decompiling in the sense that you're asking Claude to trace all the functions and then asking "what's going on here then?" or converting machine code back in to source code byte for byte? If it's the latter then you could make a public Git repository and spread the workload to other volunteers.
 
I used Ghidra to learn about all the functions and I'm doing some tests to verify the pseudo code from its output. I don't think the goal is to have it byte by byte decompiled (it would take a lot of effort), but to get a good understanding of what is going on under the hood, since i'm curious about it.

One possible output, and something we can already do is do some small changes to the ME. Like, we can change the dice rolls and how some attributes enter different calculations. Maybe we can create a working group on discord if there are more people interested in doing this?
 
I used Ghidra to learn about all the functions and I'm doing some tests to verify the pseudo code from its output. I don't think the goal is to have it byte by byte decompiled (it would take a lot of effort), but to get a good understanding of what is going on under the hood, since i'm curious about it.
Well it's trivial for an LLM, especially with Ghidra, but very time consuming by hand. I've got Claude doing Star Wars: Knight of the Old Republic in the background and it's absolutely crushing it. If I buy tFM 27 then I might do just that but for now I've got to spectate 🙃
 
Back
Top