-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Reduce stack usage of try_accounts
#3060
Comments
Thank you for creating this issue and giving a thorough explanation.
Awesome! This has been very inconsistent in my experience.
Are you able to tell whether that code path is going to be used? It currently shows the stack errors even if that code path is unreachable the compiled program.
Yeah, the biggest offender is the
What's the point of reducing their stack usage if they are only being used in compile-time? As you pointed out, they don't exist in programs, and they are only being used inside macros. |
Rust should tell you that you have unreachable code. I am not sure if it works with macros, though. This is a good case to investigate. I'd be happy if you can provide a reproducible example.
We may look into promoting items to the heap, but this would overshadow the programming language. Rust already let developers choose where to allocate (e.g. by using Box).
If we end up blocking compilation of functions that access memory outside their frames, this case would show up because it is going through the Solana backend. Maybe we can just investigate why it is being compiler for Solana then. |
I just removed some functions from the Solana target and these extra errors go away. There are still other parts being built for Solana. Hopefully, the fix is very simple. |
It's not general unreachable code, but rather unreachable code from one's program. For example, if you were to add
Isn't the optimization here (or in #2939) only for the stack? We're not really trying to improve stack usage by moving things to heap, just using a different stack frame instead. It seems to me that the compiler has enough information about the potential stack usage and can do optimizations in a more general way. |
I have been investigating complaints about invalid stack accesses in the compiler toolchain that happen with Anchor contracts. Most problems occur with function
try_accounts
, whose constraints expansion eats up a lot of stack space.anchor/lang/syn/src/codegen/accounts/try_accounts.rs
Line 121 in 5f1f72c
I also have found some items that you may be aware of:
The
try_accounts
function is the only one that gets into the contract binary, so it advisable to break it in small methods (I had a look myself but found it very intricate).Functions
linearize
andparse_account_field
also have compiler errors, but they are not put into the contract binary. Perhaps, we are building them for Solana unnecessarily. We could also decrease their stack usage, but this case is simpler thantry_accounts
and should be less priority.anchor/lang/syn/src/codegen/accounts/constraints.rs
Line 71 in ca7fcee
anchor/lang/syn/src/parser/accounts/mod.rs
Line 293 in ca7fcee
The text was updated successfully, but these errors were encountered: