Skip to content

Optimize CPU verifier and fix biome shift sampling - #35

Open
sync0516 wants to merge 3 commits into
MinecraftAtHome:mainfrom
sync0516:codex/cpu-verifier-optimizations
Open

Optimize CPU verifier and fix biome shift sampling#35
sync0516 wants to merge 3 commits into
MinecraftAtHome:mainfrom
sync0516:codex/cpu-verifier-optimizations

Conversation

@sync0516

Copy link
Copy Markdown

Summary

  • Fix the Monte Carlo prefilter's shifted-Z sampling order to match cubiomes: sampleDoublePerlin(shift, z, x, 0).
  • Replace per-cell getBiomeAt() allocation with an exact staged mushroom-fields classifier: sample continentalness first, safely reject non-candidates, then run the remaining climate parameters and climateToBiome().
  • Replace the full int state grid and worst-case preallocated flood queue with a reusable visited bitmap, compact candidate list, and dynamically growing queue.
  • Add an opt-in CPU_ARCH=native setting for GCC/Clang CPU builds; the default remains portable.

Motivation

The production large-biome verifier covers 51,200 blocks. At scale 4 this is a 12,800 x 12,800 grid (163,840,000 cells). The current implementation allocates approximately:

  • 625 MiB for the integer state grid
  • 1,875 MiB for the flood-fill queue

That is about 2.44 GiB per CPU worker before allocator overhead.

The existing Monte Carlo callback also swaps the arguments used for the shifted Z coordinate. In a 262,144-point scale-4 comparison, this changed 15 prefilter decisions, including 11 where the old expression rejected a point accepted by the correctly ordered expression.

Benchmarks

Ryzen 5 7600X, Windows 11, GCC 16.2, large biomes, seed 664124959764, center (1600, 16000), range 51,200, minimum area 1,000,000, scale 4:

Measurement Current This PR
Peak private memory 2,505.6 MiB 82.3 MiB
Peak working set 662.2 MiB 58.1 MiB
Warm portable verification 8.93 s 8.26 s
Current portable vs this PR with CPU_ARCH=native 9.20 s 5.91 s

The returned center and area were identical: (2734, 6795), 72,710,512 blocks.

Correctness checks

  • Exact staged classifier vs getBiomeAt(): 524,288 random points across normal/large biomes and scales 4/16, zero mismatches.
  • Known large mushroom region: 1,661,598 continentalness candidates, zero exact-classifier mismatches.
  • Eight full differential searches covering normal/large biomes, scales 4/16, hits, and misses: identical found state, center, and area.
  • Default and CPU_ARCH=native CPU-only builds pass.
  • -Wall -Wextra -Wpedantic produces no diagnostics.
  • git diff --check passes.

A continentalness-only final classifier was tested and rejected: it produced 603 false-positive cells at scale 4 and 26 at scale 16 in the known region, which could inflate or connect islands.

Scope

This PR is intentionally CPU-only. It does not duplicate the GPU work already proposed in #28.

Refactor cubiomes.c to improve memory management and structure usage. Replace malloc with calloc for Cubiomes creation, implement FloodEntry struct for queue management, and update flood fill logic to use the new structure.
Added section for optional CPU architecture tuning in README.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant