coral-xyz / anchor

⚓ Solana Sealevel Framework
https://anchor-lang.com
Apache License 2.0
3.36k stars 1.25k forks source link

Perf: Account Exit routine for ZeroCopy/AccountLoader is redundantly called #2935

Open nabeel99 opened 2 months ago

nabeel99 commented 2 months ago

Intro:

I have been diving deep in to the anchor lang/proc macro internals to optimise for another stack space related feature, during that i found a redundancy related to the calling of AccountExit exit method at the end of the instruction handler.

Problem:

Zero Copy Accounts are casted via bytemuck and as such no serialization framework is used, the only time anything that is written explicitly is the discriminator by the anchor lang framework, but the issue is this needs to be done once, the account exit impl of Account Loader : https://docs.rs/anchor-lang/latest/src/anchor_lang/accounts/account_loader.rs.html#236-248, writes the discriminator /overwrite exisitng discriminator everytime its called

Potential Solution :

Generate the account exit or call the accountloader exit impl when constraint #account(zero)] is called otherwise in side the parent derive account exit impl dont call the account loader exit function, this will help save on compute units.

acheroncrypto commented 2 months ago

Thanks for creating this issue. Yes, this has been a known issue for a while and should be fixed.

Happy to review PRs if you'd like to work on this.

nabeel99 commented 2 months ago

Thanks for creating this issue. Yes, this has been a known issue for a while and should be fixed.

Happy to review PRs if you'd like to work on this.

Hey thanks for confirming the issue, happy to send in a pr addressing this and the stack space issue soon.

ashantilagos commented 1 month ago

Happy to review PR if need be

nabeel99 commented 1 month ago

Happy to review PR if need be

Alright will cook it up soon.