anchor icon indicating copy to clipboard operation
anchor copied to clipboard

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

Open nabeel99 opened this issue 9 months ago • 4 comments

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.

nabeel99 avatar Apr 27 '24 07:04 nabeel99

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.

acheroncrypto avatar Apr 27 '24 16:04 acheroncrypto

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.

nabeel99 avatar Apr 27 '24 16:04 nabeel99

Happy to review PR if need be

ashantilagos avatar May 16 '24 19:05 ashantilagos

Happy to review PR if need be

Alright will cook it up soon.

nabeel99 avatar May 19 '24 20:05 nabeel99