Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Impl nth for External(Partial)Iterator #1838

Merged
merged 2 commits into from
Aug 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ and this project adheres to
- cosmwasm-vm: Use `wasmparser` for initial validation instead of `parity-wasm`
([#1786])
- cosmwasm-std: Make constructors `Decimal{,256}::{percent,permille,bps}` const
- cosmwasm-std: Use new `db_next_key` import to make `skip` and `nth`
implementation of `range` iterators more efficient. This requires the
`cosmwasm_1_4` feature to be enabled. ([#1838])

[#1667]: https://github.com/CosmWasm/cosmwasm/pull/1667
[#1674]: https://github.com/CosmWasm/cosmwasm/pull/1674
Expand All @@ -46,6 +49,7 @@ and this project adheres to
[#1701]: https://github.com/CosmWasm/cosmwasm/pull/1701
[#1786]: https://github.com/CosmWasm/cosmwasm/pull/1786
[#1793]: https://github.com/CosmWasm/cosmwasm/pull/1793
[#1838]: https://github.com/CosmWasm/cosmwasm/pull/1838

## [1.3.3] - 2023-08-22

Expand Down
32 changes: 32 additions & 0 deletions packages/std/src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ struct ExternalPartialIterator {
impl Iterator for ExternalPartialIterator {
type Item = Vec<u8>;

/// The default implementation calls `next` repeatedly,
/// which we can do a little more efficiently by using `db_next_key` instead.
/// It is used by `skip`, so it allows cheaper skipping.
#[cfg(feature = "cosmwasm_1_4")]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
skip_iter(self.iterator_id, n);
self.next()
}

fn next(&mut self) -> Option<Self::Item> {
// here we differentiate between the two types
let next_result = match self.partial_type {
Expand Down Expand Up @@ -229,6 +238,15 @@ struct ExternalIterator {
impl Iterator for ExternalIterator {
type Item = Record;

/// The default implementation calls `next` repeatedly,
/// which we can do a little more efficiently by using `db_next_key` instead.
/// It is used by `skip`, so it allows cheaper skipping.
#[cfg(feature = "cosmwasm_1_4")]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
skip_iter(self.iterator_id, n);
self.next()
}

fn next(&mut self) -> Option<Self::Item> {
let next_result = unsafe { db_next(self.iterator_id) };
let kv_region_ptr = next_result as *mut Region;
Expand All @@ -242,6 +260,20 @@ impl Iterator for ExternalIterator {
}
}

/// Helper function to skip `count` elements of an iterator.
#[cfg(all(feature = "iterator", feature = "cosmwasm_1_4"))]
fn skip_iter(iter_id: u32, count: usize) {
for _ in 0..count {
let region = unsafe { db_next_key(iter_id) };
if region == 0 {
// early return
return;
}
// just deallocate the region
unsafe { consume_region(region as *mut Region) };
}
}

/// A stateless convenience wrapper around imports provided by the VM
#[derive(Copy, Clone)]
pub struct ExternalApi {}
Expand Down