Skip to content

Optimize enums with all unit variants and empty arrays with Lazy#4237

Merged
acheroncrypto merged 2 commits into
otter-sec:masterfrom
jamie-osec:lazy-sized-fixes
Mar 8, 2026
Merged

Optimize enums with all unit variants and empty arrays with Lazy#4237
acheroncrypto merged 2 commits into
otter-sec:masterfrom
jamie-osec:lazy-sized-fixes

Conversation

@jamie-osec

Copy link
Copy Markdown
Collaborator

Best reviewed commit-by-commit. Fixes some generics-related issues in the lazy derive, as well as making the SIZED counter more accurate to allow a few more fast-paths.
Finally, enables many lang features for unit/doc/integration tests in CI.

@vercel

vercel Bot commented Feb 12, 2026

Copy link
Copy Markdown

@jamie-osec is attempting to deploy a commit to the Solana Foundation Team on Vercel.

A member of the Team first needs to authorize it.

@jamie-osec jamie-osec force-pushed the lazy-sized-fixes branch 2 times, most recently from 25217ef to 25e59c2 Compare February 12, 2026 19:30
@jamie-osec jamie-osec changed the title Collection of tweaks to Lazy Collection of fixes to Lazy Feb 12, 2026

@acheroncrypto acheroncrypto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It would be better to have separate PRs for optimizations and generics impl since they are mostly unrelated and don't depend on each other. Among many other benefits, this would allow clear and concise PR titles such as

  • Optimize enums with all unit variants in Lazy
  • Add generics support to Lazy (to me, this is more of a feature than a fix because the impl wasn't broken; it simply didn't support generics)

instead of "Collection of fixes to Lazy", which is quite vague.

Comment on lines 286 to 289
// TODO: This method can be optimized to *only* serialize the fields that we have
// initialized rather than deserializing the whole account, and then serializing it
// back, which consumes a lot more CUs than it should for most accounts.
fn exit(&self, program_id: &anchor_lang::prelude::Pubkey) -> anchor_lang::Result<()> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Just FYI since you're already at it: by far the most inefficient part of lazy accounts is this part. It's certainly possible to do all sorts of optimizations here in case you're interested.

Comment thread lang/derive/serde/src/lazy.rs Outdated
Comment thread lang/derive/serde/src/lazy.rs Outdated
@acheroncrypto acheroncrypto added ci feature and removed fix Bug fix PR labels Feb 13, 2026
@jamie-osec jamie-osec force-pushed the lazy-sized-fixes branch 2 times, most recently from 489d279 to 698c13a Compare February 13, 2026 20:06
@jamie-osec jamie-osec changed the title Collection of fixes to Lazy Optimizations enums with all unit variants and empty arrays with Lazy Feb 13, 2026
@jamie-osec

jamie-osec commented Feb 13, 2026

Copy link
Copy Markdown
Collaborator Author

Kept the two SIZED optimisations here, split the generics commits and CI changes to a new PR

@jamie-osec jamie-osec changed the title Optimizations enums with all unit variants and empty arrays with Lazy Optimize enums with all unit variants and empty arrays with Lazy Feb 13, 2026
@jamie-osec jamie-osec added fix Bug fix PR and removed ci feature labels Feb 13, 2026

@acheroncrypto acheroncrypto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for updating! It's so much easier and faster to review and merge this way.

Comment thread lang/derive/serde/src/lazy.rs Outdated
Comment thread lang/derive/serde/src/lazy.rs Outdated

@acheroncrypto acheroncrypto left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice quick win!

As a side note, I want to mention that the enum optimization is a special case (unit variant only case) of a more general potential optimization: "if all variants have the same size, the enum is sized". It would make the impl a bit more complicated, which I think was the reason I didn't do it initially, but there's essentially no downside from the user PoV.

@jamie-osec

Copy link
Copy Markdown
Collaborator Author

if all variants have the same size, the enum is sized

Is this determinable in a const context? We need to provide a const but we can't call the size_of method in a trait as it's non-const (and can't be made const).

Either way, I think I'd like to land this with the current changes, and revisit optimisations in a followup (especially any that could potentially conflict/benefit from #4240)

@acheroncrypto

Copy link
Copy Markdown
Collaborator

Is this determinable in a const context?

I believe it's technically possible, but I'm not sure if it's worth implementing just for this.

Either way, I think I'd like to land this with the current changes, and revisit optimisations in a followup (especially any that could potentially conflict/benefit from #4240)

Yeah, agreed. All good on my end.

@acheroncrypto acheroncrypto merged commit 2ddf2e2 into otter-sec:master Mar 8, 2026
111 of 114 checks passed
Otter-0x4ka5h pushed a commit to Otter-0x4ka5h/anchor that referenced this pull request Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants