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

lang: Implement Key trait without to_account_info() calls #608

Closed
armaniferrante opened this issue Aug 14, 2021 · 5 comments · Fixed by #652
Closed

lang: Implement Key trait without to_account_info() calls #608

armaniferrante opened this issue Aug 14, 2021 · 5 comments · Fixed by #652
Labels
good first issue Good for newcomers help wanted Extra attention is needed lang

Comments

@armaniferrante
Copy link
Member

armaniferrante commented Aug 14, 2021

Currently the Key trait is implemented via a wasteful call to to_account_info which incurs a clone on the AccountInfo.

@armaniferrante armaniferrante added lang help wanted Extra attention is needed good first issue Good for newcomers labels Aug 14, 2021
@fanatid
Copy link
Contributor

fanatid commented Aug 14, 2021

I'm not sure how to do this properly, but with cargo-asm I see a clone with to_account_info().key (but was able to run only with bpf toolchain, not bpfel target).

Decide to count instructions with sol_log_compute_units:

*ctx.accounts.value.to_account_info().key == clock::ID; // 162
ctx.accounts.value.to_account_info().key == &clock::ID; // 162
*ctx.accounts.value.to_account_info().key == clock::id(); // 176
ctx.accounts.value.key() == clock::id(); // 190
*ctx.accounts.value.mykey() == clock::id(); // 95
*ctx.accounts.value.mykey() == clock::ID; // 82
ctx.accounts.value.mykey2() == clock::id(); // 105

with patched anchor:

impl<'info, T: solana_program::sysvar::Sysvar> Sysvar<'info, T> {
    pub fn mykey(&self) -> &Pubkey {
        self.info.key
    }
    pub fn mykey2(&self) -> Pubkey {
        *self.info.key
    }
}

I think to add ToAccountInfoRef trait and change Key trait to KeyRef with method key which return reference, sounds good?

@armaniferrante
Copy link
Member Author

I'm not sure how to do this properly, but with cargo-asm I see a clone with to_account_info().key (but was able to run only with bpf toolchain, not bpfel target).

Decide to count instructions with sol_log_compute_units:

*ctx.accounts.value.to_account_info().key == clock::ID; // 162
ctx.accounts.value.to_account_info().key == &clock::ID; // 162
*ctx.accounts.value.to_account_info().key == clock::id(); // 176
ctx.accounts.value.key() == clock::id(); // 190
*ctx.accounts.value.mykey() == clock::id(); // 95
*ctx.accounts.value.mykey() == clock::ID; // 82
ctx.accounts.value.mykey2() == clock::id(); // 105

with patched anchor:

impl<'info, T: solana_program::sysvar::Sysvar> Sysvar<'info, T> {
    pub fn mykey(&self) -> &Pubkey {
        self.info.key
    }
    pub fn mykey2(&self) -> Pubkey {
        *self.info.key
    }
}

I think to add ToAccountInfoRef trait and change Key trait to KeyRef with method key which return reference, sounds good?

Sounds great.

@fanatid
Copy link
Contributor

fanatid commented Aug 14, 2021

Looks like there is a nicer way to solve an issue... use AsRef. But we need it solana_program::account_info::AccountInfo 😞
If we will have AsRef we would able to write: constraint = ctx.accounts.cpi_account.as_ref().owner == spl_token::ID

@fanatid
Copy link
Contributor

fanatid commented Aug 14, 2021

Submitted pull request ⬆️
Current code in branch in my fork: https://github.com/fanatid/anchor/tree/cheaper-key

Waiting solana 1.7.11

@fanatid
Copy link
Contributor

fanatid commented Aug 28, 2021

1.7.11 is out, but not all required crates are published

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed lang
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants