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

refactor!: avoid cloning storage prefixes #92

Merged
merged 2 commits into from
Mar 13, 2023
Merged

refactor!: avoid cloning storage prefixes #92

merged 2 commits into from
Mar 13, 2023

Conversation

mooori
Copy link
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
@@ -25,7 +25,7 @@ pub trait Ownable {
/// #[ownable(owner_storage_key="CUSTOM_KEY")]
/// struct Contract { /* ... */}
/// ```
fn owner_storage_key(&self) -> Vec<u8>;
fn owner_storage_key(&self) -> &'static [u8];
Copy link

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
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

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
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
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