stable-structures icon indicating copy to clipboard operation
stable-structures copied to clipboard

Add StableVec::extend method

Open ranile opened this issue 6 months ago • 1 comments

Currently the only way to extend a StableVec with another vector or slice is repeatedly calling push:

let stable_vec = StableVec::<u8, _>::init(mem).expect("Memory was not for stable vec");

let bytes = bincode::encode_to_vec(value, BINCODE_CONFIG).expect("Failed to serialize value");
for byte in bytes {
    stable_vec.push(&byte).expect("StableVec push failed");
}

The above code is very memory inefficient as it requires running the entire push code for every element (including growing memory!).

A similar method already exists for the std Vec: Vec::extend

ranile avatar Jun 18 '25 12:06 ranile

Growing the memory is amortized, so I'm not sure if this would result in a significant performance improvement. But we'd be open to the idea if you want to try it out.

adambratschikaye avatar Jun 23 '25 09:06 adambratschikaye