Crates.io | cow_arc |
lib.rs | cow_arc |
version | 0.2.2 |
source | src |
created_at | 2021-02-01 07:25:03.454142 |
updated_at | 2021-03-17 06:55:25.091362 |
description | CowArc can be useful for decreasing memory allocations by sharing immutable memory. |
homepage | https://github.com/DevNulPavel/cow_arc |
repository | https://github.com/DevNulPavel/cow_arc |
max_upload_size | |
id | 349165 |
size | 7,277 |
CowArc can be useful for decreasing memory allocations by sharing immutable memory.
It saves some RAM by sharing immutable values between CowArc clones. Memory allocates only in case of changing value.
CowArc can be usefull for creating builders.
let source = CowArc::new(vec![1, 2, 3]);
// Still shared memory
let mut changed = cloned.clone();
assert!(std::ptr::eq(source.deref(), changed.deref()) == true);
assert!(changed.eq(&vec![1, 2, 3]));
// New memory allocation
changed.set_val(vec![1, 2, 3, 4]);
assert!(std::ptr::eq(source.deref(), changed.deref()) == false);
assert!(changed.eq(&vec![1, 2, 3, 4]));
let source = CowArc::new(vec![1, 2, 3]);
// Still shared memory
let mut updated = source.clone();
assert!(std::ptr::eq(source.deref(), updated.deref()) == true);
assert!(changed.eq(&vec![1, 2, 3]));
// New memory allocation
updated.update_val(|val|{
val.push(4);
});
assert!(std::ptr::eq(source.deref(), updated.deref()) == false);
assert!(updated.eq(&vec![1, 2, 3, 4]));