Skip to content

feat(vec): add extend() method for batch element insertion - #419

Open
gabrielrondon wants to merge 1 commit into
dfinity:mainfrom
gabrielrondon:feat/vec-extend
Open

gabrielrondon wants to merge 1 commit into
dfinity:mainfrom
gabrielrondon:feat/vec-extend

Conversation

@gabrielrondon

Copy link
Copy Markdown

Summary

  • Add BaseVec::extend(&[T]) and Vec::extend(&[T]) for appending multiple items in a single operation
  • Writes all items sequentially and updates the length once, reducing stable memory writes for the length field compared to repeated push()
  • Follows the same pattern as push(): serialize each item, write entry size, write data, then update length at the end
  • Includes unit tests for empty slice, basic usage, extend after push, multiple extends, and persistence across init/reinit

Closes #349

@gabrielrondon
gabrielrondon requested a review from a team as a code owner March 23, 2026 23:47
@cla-idx-bot

cla-idx-bot Bot commented Mar 23, 2026

Copy link
Copy Markdown

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
@gabrielrondon
gabrielrondon requested a review from a team as a code owner November 16, 2026 15:52
Copilot AI balanced review requested due to automatic review settings November 16, 2026 15:52
@gabrielrondon

Copy link
Copy Markdown
Author

Rebased on main. Happy to change the signature to take impl IntoIterator<Item = T> instead of &[T] if that fits the crate better.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

Comment thread src/base_vec.rs
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())?;

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add StableVec::extend method

2 participants