Crates.io | dup-indexer |
lib.rs | dup-indexer |
version | 0.4.0 |
source | src |
created_at | 2023-04-04 21:57:54.903049 |
updated_at | 2023-11-27 03:17:34.952008 |
description | Create a non-duplicated index from Strings, static str, Vec, or Box values |
homepage | |
repository | https://github.com/nyurik/dup-indexer |
max_upload_size | |
id | 830485 |
size | 41,637 |
Create a non-duplicated vector of values without extra memory allocations, even for ref values like String
and Vec
. Each insertion returns the usize
index of the inserted value. When done, the entire vector can be used.
This approach is useful for creating a vector of unique values, such as a list of unique strings, or a list of unique objects, and then using the index of the value in the vector as a unique identifier, e.g. in a protobuf message.
There are two objects in this crate:
DupIndexer<T>
- use insert(value: T)
to add values, where value
ownership is moved into the indexer on each call. This is good for when the value is no longer needed after insertion, or for values implementing Copy
.DupIndexerRefs<T: Deref>
- use insert_owned(value: T)
and/or insert_ref(value: &T::Target)
, to either insert with ownership transfer (just like DupIndexer
), or to insert by reference, and only clone the value if it does not already exist in the index. This only works for String/&str
pair, or can be implemented for custom types.use dup_indexer::{DupIndexer, DupIndexerRefs};
fn main() {
let mut di = DupIndexerRefs::new();
assert_eq!(di.insert_owned("hello".to_string()), 0);
assert_eq!(di.insert_ref("world"), 1);
assert_eq!(di.insert_ref("hello"), 0);
assert_eq!(di.into_vec(), vec!["hello", "world"]);
// with strings
let mut di = DupIndexer::new();
assert_eq!(di.insert("hello".to_string()), 0);
assert_eq!(di.insert("world".to_string()), 1);
assert_eq!(di.insert("hello".to_string()), 0);
assert_eq!(di.into_vec(), vec!["hello", "world"]);
// with i32
let mut di = DupIndexer::with_capacity(10);
assert_eq!(di.insert(42), 0);
assert_eq!(di.insert(13), 1);
assert_eq!(di.insert(42), 0);
assert_eq!(di[1], 13); // use it as a read-only vector
assert_eq!(di.into_iter().collect::<Vec<_>>(), vec![42, 13]);
//
// with custom enum
//
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
enum Value {
Str(String),
Int(i32),
}
// All values inside the Value enum implement PtrRead
unsafe impl dup_indexer::PtrRead for Value {}
let mut di: DupIndexer<Value> = DupIndexer::new();
assert_eq!(di.insert(Value::Str("foo".to_string())), 0);
assert_eq!(di.insert(Value::Int(42)), 1);
assert_eq!(di.insert(Value::Str("foo".to_string())), 0);
assert_eq!(di[1], Value::Int(42));
assert_eq!(
di.into_vec(),
vec![Value::Str("foo".to_string()), Value::Int(42)]
);
}
DupIndexer keeps inserted values in a vector in the order of insertion. It also tracks inserted values in a lookup HashMap<T, usize>
where T
is the type of the inserted value. This means that the inserted values must implement Hash
and Eq
.
The value types like ints, floats, bools, chars and any references like &str
cause no issues because they can be copied to both the vector and the lookup map containers. However, the non-copyable types with memory allocation like String
and Vec
cannot be owned by both containers at the same time. To solve this, DupIndexer creates a shallow non-droppable copy of the value, and stores it in the hashmap, whereas the original value goes into the vector:
use std::collections::hash_map::{Entry, HashMap};
use std::mem::ManuallyDrop;
use std::hash::Hash;
pub unsafe trait PtrRead {}
pub struct DupIndexer<T> {
values: Vec<T>,
lookup: HashMap<ManuallyDrop<T>, usize>,
}
impl<T: Hash + Eq + PtrRead> DupIndexer<T> {
pub fn insert(&mut self, value: T) -> usize {
let dup_value = ManuallyDrop::new(unsafe { std::ptr::read(&value) });
match self.lookup.entry(dup_value) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let index = self.values.len();
entry.insert(index);
self.values.push(value);
index
}
}
}
}
This way, the hashmap owns the shallow copy, and the vector owns the original value. On subsequent calls, the new value is checked against the hashmap for duplicates. Once finished, the vector with the keys is consumed by the user with .into_vec()
, and the hashmap is dropped without dropping the actual keys.
The DupIndexerRefs
assumes it is safe because the result of a String
dereferencing (&str
) is valid as long as the String
itself is not modified, even if the String
objects are stored in a Vec<String>
which may be resized.
Similarly, DupIndexer
is safe because the hashmap only keeps the ptr:read
-created copy of the original value while we own it, and the value is never modified. Some types like Box
might be a bit trickier, so for safety / to keep Miri happy this lib has an unsafe PtrRead
marker trait that most basic types implement.
Miri passes all tests, but fails if T
is a Box<i32>
. When the test tries to insert a duplicate value, I see the following Miri warning. Do let me know if you know if this is really an issue and how to fix this.
❯ cargo +nightly miri test
--> .../.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1328:23
|
1328 | PartialEq::eq(&**self, &**other)
| ^^^^^^^
| |
| trying to retag from <170684> for SharedReadOnly permission at alloc64636[0x0], but that tag does not exist in the borrow stack for this location
| this error occurs as part of retag at alloc64636[0x0..0x4]
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
help: <170684> was created by a Unique retag at offsets [0x0..0x4]
--> src/lib.rs:49:17
|
49 | entry.insert(index);
| ^^^^^^^^^^^^^^^^^^^
help: <170684> was later invalidated at offsets [0x0..0x4] by a Unique retag
--> src/lib.rs:50:34
|
50 | self.values.push(value);
| ^^^^^
make
. Install it with cargo install just
.just
.just test
.git push
, it will run a few validations, including cargo fmt
, cargo clippy
, and cargo test
. Use git push --no-verify
to skip these checks.just bench
.just miri
(note that one of the tests is disabled due to the above issue).Licensed under either of
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.