Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
84fe979
fix build
maksymar May 26, 2025
1cdce5a
.
maksymar May 26, 2025
856e5fb
--persist
maksymar May 26, 2025
1ef691c
.
maksymar May 26, 2025
c447cbd
.
maksymar May 26, 2025
1a589d5
.
maksymar May 26, 2025
3e5105f
.
maksymar May 26, 2025
8cba39b
.
maksymar May 26, 2025
fada070
clippy
maksymar May 26, 2025
6d44b38
clippy
maksymar May 26, 2025
e8b77d6
fix build
maksymar May 26, 2025
04328a1
.
maksymar May 26, 2025
06f1ae8
cleanup
maksymar May 26, 2025
93a86e5
fix doc tests
maksymar May 26, 2025
3c35ba3
fix build
maksymar May 26, 2025
510f77f
Merge branch 'main' into maksym/into_bytes
maksymar May 26, 2025
38fa628
Merge branch 'main' into maksym/into_bytes
maksymar May 27, 2025
63ab429
merge main
maksymar May 27, 2025
819d53d
--persist
maksymar May 27, 2025
31cd9ce
merge main
maksymar May 28, 2025
11f4e59
fix build
maksymar May 28, 2025
5eceaed
--persist
maksymar May 28, 2025
2e21305
merge main
maksymar May 28, 2025
1617adc
--persist
maksymar May 28, 2025
f32fa75
Merge branch 'main' into maksym/into_bytes
maksymar Jun 5, 2025
70a0b66
--persist
maksymar Jun 5, 2025
006f232
merge main
maksymar Jun 5, 2025
495700c
--persist
maksymar Jun 5, 2025
d4c9264
merge main
maksymar Jun 6, 2025
1df5f11
--persist
maksymar Jun 6, 2025
6864a80
merge main
maksymar Jun 11, 2025
e1c3a8e
--persist
maksymar Jun 11, 2025
ede52aa
merge main
maksymar Jun 12, 2025
05a0b1e
--persist
maksymar Jun 12, 2025
aa365c3
merge main
maksymar Jun 12, 2025
799af70
--persist
maksymar Jun 12, 2025
a092423
fix comment
maksymar Jun 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 79 additions & 79 deletions benchmarks/btreemap/canbench_results.yml

Large diffs are not rendered by default.

176 changes: 88 additions & 88 deletions benchmarks/btreeset/canbench_results.yml

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions benchmarks/compare/canbench_results.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ benches:
read_chunks_btreemap_1:
total:
calls: 1
instructions: 1219162510
heap_increase: 3233
instructions: 799994339
heap_increase: 1635
stable_memory_increase: 1665
scopes: {}
read_chunks_btreemap_1k:
total:
calls: 1
instructions: 5415231039
heap_increase: 1604
instructions: 4994472213
heap_increase: 1602
stable_memory_increase: 1665
scopes: {}
read_chunks_btreemap_1m:
total:
calls: 1
instructions: 134599901409
instructions: 133159275732
heap_increase: 1892
stable_memory_increase: 3201
scopes: {}
Expand Down Expand Up @@ -44,42 +44,42 @@ benches:
read_chunks_vec_1:
total:
calls: 1
instructions: 1363286421
instructions: 1363286413
heap_increase: 3202
stable_memory_increase: 1665
scopes: {}
read_chunks_vec_1k:
total:
calls: 1
instructions: 1378474867
instructions: 1378477856
heap_increase: 3200
stable_memory_increase: 1665
scopes: {}
read_chunks_vec_1m:
total:
calls: 1
instructions: 4721968950
instructions: 4724968939
heap_increase: 3784
stable_memory_increase: 1665
scopes: {}
write_chunks_btreemap_1:
total:
calls: 1
instructions: 1069802998
heap_increase: 3233
instructions: 650634827
heap_increase: 1635
stable_memory_increase: 1665
scopes: {}
write_chunks_btreemap_1k:
total:
calls: 1
instructions: 4915151136
heap_increase: 1604
instructions: 4494392310
heap_increase: 1602
stable_memory_increase: 1665
scopes: {}
write_chunks_btreemap_1m:
total:
calls: 1
instructions: 90653823620
instructions: 89213197943
heap_increase: 1892
stable_memory_increase: 3201
scopes: {}
Expand Down Expand Up @@ -107,21 +107,21 @@ benches:
write_chunks_vec_1:
total:
calls: 1
instructions: 1257790953
instructions: 1257790959
heap_increase: 3202
stable_memory_increase: 1665
scopes: {}
write_chunks_vec_1k:
total:
calls: 1
instructions: 1272012661
instructions: 1272015664
heap_increase: 3200
stable_memory_increase: 1665
scopes: {}
write_chunks_vec_1m:
total:
calls: 1
instructions: 3712427298
instructions: 3715427301
heap_increase: 3784
stable_memory_increase: 1665
scopes: {}
Expand Down
10 changes: 10 additions & 0 deletions benchmarks/src/vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ impl<const N: usize> Storable for UnboundedVecN<N> {
Cow::Owned(self.0.clone())
}

