Merge pull request #949 from rdbtCVS/t3/reuse-terrain-sampler-scratch

🤖 Agent: merged, thanks rdbt! Terrain sampler scratch buffers are reused between chunks; byte-identical output across the 22 world-gen parity harnesses on the combined commit.
This commit is contained in:
Ludovic Marie
2026-10-19 04:21:05 +02:00
committed by GitHub
3 changed files with 97 additions and 6 deletions
+7
View File
@@ -103,6 +103,13 @@ impl SamplerContext {
self.scratch.push(buffer);
result
}
/// Start an independent evaluation, retaining only temporary scratch buffers.
/// Cached scalar/volume results and world bindings must not cross chunks or
/// graphs, whose cache IDs can refer to different functions.
pub fn reset(&mut self) {
self.caches = self.caches.as_ref().map(|_| Vec::new());
self.fields = WorldFields::default();
}
/// Release retained storage between worlds or after unusually large work.
pub fn clear_scratch(&mut self) {
self.scratch.clear();
+70
View File
@@ -755,3 +755,73 @@ fn spline_lazy_branch_failure_preserves_completed_output_and_returns_scratch() {
assert_eq!(context.scratch.len(), 2);
assert_eq!(sampler.sample_value(&mut context, [0; 3]), Ok(7.0));
}
#[test]
fn reset_retains_scratch_but_discards_graph_caches_and_world_bindings() {
let binding = Arc::new(ConstantSampler(17.0));
let lifetime = Arc::downgrade(&binding);
let mut context = SamplerContext::cached()
.with_world_fields(WorldFields::default().with_field(WorldField::Beardifier, binding));
let cached = CachingDensitySampler::new(
0,
Arc::new(ContextBoundSampler::new(WorldField::Beardifier)),
)
.unwrap();
let volume = DensityVolume::new([2; 3], [0; 3], [1; 3]).unwrap();
let mut output = [0.0; 8];
cached
.sample_volume(&mut context, &mut output, volume)
.unwrap();
assert_eq!(output, [17.0; 8]);
assert_eq!(cached.sample_value(&mut context, [9; 3]), Ok(17.0));
context
.with_buffer(128, |_, buffer| {
buffer.fill(99.0);
Ok(())
})
.unwrap();
let allocation = context.scratch[0].as_ptr();
let capacity = context.scratch[0].capacity();
context.reset();
assert!(lifetime.upgrade().is_none());
assert!(context.caches.as_ref().unwrap().is_empty());
assert_eq!(context.scratch[0].as_ptr(), allocation);
assert_eq!(context.scratch[0].capacity(), capacity);
// Same coordinates and cache ID must resolve the new context's fallback.
assert_eq!(cached.sample_value(&mut context, [9; 3]), Ok(0.0));
cached
.sample_volume(&mut context, &mut output, volume)
.unwrap();
assert_eq!(output, [0.0; 8]);
// A different graph may reuse IDs without inheriting scalar/volume results.
context.reset();
let replacement = CachingDensitySampler::new(0, Arc::new(ConstantSampler(3.0))).unwrap();
assert_eq!(replacement.sample_value(&mut context, [9; 3]), Ok(3.0));
replacement
.sample_volume(&mut context, &mut output, volume)
.unwrap();
assert_eq!(output, [3.0; 8]);
}
#[test]
fn reset_after_sampling_error_drops_blender_and_preserves_uncached_mode() {
let blender = Arc::new(EmptyBlender);
let lifetime = Arc::downgrade(&blender);
let mut context =
SamplerContext::default().with_world_fields(WorldFields::default().with_blender(blender));
let result: Result<(), SampleError> = context.with_buffer(64, |_, buffer| {
buffer.fill(42.0);
Err(SampleError::RecursiveCache)
});
assert_eq!(result, Err(SampleError::RecursiveCache));
let allocation = context.scratch[0].as_ptr();
context.reset();
assert!(lifetime.upgrade().is_none());
assert!(context.caches.is_none());
context
.with_buffer(32, |_, buffer| {
assert_eq!(buffer.as_ptr(), allocation);
Ok(())
})
.unwrap();
}
+20 -6
View File
@@ -973,6 +973,19 @@ where
&self,
worker: &mut WorkerState,
pos: ChunkPos,
) -> Result<ProtoChunk, GeneratorError> {
let mut context = std::mem::take(&mut worker.terrain_context);
let result = self.generate_with_context(worker, pos, &mut context);
// Release chunk-specific bindings and caches even after a sampling error.
context.reset();
worker.terrain_context = context;
result
}
fn generate_with_context(
&self,
worker: &mut WorkerState,
pos: ChunkPos,
context: &mut SamplerContext,
) -> Result<ProtoChunk, GeneratorError> {
let data = &self.data;
let (min, height, sea) = (
@@ -1025,13 +1038,12 @@ where
),
None => Vec::new(),
};
let mut context = SamplerContext::cached();
if let Some(beard) = self
.structures
.as_ref()
.and_then(|s| s.beardifier(&references, pos))
{
context = context.with_world_fields(
*context = std::mem::take(context).with_world_fields(
horizon_core::density::WorldFields::default().with_field(
horizon_core::density::WorldField::Beardifier,
Arc::new(beard),
@@ -1056,7 +1068,7 @@ where
.fork_positional(),
RegistryAquiferBlockStates::new(data.registry).map_err(stage("aquifer"))?,
volume,
&mut context,
context,
)
.map_err(stage("aquifer"))?,
),
@@ -1070,7 +1082,7 @@ where
&mut chunk,
volume,
self.router.get(RouterField::FinalDensity).as_ref(),
&mut context,
context,
aquifer.as_mut(),
self.settings.default_block,
)
@@ -1091,7 +1103,7 @@ where
possible: Some(possible),
},
preliminary_surface: self.preliminary.clone(),
density_context: &mut context,
density_context: context,
write_min_y: min,
write_max_y: min + height - 1,
},
@@ -1126,7 +1138,7 @@ where
possible: None,
},
preliminary_surface: self.preliminary.clone(),
density_context: &mut context,
density_context: context,
},
aquifer.as_mut(),
&data.carving_blocks,
@@ -1431,6 +1443,7 @@ pub struct StageTimings {
/// thread-local caches and never change results.
pub(super) struct WorkerState {
pub(super) temperatures: Vec<BiomeTemperature>,
terrain_context: SamplerContext,
biome_context: SamplerContext,
carver_context: SamplerContext,
/// Seed/dimension-bound lookups avoid the shared mutex for overlapping
@@ -1909,6 +1922,7 @@ impl OverworldGenerator {
.iter()
.map(|s| BiomeTemperature::new(*s))
.collect(),
terrain_context: SamplerContext::cached(),
biome_context: SamplerContext::cached(),
carver_context: SamplerContext::default(),
carver_biomes: HashMap::new(),