From 979aea3f34991cfd8d33e3cd3a1c9c521b83f1f3 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Wed, 12 Aug 2026 23:55:08 +0200 Subject: [PATCH 1/3] Simplify duplicate detection in IdOrdMap::insert_unique_impl --- crates/iddqd/src/id_ord_map/imp.rs | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/crates/iddqd/src/id_ord_map/imp.rs b/crates/iddqd/src/id_ord_map/imp.rs index 6fd02cb5..eca44df3 100644 --- a/crates/iddqd/src/id_ord_map/imp.rs +++ b/crates/iddqd/src/id_ord_map/imp.rs @@ -13,7 +13,6 @@ use crate::{ map_hash::MapHash, }, }; -use alloc::collections::BTreeSet; use core::{ fmt, hash::{BuildHasher, Hash}, @@ -1468,8 +1467,6 @@ impl IdOrdMap { &mut self, value: T, ) -> Result> { - let mut duplicates = BTreeSet::new(); - // Check for duplicates *before* inserting the new item, because we // don't want to partially insert the new item and then have to roll // back. @@ -1477,19 +1474,16 @@ impl IdOrdMap { // Scope this `key` to avoid lifetime issues. { let key = value.key(); - if let Some(index) = self + let duplicate: Option = self .tables .key_to_item - .find_index(&key, |index| self.items[index].key()) - { - duplicates.insert(index); - } + .find_index(&key, |index| self.items[index].key()); - if !duplicates.is_empty() { + if let Some(index) = duplicate { drop(key); return Err(DuplicateItem::__internal_new( value, - duplicates.iter().map(|ix| &self.items[*ix]).collect(), + vec![&self.items[index]], )); } } From f680dbc5c99002f9b9e5d725f48935c0280afda4 Mon Sep 17 00:00:00 2001 From: Ivan Zuzak Date: Thu, 13 Aug 2026 13:01:54 +0200 Subject: [PATCH 2/3] Simplify duplicate detection in IdHashMap::insert_unique_impl --- crates/iddqd/src/id_hash_map/imp.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/iddqd/src/id_hash_map/imp.rs b/crates/iddqd/src/id_hash_map/imp.rs index ba271403..e194dbb3 100644 --- a/crates/iddqd/src/id_hash_map/imp.rs +++ b/crates/iddqd/src/id_hash_map/imp.rs @@ -15,7 +15,6 @@ use crate::{ map_hash::MapHash, }, }; -use alloc::collections::BTreeSet; use core::{ fmt, hash::{BuildHasher, Hash}, @@ -1508,7 +1507,7 @@ impl IdHashMap { &mut self, value: T, ) -> Result> { - let mut duplicates = BTreeSet::new(); + let mut duplicate: Option = None; // Check for duplicates *before* inserting the new item, because we // don't want to partially insert the new item and then have to roll @@ -1522,16 +1521,16 @@ impl IdHashMap { .entry(state, key, |index| self.items[index].key()) { hash_table::Entry::Occupied(slot) => { - duplicates.insert(slot.get()); + duplicate = Some(slot.get()); None } hash_table::Entry::Vacant(slot) => Some(slot), }; - if !duplicates.is_empty() { + if let Some(index) = duplicate { return Err(DuplicateItem::__internal_new( value, - duplicates.iter().map(|ix| &self.items[*ix]).collect(), + vec![&self.items[index]], )); } From 80e324b66271669ce08167ae5ae4a6cf809f0da9 Mon Sep 17 00:00:00 2001 From: Rain Date: Tue, 18 Aug 2026 12:05:27 -0700 Subject: [PATCH 3/3] simplify a bit --- crates/iddqd/src/id_hash_map/imp.rs | 20 +++++++------------- crates/iddqd/src/id_ord_map/imp.rs | 7 +++---- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/crates/iddqd/src/id_hash_map/imp.rs b/crates/iddqd/src/id_hash_map/imp.rs index e194dbb3..5955a0f6 100644 --- a/crates/iddqd/src/id_hash_map/imp.rs +++ b/crates/iddqd/src/id_hash_map/imp.rs @@ -1507,8 +1507,6 @@ impl IdHashMap { &mut self, value: T, ) -> Result> { - let mut duplicate: Option = None; - // Check for duplicates *before* inserting the new item, because we // don't want to partially insert the new item and then have to roll // back. @@ -1521,21 +1519,17 @@ impl IdHashMap { .entry(state, key, |index| self.items[index].key()) { hash_table::Entry::Occupied(slot) => { - duplicate = Some(slot.get()); - None + let index = slot.get(); + return Err(DuplicateItem::__internal_new( + value, + vec![&self.items[index]], + )); } - hash_table::Entry::Vacant(slot) => Some(slot), + hash_table::Entry::Vacant(slot) => slot, }; - if let Some(index) = duplicate { - return Err(DuplicateItem::__internal_new( - value, - vec![&self.items[index]], - )); - } - let next_index = self.items.assert_can_grow().insert(value); - entry.unwrap().insert(next_index); + entry.insert(next_index); Ok(next_index) } diff --git a/crates/iddqd/src/id_ord_map/imp.rs b/crates/iddqd/src/id_ord_map/imp.rs index eca44df3..b5332610 100644 --- a/crates/iddqd/src/id_ord_map/imp.rs +++ b/crates/iddqd/src/id_ord_map/imp.rs @@ -1474,12 +1474,11 @@ impl IdOrdMap { // Scope this `key` to avoid lifetime issues. { let key = value.key(); - let duplicate: Option = self + if let Some(index) = self .tables .key_to_item - .find_index(&key, |index| self.items[index].key()); - - if let Some(index) = duplicate { + .find_index(&key, |index| self.items[index].key()) + { drop(key); return Err(DuplicateItem::__internal_new( value,