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

Optimization: Eliminate double-read when manipulating Option on storage #507

Open
cmichi opened this issue Oct 5, 2020 · 2 comments
Open
Labels
A-ink_storage [ink_storage] Work Item B-research Research task that has open questions that need to be resolved. C-discussion An issue for discussion for a given topic.

Comments

@cmichi
Copy link
Collaborator

cmichi commented Oct 5, 2020

When loading an object from storage we have an overhead of one read operation.

See here:

    // In case the contract storage is occupied we handle
    // the Option<T> as if it was a T.
    ink_env::get_contract_storage::<()>(root_key)
        .ok()
        .flatten()
        .map(|_| super::pull_spread_root::<T>(root_key))

We read the root_key two times from storage.

At the moment there is no API to ask Seal if a storage is occupied, so instead we have to issue the first read in order to find out if it's None or Some(…). In the latter case we pull recursively from storage.

Possible Solution

We probably have to adapt the SpreadLayout interface, since we have to cache those reads ourselves. An option could be to implement caching into KeyPtr.

@cmichi cmichi added A-ink_storage [ink_storage] Work Item C-discussion An issue for discussion for a given topic. labels Oct 5, 2020
@Robbepop
Copy link
Collaborator

Robbepop commented Oct 5, 2020

The special challenge behind this is to find a solution that optimally works without using heap memory allocation so that simpler contracts can still get rid of the heap allocator. This is very important since this machinery is very deeply rooted in all ink! smart contracts.
We are not without weapons here since we can make use of the ScopeBuffer internals of our ink_env on-chain environment. For off-chain we do not care about heap allocations whatsoever.

@Robbepop Robbepop changed the title Optimization: Reduce read overhead when loading from storage Optimization: Reduce read overhead when loading Option from storage Oct 8, 2020
@Robbepop Robbepop changed the title Optimization: Reduce read overhead when loading Option from storage Optimization: Eliminate double-read when reading Option from storage Oct 8, 2020
@Robbepop Robbepop changed the title Optimization: Eliminate double-read when reading Option from storage Optimization: Eliminate double-read when manipulating Option from storage Oct 8, 2020
@Robbepop Robbepop changed the title Optimization: Eliminate double-read when manipulating Option from storage Optimization: Eliminate double-read when manipulating Option on storage Oct 8, 2020
@Robbepop Robbepop added the B-research Research task that has open questions that need to be resolved. label Nov 3, 2020
@cmichi
Copy link
Collaborator Author

cmichi commented Aug 2, 2021

I've removed this ticket from Substrate's "Improve throughput" issue, since this issue here rather targets the runtime performance by having one storage read less. The "Improve throughput" issue on the other hand aims to reduce the size of contracts. The ideas we've had so far to solve this very issue here involve more code (i.e. caching) though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ink_storage [ink_storage] Work Item B-research Research task that has open questions that need to be resolved. C-discussion An issue for discussion for a given topic.
Projects
None yet
Development

No branches or pull requests

2 participants