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

Add close unstable #56

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Add close unstable #56

wants to merge 2 commits into from

Conversation

febo
Copy link
Collaborator

@febo febo commented Jan 23, 2025

Problem

When PR #55 fixed the last index of the owner Pubkey on close_unchecked, it pushed the CUs used by the method to over 50.

Solution

Reimplement close_unchecked using the sol_memset syscall, which consumes 14 CUs instead. Since this is still over the original 7 CUs, this PR also re-adds the close_unstable which uses ASM – this variant takes 7 CUs.

The close_unstable can be enabled by using the feature "asm".

Note: close_unstable courtesy of @L0STE and @deanmlittle

@@ -430,17 +436,45 @@ impl AccountInfo {
// - 8 bytes for the data_len
//
// So we can zero out them directly.
#[cfg(target_os = "solana")]
sol_memset_(self.data_ptr().sub(48), 0, 48);

Choose a reason for hiding this comment

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

you can use slice::fill and can get rid of the cfg attr

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using slice::fill takes 58 CUs – same as the current code. The sol_memset_ is more efficient in this case.

Choose a reason for hiding this comment

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

yes, but why?

If the compiler is desugaring to stores, CUs should be lower than the syscall - it's the whole point of applying the optimization. It might mean that the optimization is wrong, or the syscall is mispriced.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I had the same question. 😊 @LucasSte looked into it and already has a plan to improve it.

Copy link

@LucasSte LucasSte Jan 23, 2025

Choose a reason for hiding this comment

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

Four stores is the threshold to call memset from compiler-builtins. This is what causing the CU bump. Both the threshold to call the library in LLVM and the threshold to call the syscall in compiler-builtins need to be revised.

unsafe {
let zero = 0u64;
core::arch::asm!(
"stxdw [{0}-8], {1}",

Choose a reason for hiding this comment

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

You can try stdw [{0}-8], 0 directly here, without an extra register.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As per offline conversation, the stdw is not yet available to the compiler – only the vm.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, this has been disabled since like forever. You literally can't deploy a program that contains any of the st class insns. If your code never updates the value of r0, you can accomplish the same thing with stxdw[offset], r0 but it's not safe to do so in Rust without some kind of a compiler hint to tell it "please don't reassign r0".

Copy link

@LucasSte LucasSte Jan 24, 2025

Choose a reason for hiding this comment

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

You literally can't deploy a program that contains any of the st class insns.

Where did you get this information from? Not only does the class pass the bytecode verification, but also is implemented in the jitter and the interpreter.

The fact that the compiler wasn't emitting code for the store immediate class doesn't mean you can't deploy a program with it.

Co-authored-by: Lucas Ste <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants