diff --git a/.gitignore b/.gitignore
index 93d1646b69..099a89d9ba 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,3 +13,4 @@
 /Makefile.in
 /configure
 /freeciv-CI.log
+.idea
diff --git a/server/generator/mapgen.c b/server/generator/mapgen.c
index fa6483de7a..cec56bf4aa 100644
--- a/server/generator/mapgen.c
+++ b/server/generator/mapgen.c
@@ -115,6 +115,13 @@ static bool make_island(int islemass, int starters,
 struct river_map {
   struct dbv blocked;
   struct dbv ok;
+
+  /* Tiles marked in 'ok', each once, in the order they were marked.
+   * Lets us apply or clear a river by touching only its own tiles
+   * instead of scanning the whole map. */
+  int *ok_list;
+  int ok_count;
+  int ok_alloc;
 };
 
 static int river_test_blocked(struct river_map *privermap,
@@ -681,6 +688,50 @@ static void river_blockmark(struct river_map *privermap,
   } cardinal_adjc_iterate_end;
 }
 
+/**********************************************************************//**
+  Marks a tile as part of the river being made. Each tile is recorded
+  once in ok_list.
+**************************************************************************/
+static void river_ok_set(struct river_map *privermap, int tindex)
+{
+  if (dbv_isset(&privermap->ok, tindex)) {
+    return;
+  }
+
+  dbv_set(&privermap->ok, tindex);
+
+  if (privermap->ok_count >= privermap->ok_alloc) {
+    privermap->ok_alloc = privermap->ok_alloc > 0 ? privermap->ok_alloc * 2 : 256;
+    privermap->ok_list = fc_realloc(privermap->ok_list,
+                                    privermap->ok_alloc * sizeof(int));
+  }
+  privermap->ok_list[privermap->ok_count++] = tindex;
+}
+
+/**********************************************************************//**
+  Forgets the river tiles marked so far (clears only the bits we set).
+**************************************************************************/
+static void river_ok_clear(struct river_map *privermap)
+{
+  int i;
+
+  for (i = 0; i < privermap->ok_count; i++) {
+    dbv_clr(&privermap->ok, privermap->ok_list[i]);
+  }
+  privermap->ok_count = 0;
+}
+
+/**********************************************************************//**
+  qsort() comparison function for tile indices (ascending).
+**************************************************************************/
+static int river_index_cmp(const void *a, const void *b)
+{
+  int ia = *(const int *) a;
+  int ib = *(const int *) b;
+
+  return (ia > ib) - (ia < ib);
+}
+
 struct test_func {
   int (*func)(struct river_map *privermap, struct tile *ptile, struct extra_type *priver);
   bool fatal;
@@ -803,7 +854,7 @@ static bool make_river(struct river_map *privermap, struct tile *ptile,
 
   while (TRUE) {
     /* Mark the current tile as river. */
-    dbv_set(&privermap->ok, tile_index(ptile));
+    river_ok_set(privermap, tile_index(ptile));
     log_debug("The tile at (%d, %d) has been marked as river in river_map.",
               TILE_XY(ptile));
 
@@ -910,6 +961,13 @@ static void make_rivers(void)
   struct river_map rivermap;
   struct extra_type *road_river = nullptr;
 
+  /* For each road-like extra type (rivers included): which tiles have it.
+   * Kept up to date while rivers are added, so we don't have to scan the
+   * whole map for every new river. */
+  struct dbv road_tiles[MAX_EXTRA_TYPES];
+  bool road_tiles_used[MAX_EXTRA_TYPES];
+  bool road_tiles_nonempty[MAX_EXTRA_TYPES];
+
   /* Formula to make the river density similar om different sized maps. Avoids
      too few rivers on large maps and too many rivers on small maps. */
   int desirable_riverlength =
@@ -941,6 +999,24 @@ static void make_rivers(void)
 
   dbv_init(&rivermap.blocked, MAP_INDEX_SIZE);
   dbv_init(&rivermap.ok, MAP_INDEX_SIZE);
+  rivermap.ok_list = nullptr;
+  rivermap.ok_count = 0;
+  rivermap.ok_alloc = 0;
+
+  memset(road_tiles_used, 0, sizeof(road_tiles_used));
+  memset(road_tiles_nonempty, 0, sizeof(road_tiles_nonempty));
+  extra_type_by_cause_iterate(EC_ROAD, oroad) {
+    int oi = extra_index(oroad);
+
+    dbv_init(&road_tiles[oi], MAP_INDEX_SIZE);
+    road_tiles_used[oi] = TRUE;
+    whole_map_iterate(&(wld.map), rtile) {
+      if (tile_has_extra(rtile, oroad)) {
+        dbv_set(&road_tiles[oi], tile_index(rtile));
+        road_tiles_nonempty[oi] = TRUE;
+      }
+    } whole_map_iterate_end;
+  } extra_type_by_cause_iterate_end;
 
   /* The main loop in this function. */
   while (current_riverlength < desirable_riverlength
@@ -991,17 +1067,21 @@ static void make_rivers(void)
 
       /* Reset river map before making a new river. */
       dbv_clr_all(&rivermap.blocked);
-      dbv_clr_all(&rivermap.ok);
+      river_ok_clear(&rivermap);
 
       road_river = river_types[fc_rand(river_type_count)];
 
+      /* Tiles that have some other road-like extra are blocked. */
       extra_type_by_cause_iterate(EC_ROAD, oriver) {
-        if (oriver != road_river) {
-          whole_map_iterate(&(wld.map), rtile) {
-            if (tile_has_extra(rtile, oriver)) {
-              dbv_set(&rivermap.blocked, tile_index(rtile));
-            }
-          } whole_map_iterate_end;
+        int oi = extra_index(oriver);
+
+        if (oriver != road_river && road_tiles_nonempty[oi]) {
+          int nbytes = _BV_BYTES(dbv_bits(&rivermap.blocked));
+          int bi;
+
+          for (bi = 0; bi < nbytes; bi++) {
+            rivermap.blocked.vec[bi] |= road_tiles[oi].vec[bi];
+          }
         }
       } extra_type_by_cause_iterate_end;
 
@@ -1010,24 +1090,35 @@ static void make_rivers(void)
 
       /* Try to make a river. If it is OK, apply it to the map. */
       if (make_river(&rivermap, ptile, road_river)) {
-        whole_map_iterate(&(wld.map), ptile1) {
-          if (dbv_isset(&rivermap.ok, tile_index(ptile1))) {
-            struct terrain *river_terrain = tile_terrain(ptile1);
-
-            if (!terrain_has_flag(river_terrain, TER_CAN_HAVE_RIVER)) {
-              /* We have to change the terrain to put a river here. */
-              river_terrain = pick_terrain_by_flag(TER_CAN_HAVE_RIVER);
-              if (river_terrain != nullptr) {
-                tile_set_terrain(ptile1, river_terrain);
-              }
+        int rn;
+        int ri = extra_index(road_river);
+
+        /* Apply the river tiles in ascending tile index order. This is the
+         * same order as when scanning the whole map, which matters as
+         * pick_terrain_by_flag() consumes random numbers. */
+        qsort(rivermap.ok_list, rivermap.ok_count, sizeof(int),
+              river_index_cmp);
+
+        for (rn = 0; rn < rivermap.ok_count; rn++) {
+          struct tile *ptile1 = index_to_tile(&(wld.map),
+                                              rivermap.ok_list[rn]);
+          struct terrain *river_terrain = tile_terrain(ptile1);
+
+          if (!terrain_has_flag(river_terrain, TER_CAN_HAVE_RIVER)) {
+            /* We have to change the terrain to put a river here. */
+            river_terrain = pick_terrain_by_flag(TER_CAN_HAVE_RIVER);
+            if (river_terrain != nullptr) {
+              tile_set_terrain(ptile1, river_terrain);
             }
-
-            tile_add_extra(ptile1, road_river);
-            current_riverlength++;
-            map_set_placed(ptile1);
-            log_debug("Applied a river to (%d, %d).", TILE_XY(ptile1));
           }
-        } whole_map_iterate_end;
+
+          tile_add_extra(ptile1, road_river);
+          dbv_set(&road_tiles[ri], rivermap.ok_list[rn]);
+          road_tiles_nonempty[ri] = TRUE;
+          current_riverlength++;
+          map_set_placed(ptile1);
+          log_debug("Applied a river to (%d, %d).", TILE_XY(ptile1));
+        }
       } else {
         log_debug("mapgen.c: A river failed. It might have gotten stuck "
                   "in a helix.");
@@ -1041,6 +1132,14 @@ static void make_rivers(void)
 
   dbv_free(&rivermap.blocked);
   dbv_free(&rivermap.ok);
+  free(rivermap.ok_list);
+  extra_type_by_cause_iterate(EC_ROAD, oroad) {
+    int oi = extra_index(oroad);
+
+    if (road_tiles_used[oi]) {
+      dbv_free(&road_tiles[oi]);
+    }
+  } extra_type_by_cause_iterate_end;
 
   destroy_placed_map();
 }
diff --git a/server/generator/mapgen_utils.c b/server/generator/mapgen_utils.c
index 908208dfb4..4e8a181e6a 100644
--- a/server/generator/mapgen_utils.c
+++ b/server/generator/mapgen_utils.c
@@ -309,8 +309,18 @@ static void assign_continent_flood(struct tile *ptile, bool is_land, int nr)
                 && T_UNKNOWN != pterrain
                 && XOR(is_land, terrain_type_terrain_class(pterrain) == TC_OCEAN));
 
-  /* Create tile list and insert the initial tile. */
+  /* Create tile list and insert the initial tile.
+   * Tiles are numbered when they are queued (not when they are processed),
+   * so the continent number check below also rejects tiles that are already
+   * waiting in the list. This avoids a linear search of the list for every
+   * neighbouring tile, which is very slow on large maps. */
   tlist = tile_list_new();
+  tile_set_continent(ptile, nr);
+  if (nr < 0) {
+    wld.map.ocean_sizes[-nr]++;
+  } else {
+    wld.map.continent_sizes[nr]++;
+  }
   tile_list_append(tlist, ptile);
 
   while (tile_list_size(tlist) > 0) {
@@ -327,22 +337,18 @@ static void assign_continent_flood(struct tile *ptile, bool is_land, int nr)
         continue;
       }
 
-      /* Add the tile to the list of tiles to check. */
-      if (!tile_list_search(tlist, ptile3)) {
-        tile_list_append(tlist, ptile3);
+      /* Number the tile and add it to the list of tiles to check. */
+      tile_set_continent(ptile3, nr);
+      if (nr < 0) {
+        wld.map.ocean_sizes[-nr]++;
+      } else {
+        wld.map.continent_sizes[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);
+    /* This tile is fully processed; remove it from the list. */
     tile_list_remove(tlist, ptile2);
-
-    /* Count the tile */
-    if (nr < 0) {
-      wld.map.ocean_sizes[-nr]++;
-    } else {
-      wld.map.continent_sizes[nr]++;
-    }
   }
 
   tile_list_destroy(tlist);
