Room for a million
A street in one of the game’s towns, drawn by its 3D client with its own buildings, cars and people. The traffic and the crowds were placed for this picture: the town doesn’t fill a street like this yet. Making room for that many people, a million of them, is what this page is about. It is rewritten as each step lands, so it always tells the story up to now.
The town I want to watch is more lived in than the one the game simulates today. People go into cafés and pubs, and the visit is the sale. They sit on the terrace when the sun is out and indoors when it rains. They buy a haircut that wears off over a few weeks, and a drink after work with colleagues or friends. Cars stop for fuel. Crime is something you can see: a group loitering on a corner, not a number on a chart.
All of that is cheap for a town of two thousand and very expensive for a town of a million, which is the size the game is meant to reach. This page follows the engine from where it stood on 4 November, through each step that has landed, to where it is today.
Where we started
On 4 November, simulating one day of a million-resident town took about 120 seconds on a quiet sixteen-core machine, and twice that when the machine was busy. Midnight was the worst moment: one step held the clock for 59 seconds while everyone planned their day. At its peak the town needed 52 GB of memory. The game’s fastest speed allows 120 seconds for a day, so the town only just kept up, and with a visible stall every night.
Three things took nearly all of that time:
- Routing every car. Each car trip worked out its own route through the streets and was then timed street by street: millions of route searches a day.
- Shopping in one pass. Every household’s shopping for the day was settled together, with each shopper’s choice sorting every shop in town by distance.
- Planning at midnight. Everyone planned their whole day at 00:00, all at once.
Where one simulated day’s work went on 4 November, as shares of the whole. Routing alone was three fifths of it.
None of this was a mistake. The engine had been shaped for a town of two thousand and sped up one slow spot at a time. Adding cafés, pubs and fuel stops on top of it would only make it slower, so the shape had to change first.
Where we are heading
Nothing here is exotic. The common thread is that nobody settles everyone at once.
- People plan when they wake, spread over the morning, not all at midnight.
- Needs are worked out when someone looks at them. A person’s hunger, or their need for a haircut, is a level and the time it was last reset. Nothing ticks it down every minute for a million people.
- Shops restock when they run low, and prices and wages change on staggered days instead of for everyone on the same day.
- The day is cut into five-minute slots, and each slot is shared across every processor core. The answer is the same on any number of cores.
- A visit is the sale. Someone arrives at a shop, a café or a pub, takes a seat or an item, pays and leaves: first come, first served. There is no separate nightly pass that matches shoppers to shops.
- Roads are counted as traffic between neighbourhoods, not as a route for every person. Trip times and the number of cars on each road come out of one sweep per neighbourhood. Exact paths are worked out only for the cars you are looking at.
The first idea shows the pattern behind all of them. Planning costs the same in total whenever it happens, but done all at midnight it stops the clock:
When a million people plan their day: plans per five-minute slot over a day, from the residents’ prototype. Spreading the same work over the morning removes the stall.
Put together, one five-minute slot of the finished engine works like this:
One five-minute slot in the finished engine. Each part runs across all processor cores and the results are merged in a fixed order, which is why the town comes out the same on one core or sixteen.
Before changing the game, I had two throwaway prototypes written to test whether this shape holds up.
- The residents’ prototype runs a whole lived-in day: homes, workplaces with shifts, grocers, shops, barbers, cafés with terraces, restaurants, pubs, gyms, fuel stations and parks, cars with tanks, friends going out together, the weather, loitering and offences, and shops restocking and paying wages. For a million residents that is about 6.9 million events a day. It takes 0.4 to 0.5 seconds on eight cores, ends in exactly the same state on 1 to 16 cores, and balances money and goods at the end of every day.
- The roads prototype takes 3 million car trips over a network the size of a real million-resident city. It works out the day’s traffic and travel times in 1.2 to 3.6 seconds on eight cores, against about 73 seconds to route each trip separately.
Together that is roughly 1.5 to 4 seconds a day in about 2 GB of memory. That is the destination: not a promise, because the prototypes leave out much of what the real game does (see the limits at the end), but a sign that the shape fits with room to spare.
Memory for a million residents. A resident’s live state in the prototype is about 200 bytes, which is where most of the saving comes from.
The road so far
The work lands in small steps. Each step is checked against the town’s recorded results before it goes in, and so far none of them has changed what the town does, only how the work is done.
| Step | Landed | What it did |
|---|---|---|
| 1. Nearest few first | 4 Nov | Shop choice and job matching sort only the nearest few options |
| 2. Compact tables | 5 Nov | People, households and the town’s books in compact tables instead of maps |
| 3. Five-minute slots | 6 Nov | The clock knows about slots, and a slot’s work can be shared across cores safely |
| 4. Roads in the background | 8 Nov | The new way of counting traffic runs beside today’s routing and is compared every day |
Here is what a million-resident day took after each of them:
One simulated day of a million residents on sixteen cores, after each step. The orange bars are the game; the green sliver is the prototypes’ shape that the game is moving towards. Every run shared a workstation, so differences of a few seconds are noise.
The first three steps took the day from about 120 seconds to about 70. The fourth bought no speed: it measures how well the next step’s biggest saving will work before it is made.
1. Nearest few first
When a household chose a shop, the game sorted every shop in town by distance, then kept the nearest eight. At a million residents that sort ran millions of times a day. Now it picks out the nearest few and sorts only those, which gives exactly the same answer. A day’s shopping went from 20 to 6 seconds, and building the town from 86 to 50 seconds, because job matching had the same habit.
Choosing a shop, before and after: the square is a household and the dots are shops. The same eight shops come out in the same order, for a fraction of the work.
2. Compact tables
People, households and the town’s books moved out of lookup maps into compact tables: one row per person, one column per fact, kept in a fixed order. Each payment’s bookkeeping became seven times cheaper, and a day got about a quarter faster. Every saved game and every recorded result stayed exactly the same, down to the byte.
How the game keeps a million people. A processor reads a table row after row at full speed; following links from node to node means waiting on memory at every step.
3. Five-minute slots
A day is now 288 slots of five minutes (the length is a setting). Something the town schedules can ask to be handled with everything else due in its slot: it runs at the start of that slot and keeps its exact time, so a shopper who arrives at 10:03 is still recorded as arriving at 10:03. Something scheduled for a slot that has already started waits for the next one.
The hard part of using every core is making sure two cores never change the same thing at once, and that the answer comes out the same on a laptop with four cores and a desktop with sixteen. The rule is simple:
- each household is worked on by one core at a time, and while it works it can change only its own household’s rows;
- it reads everything else (the shops, the roads, the neighbours) as it stood when the slot began;
- anything that touches someone else, such as paying a shop or telling a friend about a plan, is written down as a note, and the notes are applied afterwards, one after another, always in the same order.
How a slot’s work is shared. Each core changes only its own households; everything that reaches beyond them waits as a note and is applied in a fixed order afterwards.
The programming language checks the first rule for us: code that tried to change another household’s rows would not compile. A test town ran two days of visits and payments on one core and on four, in different sized batches and once from a saved game, and every run ended in exactly the same state. Going over every household of a million-resident town and doing nothing costs about 0.4 thousandths of a second, so the machinery is cheap. Nothing in the game uses the slots yet: trips move onto them in one piece, two steps from now.
4. Roads in the background
The biggest single cost is routing every car, so the next step replaces it with traffic counted between neighbourhoods:
The change the roads step is preparing. The work no longer grows with the number of cars, only with the number of neighbourhoods.
Before making that switch, the new count now runs beside today’s routing on the game’s real towns: each night at midnight it counts the day just gone, and the two are compared street by street. Nothing in the town uses the new counts yet, and the town’s recorded results show that watching changed nothing.
On the million-resident town (about 800,000 car trips a day), main roads come out within about 2% of routing every car, smaller roads within about 7% and quiet residential streets within about 12%:
The new count against routing every car, street by street, on the million-resident town. The orange bars are the idea that was dropped (below).
Added up over every road, the distance driven in a day is within 0.2% of routing every car. Trip times come out about 2% shorter than the routes’ own times, and about 6% shorter than the timed trips the game records today, most of it because today’s timing rounds each street up to a whole second. The count costs under a second a day, against about 45 seconds a day for the routing it is meant to replace.
Three things came out of it:
- The whole difference is where trips start and end. Trips begin and end at their own front doors, while the new count sends them from the middle of their neighbourhood. On the towns I checked, ties between equally quick routes and drivers’ remembered routes explained none of it.
- Sending each trip’s first and last stretch through the middle of its neighbourhood made quiet streets worse. That was the plan for giving quiet streets their traffic, but it put about a fifth more driving on them and the difference rose from about 12% to about 19%. So it won’t be used as it stands, and the next step chooses another way.
- Congestion needs traffic counted by when cars enter a street, not when they set off. Counting by departure piled each trip into the hour it left and gave about 18% more time stuck in traffic than the game’s own figures. The next step counts by entry.
Where the time goes now
A million-resident day still takes about 70 seconds, and the longest single step, around midnight, still holds the clock for 20 to 30 seconds. This is how the work divides up today, and which coming step takes each part away:
Where one simulated day’s work goes now (measured on 8 November), and the step that takes each part away. Set beside the chart of 4 November, shopping has shrunk, and routing is still three fifths of the day. That is the routing step 4 has just measured a replacement for, which is why the roads come next.
What comes next
Nearest few first.Done.People, households and the books in compact tables.Done.Five-minute slots and the rules for what each step may read and write.Done.Roads counted in the background beside today’s routing, and the two compared on real towns.Done.- Roads switch over to travel times between neighbourhoods and counted traffic. This step also chooses how quiet streets get their traffic, and counts traffic by when cars enter a street.
- Trips and plans move onto the slots, which removes the midnight stall.
- Needs and mood are worked out when looked at.
- Shopping settles at the visit.
- Job search by neighbourhood.
- Then the lived-in features themselves, one at a time: venues with seats and terraces, services that meet needs, the pub after work, fuel, weather and crime.
From step 5 on, the steps change what the town does, not only how fast: trips get about 6% shorter, shops sell first come first served, and people plan when they wake. Each of those has been agreed, and each will have the town’s reference results recorded again when it lands.
Honest limits
- The prototypes are not the game, and their numbers are a lower bound. They leave out costs the real game pays: saving the town, the full checks, the event log, the data the clients draw from, and the real economy with its many goods and firms (the prototype trades five goods with one outside region). They have no children or schools, and they ran on streets built for the test.
- Quiet streets are the roads’ weak spot. On the million-resident town they are within about 12%, but on a town of 20,000 they are about 31% off. On the smallest towns, with a hundred or so car trips a day, a single street’s count is too small to compare at all. The next step has to choose how quiet streets get their traffic.
- Drawing the cars you can see needs exact paths. The plan is individual cars near the camera and flowing dots at the real density further out. That isn’t measured yet.
- Every run shared a workstation, so the numbers are not clean, and differences of a few seconds are noise. I have not measured a Steam Deck.
- The prototype is not calibrated. Its people behave sensibly (about one outing a week per worker, haircuts every four weeks, most terrace seats taken in fine weather and none in rain), but nothing is tuned to a real town.
