mojira.dev
MC-313429

ore_vein material rules take exponential time per chunk with shared density functions (domainAxes)

Short description

In 26.4-snapshot-10, each ore_vein material rule calls domainAxes() on its raw density functions once per chunk. domainAxes() isn't memoized, so it costs one visit per root-to-leaf path through the function graph, not one per node. Worldgen packs reuse named density functions (references and cache) a lot, so the path count grows exponentially with depth. Past about 30 levels of reuse, a chunk never finishes generating, even though the functions themselves are cheap to evaluate.

Steps to reproduce

  1. Start a 26.4-snapshot-10 dedicated server with a new flat world and the attached data pack mc-domainaxes-repro.zip in its datapacks folder.

  2. Run these one at a time and time each reply:

/execute in repro:n20 run forceload add 0 0
/execute in repro:n24 run forceload add 0 0
/execute in repro:n26 run forceload add 0 0
/execute in repro:n28 run forceload add 0 0
/execute in repro:terrain40 run forceload add 0 0
/execute in repro:n40 run forceload add 0 0

In the pack, repro:chain/0 is a noise function and repro:chain/k is cache(max(chain/k-1, chain/k-1)), so chain/k has k+1 nodes but 2^k paths. Dimension repro:nK paints with one ore_vein rule whose density is repro:chain/K, then stone. The control, repro:terrain40, uses repro:chain/40 only in final_density and paints plain stone.

Expected result

All the dimensions load in about the same time, since the functions are cheap to evaluate (see the control).

Actual result

Seconds until forceload replies (measured with max-tick-time=-1 so the slow steps can finish):

Dimension         Paths      Seconds
repro:n0          1          0.67
repro:n20         2^20       0.17
repro:n24         2^24       1.25
repro:n26         2^26       4.17
repro:n28         2^28       14.8
repro:n30         2^30       75.7
repro:n40         2^40       never (killed after more than 2 minutes)
repro:terrain40   2^40       0.29  (control: same function, final_density only)

That's about 4x per 2 levels, linear in the path count. With the default max-tick-time, forceload in n30 or n40 makes the watchdog crash the server after 60 s. A player in such a dimension just sees chunks that never load.

A thread dump taken while n40 is stuck (attached jstack-n40-excerpt.txt) shows all 19 worldgen workers in this recursion:

at ...op.BinaryFunction.domainAxes(BinaryFunction.java:130)
at ...op.CacheFunction.domainAxes(CacheFunction.java:36)
at ...DensityFunctions$HolderHolder.domainAxes(DensityFunctions.java:425)
... (these three frames repeat once per level) ...
at ...MaterialRuleContext.lambda$getDensitiesInChunk$0(MaterialRuleContext.java:196)
at java.util.HashMap.computeIfAbsent(HashMap.java:1229)
at ...MaterialRuleContext.getDensitiesInChunk(MaterialRuleContext.java:195)
at ...rule.OreVeinRule.compile(OreVeinRule.java:65)
at ...rule.SequenceRule.compile(SequenceRule.java:28)
at ...ChunkTerrainBuilder.<init>(ChunkTerrainBuilder.java:49)
at ...NoiseBasedChunkGenerator.buildTerrain(NoiseBasedChunkGenerator.java:290)

Code analysis

  • ChunkTerrainBuilder makes a new MaterialRuleContext per chunk, so its density getter cache starts empty for every chunk.

  • OreVeinRule.compile calls getDensitiesInChunk for density, richness and filler_gap. On a cache miss, it checks f.domainAxes() on the raw function to decide 2D or 3D.

  • domainAxes() recurses with no memo. HolderHolder forwards to its target; cache, unary, clamp and upscale forward to their input; binary, range_choice, lerp, interval_select, domain_warp, find_top_surface and splines combine all their children. So a holder referenced twice per level doubles the work per level.

  • Terrain sampling avoids this: DensityFunctionCompiler works on the shared graph, and its PreparedCache already stores the axes.

Possible fixes

  • Take the axes from the compiled or prepared function, or memoize domainAxes() per function or holder, so the cost is linear in the node count.

  • The 2D/3D answer doesn't change between chunks, so it could be computed once (for example in MaterialSystem).

  • range() recurses through HolderHolder the same way. We haven't seen it stall, but it may want the same memo.

Impact

Real packs hit this. Our custom dimension's surface masks share large terrain functions, with domainAxes path counts around 10^18 to 10^22. On 26.4-snapshot-10 its first chunks never generate (one test spent 24,899 CPU-seconds without finishing the first 100 chunks). With those rules replaced, the same terrain generates 5 to 7 times faster than on 26.3.

Environment

26.4-snapshot-10 dedicated server (official server jar), Java 25.0.4, Windows 10 (build 19045), 19 worldgen worker threads. No mods; only the attached data pack.

Attachments

Comments 1

Thank you for helping us improve Minecraft! We saved your files:

[media][media]

Alexander

(Unassigned)

Unconfirmed

26.4 Snapshot 10

Retrieved