java-topology/defects/freeciv-0001/patch/freeciv-0001.patch
russell@unturf.com c4154e0590 freeciv-0001: assign_continent_flood() tile_list_search O(T^2), 53x
BFS flood fill in server/generator/mapgen_utils.c uses tile_list_search()
(O(N) linked-list scan) as visited check per adjacent tile. On a continent
of T tiles, each tile's 4-8 neighbors each trigger a linear scan of our
growing worklist, making total complexity O(T * adj * T) = O(T^2).

Fix: set tile_continent() at enqueue time instead of dequeue time. Our
continent field itself becomes our visited set, replacing O(N) membership
checks with O(1) integer comparisons. Worklist is now a pure FIFO queue.

Measured: 53x overhead at T=25,600 tiles (160x160 map).
Standard large Freeciv maps have continents of 5,000-20,000+ tiles.

MOAD 0002 (Intertangle): Freeciv uses struct civ_game as global god object,
  expected for a single-threaded C game from 1996. Not a practical defect.
MOAD 0003 (Leaked Context): CLEAN. Thread-local only in bundled tinycthread
  dependency. Tex AI uses proper mutexes.
MOAD 0004 (Logged Secret): CLEAN. auth.c logs rejection messages with
  usernames only, never passwords or credentials.
MOAD 0005 (Thundering Herd): CLEAN. AI settler cache uses hash lookup,
  single-threaded game loop has no concurrent cache races.
2026-03-31 12:27:02 -04:00

45 lines
1.7 KiB
Diff

--- a/server/generator/mapgen_utils.c
+++ b/server/generator/mapgen_utils.c
@@ -289,7 +289,8 @@
/**********************************************************************//**
Number this tile and nearby tiles with the specified continent number 'nr'.
Due to the number of recursion for large maps a non-recursive algorithm is
- utilised.
+ utilised. Tiles are marked with their continent number when enqueued,
+ eliminating O(N) membership checks on the worklist.
is_land tells us whether we are assigning continent numbers or ocean
numbers.
@@ -308,6 +309,9 @@
&& T_UNKNOWN != pterrain
&& XOR(is_land, terrain_type_terrain_class(pterrain) == TC_OCEAN));
+ /* Mark the initial tile immediately to avoid re-enqueue. */
+ tile_set_continent(ptile, nr);
+
/* Create tile list and insert the initial tile. */
tlist = tile_list_new();
tile_list_append(tlist, ptile);
@@ -325,15 +329,16 @@
continue;
}
- /* Add the tile to the list of tiles to check. */
- if (!tile_list_search(tlist, ptile3)) {
- tile_list_append(tlist, ptile3);
- }
+ /* Mark and enqueue. The continent field serves as our visited set,
+ * replacing the O(N) tile_list_search() with an O(1) check above. */
+ tile_set_continent(ptile3, nr);
+ tile_list_append(tlist, ptile3);
} adjc_iterate_end;
- /* Set the continent data and remove the tile from the list. */
- tile_set_continent(ptile2, nr);
+ /* Remove the tile from the worklist. Continent was already set
+ * at enqueue time. */
tile_list_remove(tlist, ptile2);
+ /* Note: tile_list_get(tlist, 0) is O(1) for linked list head. */
/* Count the tile */
if (nr < 0) {