Skip to content

Lazy Rent Sysvar Fetching for Pre-funded Accounts #214

@spellsaif

Description

@spellsaif

Use case

  • Minimizing required syscalls and saving CUs during account initialization.

What are you trying to do?

Currently, the #[derive(Accounts)] macro uses RentPlan::FetchOnce for instructions involving init or realloc. This unconditionally executes the sol_get_sysvar syscall at the start of the instruction, costing ~100 to 150 CUs.
During initialization in lang/src/cpi/system.rs, the framework uses Rent strictly to check if an account requires additional lamports:

let lamports = rent.try_minimum_balance(space as usize)?;
if account.lamports() >= lamports { 
    // Allocate + Assign
} else {
    // CreateAccount (transfers lamports)
}

If an account is pre-funded, the framework doesn't actually need to calculate the minimum balance. The Solana runtime natively enforces rent-exemption at the end of the transaction anyway. Fetching the Rent sysvar for pre-funded accounts wastes ~150 CUs for no reason.

Proposed solution

We can move the Rent sysvar fetch to be completely lazy, executing only if an on-chain transfer is strictly required (when account.lamports() == 0).

  1. Modify RentPlan::FetchOnce in the derive macro so that it passes a lazy closure or Option<Rent> to the initialization context instead of eagerly fetching the sysvar.
  2. Update init_account_with_rent (in lang/src/cpi/system.rs) to check if account.lamports() > 0 before attempting to evaluate the minimum rent balance.
  3. If true, safely skip the Rent syscall and proceed directly with Allocate and Assign (the SVM guarantees rent-exemption safety at the end of the transaction).
  4. If false, invoke the lazy fetch (sol_get_sysvar) to determine the exact lamports required for the System Program transfer.
    This saves ~150 CUs for pre-funded initialization paths automatically.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions