Skip to content

refactor!: avoid cloning storage prefixes#92

Merged
mooori merged 2 commits into
masterfrom
avoid-clones-2
Mar 13, 2023
Merged

refactor!: avoid cloning storage prefixes#92
mooori merged 2 commits into
masterfrom
avoid-clones-2

Conversation

@mooori
Copy link
Copy Markdown
Contributor

@mooori mooori commented Mar 13, 2023

Some functions that return storage prefixes returned Vec<u8> while &'static [u8] would suffice. This PR changes these functions to return the latter. This avoids unnecessary clones as pointed out in this external issue.

Breaking changes

The return type changes from Vec<u8> to &'static [u8] for

  • Ownable::owner_storage_key
  • Pausable::pa_storage_key

The bytes which are returned do not change.

@mooori mooori marked this pull request as ready for review March 13, 2023 12:01
@mooori mooori requested a review from birchmd March 13, 2023 12:01
/// struct Contract { /* ... */}
/// ```
fn owner_storage_key(&self) -> Vec<u8>;
fn owner_storage_key(&self) -> &'static [u8];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I wonder if we should use &[u8] instead of enforcing a static lifetime. I can't really think of any, but maybe there is some use case where the storage key is not known at compile-time for some reason?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@birchmd Can we merge this as is in order to fix the external issue? I would suggest addressing the change to return &[u8] in a separate issue/PR due to the issue described below.


Returning &[u8] is the better option and it works for all traits except AccessControllable. For the other traits, the function which returns the storage prefix has parameter &self, so returning &[u8] (without lifetime specifier) is fine.

As of now, AccessControllable::acl_storage_prefix cannot take &self due to this usage of Default::default(). Therefore the return value of acl_storage_prefix must have a lifetime specifier. I think we have the following options:

  • Refactor the default implementation of AccessControllable to not rely on Default. Then return &[u8] from all trait methods that return the storage prefix.
  • Migrate the other traits to return &[u8] and accept AccessControllable as an outlier whose storage prefix function returns &'static [u8].
  • Defer this change until a use case pops up where returning &'static [u8] is not possible.

What do you think?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Sure, we can merge this as-is. I think doing the refactor of AccessControllable so that all traits can use &[u8] is probably the "right" thing to do, but without a clear use-case it's low priority to actually do it. For I think it's fine to file an issue here on GitHub that this is something we might want to do in the future (or put out a bounty for).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Tracked by issue #93.

@mooori mooori merged commit 8920f9a into master Mar 13, 2023
@mooori mooori deleted the avoid-clones-2 branch March 13, 2023 15:36
This was referenced Mar 27, 2026
This was referenced Apr 7, 2026
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.

3 participants