Project

General

Profile

Actions

Feature #2502

open

Fastening map generation

Added by Paweł Nowak 5 days ago. Updated about 15 hours ago.

Status:
New
Priority:
Normal
Assignee:
-
Category:
Server
Target version:
Start date:
11/23/2026
Due date:
% Done:

0%

Estimated time:

Description

Generating a very large map takes a long time. On a map of 2,000,000
tiles with settings: (generator FRACTAL, mapsize FULLSIZE, size 2000,
other settings default) it took about 9 minutes on Debian on Intel(R) Core(TM) i5-10300H CPU @ 2.50GHz.

A perf profile of freeciv-server (build from git, -O2 -g) showed two
hot spots:

1. make_land(), with make_rivers() inlined, about 60% of the samples.
For every river attempt make_rivers() scans the whole map several
times (to build the blocked tile set from other road-like extras,
and again to apply the river). The number of rivers grows with the
map size, so the cost is about quadratic.
2. genlist_search(), about 25-30% of the samples. It is called from
assign_continent_flood() (mapgen_utils.c), which calls
tile_list_search() on its queue for every neighbouring tile.

The attached patch fix both without changing the result:

  • fasten_map_gen.patch

With it applied, generating the same 2,000,000 tile map takes
under 1 minute on the same computer.

Testing: built from git commit https://github.com/FuriousFourier/freeciv/commit/766e48ced7cd9b0c03585cc6c423b206a39b7852. I
generated maps with the same mapseed and gameseed before and after the
patches and compared the saved games. On a 2,000,000 tile map (and on
a smaller map up to 20,000) the map sections (terrain, extras, altitude,
start positions) are identical. The patch doesn't change the order of
random number calls: pick_terrain_by_flag() still gets river tiles in
ascending tile index order.


Files

fasten_map_gen.patch (9.95 KB) fasten_map_gen.patch Paweł Nowak, 11/23/2026 05:09 PM
0001-Fasten-map-generation-v2.patch (10.7 KB) 0001-Fasten-map-generation-v2.patch Paweł Nowak, 11/27/2026 10:39 PM
Actions #2

Updated by Paweł Nowak 5 days ago

The file which size is 0B is added by mistake. I can't remove it. Just ignore it please

Actions #3

Updated by Dean Brown 5 days ago

  • File deleted (fasten_map_gen.patch)
Actions #4

Updated by Dean Brown 5 days ago

I was able to delete that 0 byte file.

Actions #5

Updated by Sweet Tea Dorminy 5 days ago

Thanks for this. Very large maps are painful today, and the profile makes the case clearly.

Empirically it looks fine, on a wide variety of scenarios. For me, your 2,000,000-tile FRACTAL case went from 1297 s to 20 s here (a busy machine, one run each), with the same map.

A small helper for the number-and-count lines, which now appear twice, would let the comment state the invariant rather than describe the old search?

For the rivers, I wonder whether the road_tiles[] cache is needed at all. The blocked set is read only in river_test_blocked(), and the map does not change while make_river() runs, so "has another road-like extra" can be tested on the tile itself against a per-attempt mask:

static bool river_is_blocked(const struct river_map *privermap,
const struct tile *ptile) {
return dbv_isset(&privermap->blocked, tile_index(ptile)) || BV_CHECK_MASK(*tile_extras(ptile), privermap->blocking_extras);
}

with a bv_extras blocking_extras in struct river_map, filled from extra_type_by_cause_iterate(EC_ROAD) before each attempt. Then river_test_blocked() could call this in place of its two dbv_isset() calls. That drops road_tiles, road_tiles_used, road_tiles_nonempty, the OR loop into struct dbv's internals, and the need to keep the cache in step with tile_add_extra() -- I'd guess like a couple percent more speedup?

One small thing: you have a tiny .gitignore change that snuck in ;)

Actions #6

Updated by Paweł Nowak about 22 hours ago

Thanks for the review, both suggestions made sense and are in the
updated patch (attached, replacing the earlier one):

  • assign_continent_tile() now holds the number-and-count lines, and the
    comment states the invariant (a tile is numbered when queued, so a
    numbered tile is done or already waiting).
  • You're right that 'blocked' is only read in river_test_blocked() and
    the map doesn't change while make_river() runs. The road_tiles[]
    cache and its bookkeeping are gone; river_is_blocked() checks the
    tile's extras against a per-attempt bv_extras mask.

Testing: with the same mapseed and gameseed, saved maps from unpatched
and patched servers are identical. The acceleration is still big.

Actions #7

Updated by Marko Lindqvist about 19 hours ago

  • Target version set to 3.4.0
Actions #8

Updated by Sweet Tea Dorminy about 15 hours ago

Love it.

- One line is over 80 columns, in river_ok_set():
privermap->ok_alloc = privermap->ok_alloc > 0 ? privermap->ok_alloc * 2 : 256;

- Optional: rivermap.ok is now read only by river_ok_set(), to keep duplicates out of ok_list. As far as I can see a river never revisits a tile (make_river() blockmarks each tile it leaves, and the next tile must pass river_test_blocked()), so ok_list alone would do, or an fc_assert() there would document that the early return never fires.

If I were doing it, personally, I'd probably make this two changes, one floodfill improvement and one rivers. But that's a taste thing and plenty of people would disagree with me :).

Reviewed-by: Sweet Tea Dorminy <>

Actions

Also available in: Atom PDF