#[inline]
fn into_bytes(self) -> Vec<u8> {
self.0
}

#[inline]
fn from_bytes(bytes: Cow<[u8]>) -> Self {
Self(bytes.into_owned())
Expand Down Expand Up @@ -75,6 +80,11 @@ impl<const N: usize> Storable for BoundedVecN<N> {
Cow::Owned(self.0.clone())
}

#[inline]
fn into_bytes(self) -> Vec<u8> {
self.0
}

#[inline]
fn from_bytes(bytes: Cow<[u8]>) -> Self {
Self(bytes.into_owned())
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/vec/canbench_results.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ benches:
vec_insert_u64:
total:
calls: 1
instructions: 5869519
instructions: 5359519
heap_increase: 0
stable_memory_increase: 1
scopes: {}
Expand Down
5 changes: 5 additions & 0 deletions docs/src/schema-upgrades.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ impl Storable for Asset {
Cow::Owned(bytes)
}

fn into_bytes(self) -> Vec<u8> {
let mut bytes = vec![];
ciborium::ser::into_writer(&self, &mut bytes).unwrap()
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
ciborium::de::from_reader(&*bytes).expect("deserialization must succeed.")
}
Expand Down
8 changes: 5 additions & 3 deletions examples/src/custom_types_example/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use candid::{CandidType, Decode, Deserialize, Encode};
use ic_stable_structures::memory_manager::{MemoryId, MemoryManager, VirtualMemory};
use ic_stable_structures::{
storable::Bound, DefaultMemoryImpl, StableBTreeMap, Storable,
};
use ic_stable_structures::{storable::Bound, DefaultMemoryImpl, StableBTreeMap, Storable};
use std::{borrow::Cow, cell::RefCell};

type Memory = VirtualMemory<DefaultMemoryImpl>;
Expand Down Expand Up @@ -30,6 +28,10 @@ impl Storable for UserProfile {
Cow::Owned(Encode!(self).unwrap())
}

fn into_bytes(self) -> Vec<u8> {
Encode!(&self).unwrap()
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
Decode!(bytes.as_ref(), Self).unwrap()
}
Expand Down
8 changes: 8 additions & 0 deletions examples/src/vecs_and_strings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ impl Storable for UserName {
self.0.to_bytes()
}

fn into_bytes(self) -> Vec<u8> {
self.0.into_bytes()
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
Self(String::from_bytes(bytes))
}
Expand All @@ -40,6 +44,10 @@ impl Storable for UserData {
self.0.to_bytes()
}

fn into_bytes(self) -> Vec<u8> {
self.0.into_bytes()
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
Self(<Vec<u8>>::from_bytes(bytes))
}
Expand Down
8 changes: 8 additions & 0 deletions fuzz/fuzz_targets/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ impl Storable for BoundedFuzzStruct {
Cow::Owned(serde_cbor::ser::to_vec(self).unwrap())
}

fn into_bytes(self) -> Vec<u8> {
serde_cbor::ser::to_vec(&self).unwrap()
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
let value: Self = serde_cbor::de::from_slice(bytes.as_ref()).unwrap();
value
Expand All @@ -41,6 +45,10 @@ impl Storable for UnboundedFuzzStruct {
Cow::Owned(serde_cbor::ser::to_vec(self).unwrap())
}

fn into_bytes(self) -> Vec<u8> {
serde_cbor::ser::to_vec(&self).unwrap()
}

fn from_bytes(bytes: std::borrow::Cow<[u8]>) -> Self {
let value: Self = serde_cbor::de::from_slice(bytes.as_ref()).unwrap();
value
Expand Down
24 changes: 21 additions & 3 deletions src/btreemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ const PAGE_SIZE_VALUE_MARKER: u32 = u32::MAX;
/// Cow::Owned(bytes)
/// }
///
/// fn into_bytes(self) -> Vec<u8> {
/// let mut bytes = Vec::new();
/// // TODO: Convert your struct to bytes...
/// bytes
/// }
///
/// fn from_bytes(bytes: Cow<[u8]>) -> Self {
/// // TODO: Convert bytes back to your struct
/// let (id, name) = (0, "".to_string());
Expand Down Expand Up @@ -492,7 +498,7 @@ where
/// key.to_bytes().len() <= max_size(Key)
/// value.to_bytes().len() <= max_size(Value)
pub fn insert(&mut self, key: K, value: V) -> Option<V> {
let value = value.to_bytes_checked().into_owned();
let value = value.into_bytes_checked();

let root = if self.root_addr == NULL {
// No root present. Allocate one.
Expand Down Expand Up @@ -1428,7 +1434,7 @@ mod test {

/// Encodes an object into a byte vector.
fn encode<T: Storable>(object: T) -> Vec<u8> {
object.to_bytes_checked().into_owned()
object.into_bytes_checked()
}

/// A helper method to succinctly create a blob.
Expand Down Expand Up @@ -2980,6 +2986,10 @@ mod test {
Cow::Borrowed(&[1, 2, 3, 4])
}

fn into_bytes(self) -> Vec<u8> {
self.to_bytes().into_owned()
}

fn from_bytes(_: Cow<[u8]>) -> Self {
unimplemented!();
}
Expand Down Expand Up @@ -3116,7 +3126,11 @@ mod test {
struct T;
impl Storable for T {
fn to_bytes(&self) -> Cow<[u8]> {
Cow::Owned(vec![1, 2, 3])
Cow::Borrowed(&[1, 2, 3])
}

fn into_bytes(self) -> Vec<u8> {
self.to_bytes().into_owned()
}

fn from_bytes(bytes: Cow<[u8]>) -> Self {
Expand All @@ -3138,6 +3152,10 @@ mod test {
Cow::Owned(vec![1, 2, 3])
}

fn into_bytes(self) -> Vec<u8> {
self.to_bytes().into_owned()
}

fn from_bytes(bytes: Cow<[u8]>) -> Self {
assert_eq!(bytes.to_vec(), vec![1, 2, 3]);
T2
Expand Down
4 changes: 4 additions & 0 deletions src/btreeset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ where
/// Cow::Owned(self.id.to_le_bytes().to_vec())
/// }
///
/// fn into_bytes(self) -> Vec<u8> {
/// self.id.to_le_bytes().to_vec()
/// }
///
/// fn from_bytes(bytes: Cow<[u8]>) -> Self {
/// let id = u64::from_le_bytes(bytes.as_ref().try_into().unwrap());
/// CustomType { id }
Expand Down
Loading