Quick answer: Godot 4 set_cells_terrain_connect taking 200ms for moderate-sized regions? Terrain matching iterates per cell against rules - precompute terrain bitmasks or use set_cell directly.
Procedural cave generates 64x64 chunks. Terrain matching adds 200ms per chunk to gen time.
Set cells directly
For procedural generation, decide terrain at gen time and call set_cell with the right tile. Skip the matching step.
Batch chunks
Generate 4 chunks per frame; spread cost. UX-wise: a loading bar is fine; a 200ms hitch isn't.
Pre-compute the rule set
If you must use terrain matching, precompute a lookup table mapping neighbor masks to tile IDs. Lookup is O(1).
“TileMap terrain matching is convenient at edit time, slow at runtime.”
Use the matching API for the editor's auto-tiling. Use raw set_cell for runtime generation. Different problems; different tools.