From 6f70591a20ae94a95630dddb2d3d5f06c2a83d4a Mon Sep 17 00:00:00 2001 From: Alex Lytvynenko Date: Thu, 5 Nov 2026 02:37:42 +0100 Subject: [PATCH 1/3] claim: preserve removed hash records across growth (WR-13) Scoped draft claim; tests-first implementation and verification pending. Co-Authored-By: GPT-6.1 Sol From 827bb4cab94a852a9b45e0eff70b6fc00537f1a1 Mon Sep 17 00:00:00 2001 From: Alex Lytvynenko Date: Thu, 5 Nov 2026 02:40:48 +0100 Subject: [PATCH 2/3] test: expose WR-13 default growth corruption Co-Authored-By: GPT-6.1 Terra --- crates/solidcore/src/geom/hash_table.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/crates/solidcore/src/geom/hash_table.rs b/crates/solidcore/src/geom/hash_table.rs index d58530b5ae..d63bbe6397 100644 --- a/crates/solidcore/src/geom/hash_table.rs +++ b/crates/solidcore/src/geom/hash_table.rs @@ -1383,6 +1383,26 @@ mod tests { assert_eq!(full_hist, [1, 0, 2, 0, 3, 0, 0, 0, 4, 0, 0]); } + /// Capacity growth preserves removed records and every table link and count. + #[test] + fn wr13_default_growth_preserves_all_removed_records() { + let mut table = HashTable::new(0, 4).unwrap(); + table.add(101, 0, 11).unwrap(); + table.add(202, 2, 22).unwrap(); + assert_eq!(table.remove(&[0, 1]), 2); + let records = table.bucket_items().to_vec(); + let buckets = table.buckets().to_vec(); + let used = table.first_used_bucket(); + let counts = (table.total_bucket_item_count(), table.removed_bucket_item_count(), table.used_bucket_item_count(), table.used_bucket_count()); + table.inc_max_item_count(); + assert_eq!(table.capacity(), 128); + assert_eq!(table.bucket_items(), records); + assert_eq!(table.buckets(), buckets); + assert_eq!(table.first_used_bucket(), used); + assert_eq!((table.total_bucket_item_count(), table.removed_bucket_item_count(), table.used_bucket_item_count(), table.used_bucket_count()), counts); + assert_eq!(table.remove(&[0, 1]), 0); + } + #[test] fn wr13_default_grow_after_removing_all_zeroes_records_and_corrupts_remove() { let mut ht = HashTable::new(1, 4).unwrap(); From e63384bb6f971f7b8e0d94b466b9adc8245d1b0f Mon Sep 17 00:00:00 2001 From: Alex Lytvynenko Date: Thu, 5 Nov 2026 02:48:19 +0100 Subject: [PATCH 3/3] fix: preserve hash records during default growth Co-Authored-By: GPT-6.1 Terra --- crates/solidcore/src/geom/hash_table.rs | 463 +++++++----------- crates/solidcore/src/kernel.rs | 12 - .../issue_2260_gn_pipe_refined_range_end.rs | 1 - .../solidcore/tests/kernel_cases/fixes.json | 3 +- ...ddendum-2026-11-05-default-preservation.md | 14 + 5 files changed, 188 insertions(+), 305 deletions(-) create mode 100644 plan/upstream-issues/WR-13--addendum-2026-11-05-default-preservation.md diff --git a/crates/solidcore/src/geom/hash_table.rs b/crates/solidcore/src/geom/hash_table.rs index d63bbe6397..08e1c9c833 100644 --- a/crates/solidcore/src/geom/hash_table.rs +++ b/crates/solidcore/src/geom/hash_table.rs @@ -16,14 +16,14 @@ //! | [`ObjsInVoxels::map_point_to_voxel`] | `SmObjsInVoxels::MapPointToVoxel` | //! | [`ObjsInVoxels::map_voxel_index_to_bbox`] | `SmObjsInVoxels::MapVoxelIndexToBBox` | //! | [`ObjsInVoxels::find_point_items`] | `SmObjsInVoxels::FindPointItems` | -//! | [`ObjsInVoxels::add`], [`ObjsInVoxels::add_with_fixes`] | `SmObjsInVoxels::Add` | +//! | [`ObjsInVoxels::add`] | `SmObjsInVoxels::Add` | //! | [`ObjsInVoxels::remove`] | `SmObjsInVoxels::Remove` | //! | [`ObjsInVoxels::find_coincident_pairs`] | `SmObjsInVoxels::FindCoincidentPairs` | //! | [`ObjsInVoxels::point_items`], [`ObjsInVoxels::hash_table`], ... | `GetPointItems`, `m_sHashTable`, ... | //! | [`PointItem`] | `SmPointItem` | //! | [`HashTable`] | `SmHashTable` (template bodies this unit needs) | -//! | [`HashTable::inc_max_item_count`], [`HashTable::inc_max_item_count_with_fixes`] | `SmHashTable::IncMaxItemCount` | -//! | [`HashTable::add`], [`HashTable::add_with_fixes`] | `SmHashTable::Add` | +//! | [`HashTable::inc_max_item_count`] | `SmHashTable::IncMaxItemCount` | +//! | [`HashTable::add`] | `SmHashTable::Add` | //! | [`HashTable::remove`] | `SmHashTable::Remove` | //! | [`BucketItem`] | `SmBucketItem` | //! | [`gap_histogram_index`] | `SM_HISTOGRAM_GAPSIZE_INDEX` | @@ -35,11 +35,7 @@ //! bucket-item indices, `SM_UNDEF_ULONG` bucket indices and voxel indices are //! `None`. Growing the arrays therefore never invalidates anything. Capacities //! (`GetDataSize`) are still tracked so growth happens at the same moments, and the -//! all-removed growth path that zeroes the bucket-item records upstream (WR-13) is -//! reproduced by default unless -//! [`UpstreamFixes::hash_table_grow_preserves_removed_records`] is enabled via -//! [`HashTable::inc_max_item_count_with_fixes`], [`HashTable::add_with_fixes`], or -//! [`ObjsInVoxels::add_with_fixes`]. +//! all-removed growth path preserves existing records instead of zeroing them (WR-13). //! * Upstream undefined behaviour (a bucket index past the table, a voxel model //! with a box but zero voxels in `Add`, an empty explicit bucket list, a zero //! divisor in `MapVoxelIndexToIndices`) returns [`SolidError::InvalidInput`] @@ -55,7 +51,7 @@ use crate::error::{Result, SolidError}; use crate::geom::extent3d::Extent3d; use crate::geom::tol::xsect_tol3d; -use crate::kernel::{Kernel, UpstreamFixes}; +use crate::kernel::Kernel; use crate::math::{Point3, UNDEF_DOUBLE}; /// Number of gap-size histogram buckets (`SM_HISTOGRAM_GAPSIZE_BUCKETCNT`). @@ -126,8 +122,7 @@ pub struct BucketItem { /// `== SM_UNDEF_ULONG`). pub bucket_index: Option, /// The hashed item: for [`ObjsInVoxels`] the index of its [`PointItem`] - /// (`m_sItem`; `None` only for records zeroed by a growth, see - /// [`HashTable::inc_max_item_count`]). + /// (`m_sItem`). pub item: Option, /// Caller id stored with the item (`m_lItemId`). pub item_id: usize, @@ -200,40 +195,10 @@ impl HashTable { Ok(()) } - /// Grows the item capacity to `max(1.5 * capacity, capacity + 64)` - /// (`IncMaxItemCount`) with default [`UpstreamFixes`]. - /// - /// Upstream copies the item records into the new array only while some item - /// is still in a bucket (`GetUsedBucketItemCount() > 0`); otherwise the - /// records come back zero-filled (bucket `0`, no item, id `0`, no links; - /// WR-13), which is reproduced here. Use - /// [`Self::inc_max_item_count_with_fixes`] to honour - /// [`UpstreamFixes::hash_table_grow_preserves_removed_records`]. + /// Grows the item capacity without changing existing records or links + /// (`IncMaxItemCount`); unlike upstream, this preserves removed records. pub fn inc_max_item_count(&mut self) { - self.inc_max_item_count_with_fixes(&UpstreamFixes::default()); - } - - /// Grows the item capacity to `max(1.5 * capacity, capacity + 64)` - /// (`IncMaxItemCount`), honouring - /// [`UpstreamFixes::hash_table_grow_preserves_removed_records`]. - /// - /// When `fixes.hash_table_grow_preserves_removed_records` is `false` - /// (default), reproduces upstream WR-13 by overwriting existing records with - /// zero-filled entries (`bucket_index: Some(0)`, `item: None`, `item_id: 0`, - /// no links) whenever `used_bucket_item_count() == 0`. When `true`, existing - /// removed records (`bucket_index: None`, `item`, `item_id`) are preserved - /// across growth even when all items have been removed. - pub fn inc_max_item_count_with_fixes(&mut self, fixes: &UpstreamFixes) { self.capacity = grown_capacity(self.capacity); - if !fixes.hash_table_grow_preserves_removed_records && self.used_bucket_item_count() == 0 { - self.bucket_items.fill(BucketItem { - next_used_bucket: None, - next_bucket_item: None, - bucket_index: Some(0), - item: None, - item_id: 0, - }); - } } /// Bucket heads (`GetBuckets`). @@ -293,33 +258,14 @@ impl HashTable { self.removed_bucket_item_count } - /// Adds `item` to bucket `bucket_index` with id `item_id` (`Add`) using - /// default [`UpstreamFixes`]: the first item of an empty bucket is pushed on - /// the used-bucket list, later items go second in their bucket. + /// Adds `item` to bucket `bucket_index` with id `item_id` (`Add`): + /// the first item of an empty bucket is pushed on the used-bucket list, + /// later items go second in their bucket. /// /// # Errors /// - /// [`SolidError::InvalidInput`] when `bucket_index` is not a bucket (undefined - /// behaviour upstream). + /// [`SolidError::InvalidInput`] when `bucket_index` is not a bucket. pub fn add(&mut self, item: usize, bucket_index: usize, item_id: usize) -> Result<()> { - self.add_with_fixes(item, bucket_index, item_id, &UpstreamFixes::default()) - } - - /// Adds `item` to bucket `bucket_index` with id `item_id` (`Add`), - /// forwarding `fixes` to [`Self::inc_max_item_count_with_fixes`] when the - /// item array grows. - /// - /// # Errors - /// - /// [`SolidError::InvalidInput`] when `bucket_index` is not a bucket (undefined - /// behaviour upstream). - pub fn add_with_fixes( - &mut self, - item: usize, - bucket_index: usize, - item_id: usize, - fixes: &UpstreamFixes, - ) -> Result<()> { let Some(&head) = self.buckets.get(bucket_index) else { return Err(SolidError::InvalidInput { operation: "geom::hash_table::HashTable::add", @@ -327,7 +273,7 @@ impl HashTable { }); }; if self.bucket_items.len() == self.capacity { - self.inc_max_item_count_with_fixes(fixes); + self.inc_max_item_count(); } let new = self.bucket_items.len(); let mut rec = BucketItem { @@ -610,8 +556,8 @@ impl ObjsInVoxels { /// Grows the point-item and bucket-item capacities together /// (`IncMaxItemCount`). - fn inc_max_item_count(&mut self, fixes: &UpstreamFixes) { - self.hash.inc_max_item_count_with_fixes(fixes); + fn inc_max_item_count(&mut self) { + self.hash.inc_max_item_count(); self.point_capacity = self.hash.capacity(); } @@ -767,44 +713,19 @@ impl ObjsInVoxels { .collect() } - /// Places `item` at `point` with zone tolerance `zone_tol` into its voxel, - /// and into each neighbouring voxel whose shared side is within `zone_tol` - /// (`Add`), using default [`UpstreamFixes`]. Returns the voxel index, or - /// `None` when the model has no box. The voxels used are added (once) to - /// `updated`. + /// Places `item` at `point` in its voxel and neighbouring voxels within + /// `zone_tol` (`Add`). Returns the voxel index, or `None` with no box; + /// used voxels are added once to `updated`. /// /// # Errors /// - /// [`SolidError::InvalidInput`] when the model has a box but no voxels - /// (undefined behaviour upstream). + /// [`SolidError::InvalidInput`] when the model has a box but no voxels. pub fn add( - &mut self, - item: T, - point: Point3, - zone_tol: f64, - updated: Option<&mut Vec>, - ) -> Result> { - self.add_with_fixes(item, point, zone_tol, updated, &UpstreamFixes::default()) - } - - /// Places `item` at `point` with zone tolerance `zone_tol` into its voxel, - /// and into each neighbouring voxel whose shared side is within `zone_tol` - /// (`Add`), forwarding `fixes` when growing the item arrays (see - /// [`UpstreamFixes::hash_table_grow_preserves_removed_records`]). Returns - /// the voxel index, or `None` when the model has no box. The voxels used are - /// added (once) to `updated`. - /// - /// # Errors - /// - /// [`SolidError::InvalidInput`] when the model has a box but no voxels - /// (undefined behaviour upstream). - pub fn add_with_fixes( &mut self, item: T, point: Point3, zone_tol: f64, mut updated: Option<&mut Vec>, - fixes: &UpstreamFixes, ) -> Result> { if self.bbox.is_init() { return Ok(None); @@ -816,7 +737,7 @@ impl ObjsInVoxels { }); }; while self.point_items.len() + MAX_ITEMS_PER_ADD > self.point_capacity { - self.inc_max_item_count(fixes); + self.inc_max_item_count(); } let v = hit.index; let pi = PointItem { @@ -824,27 +745,27 @@ impl ObjsInVoxels { point, zone_tol, }; - self.place(pi, v, &mut updated, fixes)?; + self.place(pi, v, &mut updated)?; let (on_boundary, inward) = hit.extent.is_point_on_boundary(point, zone_tol); if on_boundary { let xy = self.x_cnt.wrapping_mul(self.y_cnt); if inward.x == -1.0 && hit.ix < self.x_cnt.wrapping_sub(1) { - self.place(pi, v.wrapping_add(1), &mut updated, fixes)?; + self.place(pi, v.wrapping_add(1), &mut updated)?; } if inward.x == 1.0 && hit.ix > 0 { - self.place(pi, v.wrapping_sub(1), &mut updated, fixes)?; + self.place(pi, v.wrapping_sub(1), &mut updated)?; } if inward.y == -1.0 && hit.iy < self.y_cnt.wrapping_sub(1) { - self.place(pi, v.wrapping_add(self.x_cnt), &mut updated, fixes)?; + self.place(pi, v.wrapping_add(self.x_cnt), &mut updated)?; } if inward.y == 1.0 && hit.iy > 0 { - self.place(pi, v.wrapping_sub(self.x_cnt), &mut updated, fixes)?; + self.place(pi, v.wrapping_sub(self.x_cnt), &mut updated)?; } if inward.z == -1.0 && hit.iz < self.z_cnt.wrapping_sub(1) { - self.place(pi, v.wrapping_add(xy), &mut updated, fixes)?; + self.place(pi, v.wrapping_add(xy), &mut updated)?; } if inward.z == 1.0 && hit.iz > 0 { - self.place(pi, v.wrapping_sub(xy), &mut updated, fixes)?; + self.place(pi, v.wrapping_sub(xy), &mut updated)?; } } Ok(Some(v)) @@ -856,11 +777,10 @@ impl ObjsInVoxels { pi: PointItem, voxel: usize, updated: &mut Option<&mut Vec>, - fixes: &UpstreamFixes, ) -> Result<()> { self.point_items.push(pi); let idx = self.point_items.len() - 1; - self.hash.add_with_fixes(idx, voxel, idx, fixes)?; + self.hash.add(idx, voxel, idx)?; if let Some(u) = updated.as_deref_mut() { add_unique(u, voxel); } @@ -1393,82 +1313,40 @@ mod tests { let records = table.bucket_items().to_vec(); let buckets = table.buckets().to_vec(); let used = table.first_used_bucket(); - let counts = (table.total_bucket_item_count(), table.removed_bucket_item_count(), table.used_bucket_item_count(), table.used_bucket_count()); + let counts = ( + table.total_bucket_item_count(), + table.removed_bucket_item_count(), + table.used_bucket_item_count(), + table.used_bucket_count(), + ); table.inc_max_item_count(); assert_eq!(table.capacity(), 128); assert_eq!(table.bucket_items(), records); assert_eq!(table.buckets(), buckets); assert_eq!(table.first_used_bucket(), used); - assert_eq!((table.total_bucket_item_count(), table.removed_bucket_item_count(), table.used_bucket_item_count(), table.used_bucket_count()), counts); + assert_eq!( + ( + table.total_bucket_item_count(), + table.removed_bucket_item_count(), + table.used_bucket_item_count(), + table.used_bucket_count() + ), + counts + ); assert_eq!(table.remove(&[0, 1]), 0); } + /// Removed handles remain inert after growth and subsequent bucket reuse. #[test] - fn wr13_default_grow_after_removing_all_zeroes_records_and_corrupts_remove() { + fn wr13_removed_handles_remain_inert_after_growth() { let mut ht = HashTable::new(1, 4).unwrap(); - assert_eq!(ht.capacity(), 65); ht.add(101, 2, 11).unwrap(); ht.add(202, 3, 22).unwrap(); ht.add(303, 2, 33).unwrap(); assert_eq!(ht.remove(&[0, 1, 2]), 3); - - assert_eq!(ht.total_bucket_item_count(), 3); - assert_eq!(ht.removed_bucket_item_count(), 3); - assert_eq!(ht.used_bucket_item_count(), 0); - assert_eq!(ht.used_bucket_count(), 0); - assert_eq!(ht.first_used_bucket(), None); - assert!(ht.bucket_items().iter().all(BucketItem::is_removed)); - assert_eq!(ht.bucket_items()[1].item, Some(202)); - assert_eq!(ht.bucket_items()[1].item_id, 22); - - let mut via_default_fixes = ht.clone(); - ht.inc_max_item_count(); - via_default_fixes.inc_max_item_count_with_fixes(&UpstreamFixes::default()); - assert_eq!(ht.capacity(), 129); - assert_eq!(ht.bucket_items(), via_default_fixes.bucket_items()); - - let zeroed = BucketItem { - next_used_bucket: None, - next_bucket_item: None, - bucket_index: Some(0), - item: None, - item_id: 0, - }; - assert_eq!(ht.bucket_items(), &[zeroed, zeroed, zeroed]); - assert!(ht.bucket_items().iter().all(|b| !b.is_removed())); - assert_eq!(ht.buckets(), &[None, None, None, None]); - - // Re-removing already-removed handle 1 walks bucket 0 and increments - // removed_bucket_item_count past total_bucket_item_count (wrapping used count). - assert_eq!(ht.remove(&[1]), 1); - assert_eq!(ht.removed_bucket_item_count(), 4); - assert_eq!(ht.total_bucket_item_count(), 3); - assert_eq!(ht.used_bucket_item_count(), usize::MAX); - assert!(ht.bucket_items()[1].is_removed()); - assert!(!ht.bucket_items()[0].is_removed()); - assert!(!ht.bucket_items()[2].is_removed()); - } - - #[test] - fn wr13_opt_in_grow_after_removing_all_preserves_removed_records_and_invariants() { - let mut k = Kernel::default(); - assert!(!k.fixes.hash_table_grow_preserves_removed_records); - let flag = k - .fixes - .flag_mut("hash_table_grow_preserves_removed_records") - .unwrap(); - assert!(!*flag); - *flag = true; - assert!(k.fixes.hash_table_grow_preserves_removed_records); - - let mut ht = HashTable::new(1, 4).unwrap(); - ht.add_with_fixes(101, 2, 11, &k.fixes).unwrap(); - ht.add_with_fixes(202, 3, 22, &k.fixes).unwrap(); - ht.add_with_fixes(303, 2, 33, &k.fixes).unwrap(); - assert_eq!(ht.remove(&[0, 1, 2]), 3); let before_grow = ht.bucket_items().to_vec(); - ht.inc_max_item_count_with_fixes(&k.fixes); + ht.inc_max_item_count(); assert_eq!(ht.capacity(), 129); assert_eq!(ht.bucket_items(), before_grow.as_slice()); assert!(ht.bucket_items().iter().all(BucketItem::is_removed)); @@ -1484,9 +1362,9 @@ mod tests { assert_eq!(ht.used_bucket_count(), 0); // Subsequent additions (including into bucket 0) and removals keep exact counts. - ht.add_with_fixes(404, 0, 44, &k.fixes).unwrap(); - ht.add_with_fixes(505, 0, 55, &k.fixes).unwrap(); - ht.add_with_fixes(606, 2, 66, &k.fixes).unwrap(); + ht.add(404, 0, 44).unwrap(); + ht.add(505, 0, 55).unwrap(); + ht.add(606, 2, 66).unwrap(); assert_eq!(ht.remove(&[0, 1, 2]), 0); assert_eq!(ht.total_bucket_item_count(), 6); assert_eq!(ht.removed_bucket_item_count(), 3); @@ -1503,137 +1381,142 @@ mod tests { assert!(ht.bucket_items().iter().all(BucketItem::is_removed)); } + /// Repeated automatic growth preserves historical records and voxel reuse. #[test] - fn wr13_auto_grow_via_add_with_fixes_and_objs_in_voxels() { - let mut k = Kernel::default(); - k.fixes.hash_table_grow_preserves_removed_records = true; - - // 1. HashTable::add vs HashTable::add_with_fixes auto-growing at full capacity - // after all 64 initial slots were removed. - let mut ht_def = HashTable::new(0, 4).unwrap(); - let mut ht_fix = HashTable::new(0, 4).unwrap(); - for i in 0..64usize { - let bucket = (i % 3) + 1; - ht_def.add(1000 + i, bucket, i).unwrap(); - ht_fix - .add_with_fixes(1000 + i, bucket, i, &k.fixes) - .unwrap(); + fn wr13_auto_growth_and_voxel_reuse() { + let mut ht = HashTable::new(0, 4).unwrap(); + for i in 0..64 { + ht.add(1000 + i, i % 4, i).unwrap(); } - let all_64: Vec = (0..64).collect(); - assert_eq!(ht_def.remove(&all_64), 64); - assert_eq!(ht_fix.remove(&all_64), 64); - assert_eq!(ht_def.capacity(), 64); - assert_eq!(ht_def.used_bucket_item_count(), 0); - - // 65th add triggers inc_max_item_count while used_bucket_item_count == 0. - ht_def.add(9999, 0, 999).unwrap(); - ht_fix.add_with_fixes(9999, 0, 999, &k.fixes).unwrap(); - assert_eq!(ht_def.capacity(), 128); - assert_eq!(ht_fix.capacity(), 128); - assert_eq!(ht_def.bucket_items()[0].bucket_index, Some(0)); - assert_eq!(ht_def.bucket_items()[0].item, None); - assert_eq!(ht_fix.bucket_items()[0].bucket_index, None); - assert_eq!(ht_fix.bucket_items()[0].item, Some(1000)); - - // Re-removing slot 0 under default walks live bucket 0, double-counts removal, - // and drops used_bucket_item_count to 0 while slot 64 is still in bucket 0 - // (causing a second grow to zero live slot 64 as well). - assert_eq!(ht_def.remove(&[0]), 1); - assert_eq!(ht_def.used_bucket_item_count(), 0); - assert_eq!(ht_def.buckets()[0], Some(64)); - ht_def.inc_max_item_count(); - assert_eq!(ht_def.bucket_items()[64].item, None); - - // Under opt-in, slot 0 is skipped, live slot 64 stays intact across further - // growth, and removing slot 64 cleanly empties bucket 0. - assert_eq!(ht_fix.remove(&[0]), 0); - assert_eq!(ht_fix.used_bucket_item_count(), 1); - ht_fix.inc_max_item_count_with_fixes(&k.fixes); - assert_eq!(ht_fix.bucket_items()[64].item, Some(9999)); - assert_eq!(ht_fix.bucket_items()[64].item_id, 999); - assert_eq!(ht_fix.remove(&[64]), 1); - assert_eq!(ht_fix.used_bucket_item_count(), 0); - assert_eq!(ht_fix.buckets()[0], None); - - // 2. ObjsInVoxels::add vs ObjsInVoxels::add_with_fixes auto-growing when - // point_items.len() + MAX_ITEMS_PER_ADD > point_capacity after removing all. - let mut v_def = ObjsInVoxels::::new(2, 2, 1, Some(&unit_box()), 0).unwrap(); - let mut v_fix = ObjsInVoxels::::new(2, 2, 1, Some(&unit_box()), 0).unwrap(); - for id in 0..58u32 { - let p = Vec3::new(0.2, 0.2, 0.5); - v_def.add(id, p, 0.0, None).unwrap(); - v_fix.add_with_fixes(id, p, 0.0, None, &k.fixes).unwrap(); + assert_eq!(ht.remove(&(0..64).collect::>()), 64); + let old = ht.bucket_items().to_vec(); + ht.add(9999, 0, 0).unwrap(); + assert_eq!(ht.capacity(), 128); + assert_eq!(&ht.bucket_items()[..64], old); + assert_eq!(ht.remove(&[0]), 0); + for i in 65..128 { + ht.add(1000 + i, i % 4, i).unwrap(); + } + // The first four additions are bucket heads; subsequent ones are + // inserted immediately after their head, in reverse insertion order. + let expected: Vec<_> = (0..128) + .map(|i| { + if i < 64 { + BucketItem { + next_used_bucket: None, + next_bucket_item: None, + bucket_index: None, + item: Some(1000 + i), + item_id: i, + } + } else { + let next_bucket_item = match i { + 64 => Some(124), + 65 => Some(125), + 66 => Some(128), // the new item is inserted here + 67 => Some(127), + 68..=71 => None, + _ => Some(i - 4), + }; + BucketItem { + next_used_bucket: if (65..=67).contains(&i) { + Some(i - 1) + } else { + None + }, + next_bucket_item, + bucket_index: Some(i % 4), + item: Some(if i == 64 { 9999 } else { 1000 + i }), + item_id: if i == 64 { 0 } else { i }, + } + } + }) + .collect(); + ht.add(1234, 2, 1).unwrap(); + assert_eq!(ht.capacity(), 192); + assert_eq!(&ht.bucket_items()[..128], expected); + assert_eq!(&ht.bucket_items()[..64], old); + assert_eq!( + ht.bucket_items()[128], + BucketItem { + next_used_bucket: None, + next_bucket_item: Some(126), + bucket_index: Some(2), + item: Some(1234), + item_id: 1, + } + ); + assert_eq!(ht.buckets(), &[Some(64), Some(65), Some(66), Some(67)]); + assert_eq!(ht.first_used_bucket(), Some(67)); + assert_eq!(ht.used_bucket_count(), 4); + assert_eq!(ht.removed_bucket_item_count(), 64); + assert_eq!(ht.total_bucket_item_count(), 129); + assert_eq!(ht.remove(&[0]), 0); + assert_eq!(ht.used_bucket_item_count(), 65); + + let k = Kernel::default(); + let mut voxels = ObjsInVoxels::::new(2, 2, 1, Some(&unit_box()), 0).unwrap(); + for id in 0..58 { + voxels.add(id, Vec3::new(0.2, 0.2, 0.5), 0.0, None).unwrap(); } - for id in 0..58u32 { - assert_eq!(v_def.remove(id), 1); - assert_eq!(v_fix.remove(id), 1); + for id in 0..58 { + assert_eq!(voxels.remove(id), 1); } - assert_eq!(v_def.point_capacity(), 64); - assert_eq!(v_def.hash_table().used_bucket_item_count(), 0); - - let p_a = Vec3::new(0.75, 0.75, 0.5); - let p_b = Vec3::new(0.7502, 0.75, 0.5); - v_def.add(0, p_a, 1e-3, None).unwrap(); - v_def.add(100, p_b, 1e-3, None).unwrap(); - v_fix.add_with_fixes(0, p_a, 1e-3, None, &k.fixes).unwrap(); - v_fix - .add_with_fixes(100, p_b, 1e-3, None, &k.fixes) + voxels + .add(0, Vec3::new(0.75, 0.75, 0.5), 1e-3, None) .unwrap(); - assert_eq!(v_def.point_capacity(), 128); - assert_eq!(v_fix.point_capacity(), 128); - - let pairs_fix = v_fix + voxels + .add(100, Vec3::new(0.7502, 0.75, 0.5), 1e-3, None) + .unwrap(); + assert_eq!(voxels.point_capacity(), 128); + let pairs = voxels .find_coincident_pairs(&k, FindPairsOptions::default()) .unwrap(); - assert_eq!(sorted_item_pairs(&v_fix, &pairs_fix), [(0, 100)]); - assert_eq!(v_fix.find_point_items(0), vec![0, 58]); - - // Removing item 0 visits historical slot 0 and live slot 58: default double-counts - // zeroed slot 0, whereas opt-in skips slot 0 and removes only live slot 58. - assert_eq!(v_def.remove(0), 2); - assert_eq!(v_def.hash_table().used_bucket_item_count(), 0); - assert_eq!(v_fix.remove(0), 1); - assert_eq!(v_fix.removed_point_item_count(), 59); - assert_eq!(v_fix.hash_table().used_bucket_item_count(), 1); + assert_eq!(sorted_item_pairs(&voxels, &pairs), [(0, 100)]); + assert_eq!(voxels.find_point_items(0), vec![0, 58]); + assert_eq!(voxels.remove(0), 1); + assert_eq!(voxels.removed_point_item_count(), 59); + assert_eq!(voxels.hash_table().used_bucket_item_count(), 1); } + /// Empty and mixed tables retain records, heads, links and counts on growth. #[test] - fn wr13_non_empty_and_fresh_empty_grow_parity() { - let fixes = UpstreamFixes { - hash_table_grow_preserves_removed_records: true, - ..Default::default() - }; - - // Fresh empty table (total == 0, used == 0) grows identically under both modes. - let mut empty_def = HashTable::new(0, 4).unwrap(); - let mut empty_fix = HashTable::new(0, 4).unwrap(); - empty_def.inc_max_item_count(); - empty_fix.inc_max_item_count_with_fixes(&fixes); - assert_eq!(empty_def.capacity(), 64); - assert_eq!(empty_fix.capacity(), 64); - assert_eq!(empty_def.bucket_items(), empty_fix.bucket_items()); - - // Non-empty table with both active and removed records grows identically under - // both modes, preserving both active and removed records. - let mut mixed_def = HashTable::new(1, 4).unwrap(); - for (item, bucket, id) in [(10, 1, 1), (20, 2, 2), (30, 1, 3), (40, 3, 4)] { - mixed_def.add(item, bucket, id).unwrap(); + fn wr13_fresh_empty_and_mixed_growth() { + let mut empty = HashTable::new(0, 4).unwrap(); + empty.inc_max_item_count(); + assert_eq!(empty.capacity(), 64); + assert!(empty.bucket_items().is_empty()); + assert_eq!(empty.buckets(), &[None; 4]); + assert_eq!(empty.first_used_bucket(), None); + assert_eq!( + ( + empty.used_bucket_count(), + empty.used_bucket_item_count(), + empty.removed_bucket_item_count() + ), + (0, 0, 0) + ); + let mut mixed = HashTable::new(1, 4).unwrap(); + for (item, bucket, id) in [(10, 0, 1), (20, 2, 2), (30, 0, 3), (40, 3, 4)] { + mixed.add(item, bucket, id).unwrap(); } - assert_eq!(mixed_def.remove(&[0, 3]), 2); - assert_eq!(mixed_def.used_bucket_item_count(), 2); - assert_eq!(mixed_def.removed_bucket_item_count(), 2); - let mut mixed_fix = mixed_def.clone(); - - mixed_def.inc_max_item_count(); - mixed_fix.inc_max_item_count_with_fixes(&fixes); - assert_eq!(mixed_def.capacity(), mixed_fix.capacity()); - assert_eq!(mixed_def.buckets(), mixed_fix.buckets()); - assert_eq!(mixed_def.bucket_items(), mixed_fix.bucket_items()); - assert_eq!(mixed_def.first_used_bucket(), mixed_fix.first_used_bucket()); - assert_eq!(mixed_def.used_bucket_count(), mixed_fix.used_bucket_count()); + assert_eq!(mixed.remove(&[0, 3]), 2); + let records = mixed.bucket_items().to_vec(); + let heads = mixed.buckets().to_vec(); + let used = mixed.first_used_bucket(); + mixed.inc_max_item_count(); + assert_eq!(mixed.capacity(), 129); + assert_eq!(mixed.bucket_items(), records); + assert_eq!(mixed.buckets(), heads); + assert_eq!(mixed.first_used_bucket(), used); assert_eq!( - mixed_def.removed_bucket_item_count(), - mixed_fix.removed_bucket_item_count() + ( + mixed.total_bucket_item_count(), + mixed.used_bucket_item_count(), + mixed.removed_bucket_item_count(), + mixed.used_bucket_count() + ), + (4, 2, 2, 2) ); } } diff --git a/crates/solidcore/src/kernel.rs b/crates/solidcore/src/kernel.rs index 18b5feb11b..01b31b133a 100644 --- a/crates/solidcore/src/kernel.rs +++ b/crates/solidcore/src/kernel.rs @@ -274,18 +274,6 @@ pub struct UpstreamFixes { /// `SmPlane::IsNurbSurfacePlane` places a recognised surface's origin after /// orthogonalising its axes (WR-652, upstream issue #1056). pub plane_recognition_origin_after_axes: bool, - /// `SmHashTable::IncMaxItemCount` ([`crate::geom::hash_table::HashTable::inc_max_item_count_with_fixes`], - /// [`crate::geom::hash_table::HashTable::add_with_fixes`], and - /// [`crate::geom::hash_table::ObjsInVoxels::add_with_fixes`]) preserves existing - /// removed `SmBucketItem` records (`bucket_index == None`, `item`, `item_id`) when - /// growing the item array while `GetUsedBucketItemCount() == 0` - /// (`GetTotalBucketItemCount() > 0`), so a subsequent `Remove` skips already-removed - /// slots instead of walking bucket `0` on `calloc`-zeroed records (`bucket_index == 0`, - /// `item == NULL`, `item_id == 0`) and incrementing the removed-item counter past the - /// total item count (WR-13, #2200; see - /// `plan/upstream-issues/WR-13--addendum-2026-11-01-preserve-removed-records-on-grow.md`). - /// Off by default for pinned C++ parity. - pub hash_table_grow_preserves_removed_records: bool, /// `SmGapFunction::BuildSampleSet` samples the B-spline spans inside the requested /// curve interval or surface domain, normalizing against that interval and keeping /// its exact endpoints. Upstream starts at the first knot and normalizes against diff --git a/crates/solidcore/tests/issue_2260_gn_pipe_refined_range_end.rs b/crates/solidcore/tests/issue_2260_gn_pipe_refined_range_end.rs index 255f91536b..73825681c5 100644 --- a/crates/solidcore/tests/issue_2260_gn_pipe_refined_range_end.rs +++ b/crates/solidcore/tests/issue_2260_gn_pipe_refined_range_end.rs @@ -188,7 +188,6 @@ const SOLIDCRAFT_C494A18: &[&str] = &[ "global_solver_identical_prefers_larger_when_maximizing", "glue_vertices_joins_every_region", "good_drop_result_distances_in_order", - "hash_table_grow_preserves_removed_records", "heal_stage3_failures_propagate", "imprint_extends_edge_to_proto_vertex", "imprint_extension_keeps_analytic_curve", diff --git a/crates/solidcore/tests/kernel_cases/fixes.json b/crates/solidcore/tests/kernel_cases/fixes.json index 3a5925ffa4..dc364ca582 100644 --- a/crates/solidcore/tests/kernel_cases/fixes.json +++ b/crates/solidcore/tests/kernel_cases/fixes.json @@ -1,6 +1,5 @@ [ "plane_recognition_origin_after_axes", - "hash_table_grow_preserves_removed_records", "gap_samples_use_trimmed_spans", "gap_samples_non_bspline_v_stride", "transform_aligned_bbox_image", @@ -308,4 +307,4 @@ "plane_first_torus_section_exact", "keep_exact_pcurves", "shell_one_construction" -] \ No newline at end of file +] diff --git a/plan/upstream-issues/WR-13--addendum-2026-11-05-default-preservation.md b/plan/upstream-issues/WR-13--addendum-2026-11-05-default-preservation.md new file mode 100644 index 0000000000..7450588772 --- /dev/null +++ b/plan/upstream-issues/WR-13--addendum-2026-11-05-default-preservation.md @@ -0,0 +1,14 @@ +Updates: WR-13 + +#### WR-13 (addendum 2026-11-05) — Preserve removed records by default + +The prior opt-in behavior is now the only behavior: capacity growth retains all +records, links, bucket heads and counts even when every item is removed. A later +removal of an old index therefore cannot corrupt the count or a live bucket. +The `hash_table_grow_preserves_removed_records` flag and three `*_with_fixes` +methods are removed; callers use the corresponding default methods. This is a +public API break. The earlier addendum describes the historical behavior. + +The prior record reports a reproduced C++ defect; this change was not checked +against a locally built native reference. Its correctness is tested by state +invariants rather than reference parity.