Repository navigation
feat(vec): add extend() method for batch element insertion - #419
gabrielrondon wants to merge 1 commit into
Conversation
|
Dear @gabrielrondon, In order to potentially merge your code in this open-source repository and therefore proceed with your contribution, we need to have your approval on DFINITY's CLA. If you decide to agree with it, please visit this issue and read the instructions there. Once you have signed it, re-trigger the workflow on this PR to see if your code can be merged. — The DFINITY Foundation |
Add `BaseVec::extend()` and `Vec::extend()` for appending multiple items in a single operation. Writes all items sequentially and updates the length once, reducing stable memory writes compared to repeated push(). Closes dfinity#349
d0d32dc to
c3fb0e3
Compare
|
Rebased on main. Happy to change the signature to take |
There was a problem hiding this comment.
🟡 Changes recommended
The tests omit mid-batch extend failures, leaving preservation of the original state after a panic and reinitialization, and correct overwriting of unpublished entries by a later append, unverified.
1 open issue
What changed in this PR
Adds slice-based batch insertion to stable vectors, addressing #349 by reducing length-field writes compared with repeated push() calls.
Changes:
- Adds
BaseVec::extend(&[T]), writing entries sequentially and updating the length once after success. - Exposes
Vec::extend(&[T])with panic-based growth-failure handling. - Adds tests for empty batches, appending after
push, repeated calls, and persistence across reinitialization.
| File | Description |
|---|---|
src/vec/tests.rs |
Tests successful batch insertion and persistence. |
src/vec.rs |
Exposes the public batch insertion method. |
src/base_vec.rs |
Implements batch writes with a deferred length update. |
Review effort: Balanced
| let offset = DATA_OFFSET + slot * (start_index + i as u64); | ||
| let bytes = item.to_bytes_checked(); | ||
| let data_offset = self.write_entry_size(offset, bytes.len() as u32)?; | ||
| safe_write(&self.memory, data_offset, bytes.borrow())?; |

Summary
BaseVec::extend(&[T])andVec::extend(&[T])for appending multiple items in a single operationpush()push(): serialize each item, write entry size, write data, then update length at the endCloses #349