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

Add remove method to Mapping #1023

Merged
merged 9 commits into from
Nov 29, 2021
Merged

Conversation

HCastano
Copy link
Contributor

While updating one of the example contracts I realized that this would be a useful method
to have.

crates/storage/src/lazy/mapping.rs Show resolved Hide resolved
crates/storage/src/lazy/mapping.rs Outdated Show resolved Hide resolved
@paritytech-ci
Copy link

paritytech-ci commented Nov 18, 2021

🦑 📈 ink! Example Contracts ‒ Size Change Report 📉 🦑

These are the results of building the examples/* contracts from this branch with cargo-contract 0.16.0:

Δ Original Size Δ Optimized Size Total Optimized Size
accumulator 26.75 K
adder 28.39 K
contract-terminate 20.03 K
contract-transfer 28.02 K
delegator 32.57 K
dns 51.79 K
erc1155 88.03 K
erc20 +0.06 K +0.06 K 34.37 K
erc721 84.63 K
flipper 20.95 K
incrementer 20.72 K
multisig 105.32 K
proxy 23.86 K
rand-extension 26.52 K
subber 28.39 K
trait-erc20 64.41 K
trait-flipper 20.67 K
trait-incrementer 20.69 K

Link to the run | Last update: Mon Nov 29 15:58:17 CET 2021

@codecov-commenter
Copy link

codecov-commenter commented Nov 25, 2021

Codecov Report

Merging #1023 (303d30f) into master (141673b) will decrease coverage by 3.16%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1023      +/-   ##
==========================================
- Coverage   78.78%   75.62%   -3.17%     
==========================================
  Files         248      248              
  Lines        9363     9367       +4     
==========================================
- Hits         7377     7084     -293     
- Misses       1986     2283     +297     
Impacted Files Coverage Δ
crates/storage/src/lazy/mapping.rs 100.00% <100.00%> (ø)
crates/lang/ir/src/ir/contract.rs 0.00% <0.00%> (-100.00%) ⬇️
crates/lang/ir/src/ir/ink_test.rs 0.00% <0.00%> (-87.50%) ⬇️
crates/lang/ir/src/ir/trait_def/mod.rs 0.00% <0.00%> (-66.67%) ⬇️
crates/lang/ir/src/ir/blake2.rs 16.66% <0.00%> (-62.50%) ⬇️
crates/lang/ir/src/ir/trait_def/item/trait_item.rs 45.31% <0.00%> (-43.75%) ⬇️
crates/lang/ir/src/ir/trait_def/config.rs 7.69% <0.00%> (-38.47%) ⬇️
crates/lang/ir/src/ir/utils.rs 53.84% <0.00%> (-30.77%) ⬇️
crates/lang/ir/src/ir/item_mod.rs 62.28% <0.00%> (-27.43%) ⬇️
crates/lang/ir/src/ir/item_impl/mod.rs 66.94% <0.00%> (-23.73%) ⬇️
... and 15 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 141673b...303d30f. Read the comment docs.

@@ -164,6 +180,45 @@ const _: () = {
mod tests {
use super::*;

/// A dummy type which `REQUIRES_DEEP_CLEAN_UP`
#[derive(PartialEq, Debug, scale::Encode, scale::Decode)]
struct DeepClean<T>(T);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wasn't able to find any of the "default" types that we implement SpreadLayout for which had REQUIRES_DEEP_CLEAN_UP set to true. Let me know if there is such a type so I can use that instead

Copy link
Collaborator

Choose a reason for hiding this comment

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

ink_storage::Box is an example.

Generally ink_storage::Box is the interface to work with SpreadLayout types from within PackedLayout cells.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried using a Box, but it uses the REQUIRES_DEEP_CLEAN_UP of T, which are all false for primitives types as far as I could tell

Copy link
Collaborator

Choose a reason for hiding this comment

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

You usually use ink_storage::Box with SpreadLayout types that do nit implement PackedLayout. For example other ink_storage::collections types such as ink_storage::Vec.

@HCastano HCastano changed the title Add remove method to Mapping Add clear_entry method to Mapping Nov 25, 2021
}

/// Clears the value at `key` from storage.
pub fn clear_entry<Q>(&self, key: Q)
Copy link
Collaborator

@cmichi cmichi Nov 26, 2021

Choose a reason for hiding this comment

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

Suggested change
pub fn clear_entry<Q>(&self, key: Q)
pub fn remove<Q>(&self, key: Q)

Le'ts name it remove. The term entry doesn't appear anywhere else in Mapping (there's also no get_entry, no insert_entry). It can also be misleading, since we have an entry API for some data structures.

You'll have to adapt the other occurrences as well.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I too think that remove as a name is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I want to reserve remove in case we want to add an analogue to HashMap/BTreeMap::remove. Although I guess we can also use take for that. What do you guys think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree. I'm wondering, do you think you'll get away without take for the examples?

Copy link
Contributor Author

@HCastano HCastano Nov 26, 2021

Choose a reason for hiding this comment

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

So far I haven't needed it, but I've only gone through ERC20, ERC1155, and just started ERC721. Either way, its behaviour can be replicated by doing get followed by clear_entry (or whatever we name it)

@@ -164,6 +180,45 @@ const _: () = {
mod tests {
use super::*;

/// A dummy type which `REQUIRES_DEEP_CLEAN_UP`
#[derive(PartialEq, Debug, scale::Encode, scale::Decode)]
struct DeepClean<T>(T);
Copy link
Collaborator

Choose a reason for hiding this comment

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

ink_storage::Box is an example.

Generally ink_storage::Box is the interface to work with SpreadLayout types from within PackedLayout cells.

crates/storage/src/lazy/mapping.rs Outdated Show resolved Hide resolved
@Robbepop Robbepop changed the title Add clear_entry method to Mapping Add remove method to Mapping Nov 29, 2021
@HCastano HCastano merged commit 139cd05 into master Nov 29, 2021
@HCastano HCastano deleted the hc-add-reduce-method-to-mapping branch November 29, 2021 19:01
xgreenx pushed a commit to Supercolony-net/ink that referenced this pull request Feb 8, 2022
* Add `remove` method to `Mapping`

* Avoid loading entries during clean-up if no deep clean is required

* Change API to just clear storage instead of also returning value

* Reduce trait bound to just `EncodeLike`

* Rename `clear_entry` back to `remove`

* Replace dummy `DeepClean` type with `Pack`

* RustFmt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants