Skip to content

Commit 5c44944

Browse files
committed
Feat: added memory section on performance
1 parent 8f2b0fd commit 5c44944

File tree

1 file changed

+69
-1
lines changed
  • src/app/content/courses/pinocchio-for-dummies/performance

1 file changed

+69
-1
lines changed

src/app/content/courses/pinocchio-for-dummies/performance/en.mdx

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,72 @@ flags ^= FLAG_ACTIVE; // flags = 0000_1001 (now has VERIFIED)
172172
flags ^= FLAG_LOCKED; // flags = 0000_0001 (LOCKED now cleared)
173173

174174
// Toggle multiple flags at once
175-
flags ^= FLAG_PREMIUM | FLAG_LOCKED; // flags = 0000_1011
175+
flags ^= FLAG_PREMIUM | FLAG_LOCKED; // flags = 0000_1011
176+
```
177+
178+
<ArticleSection name="Memory on Solana" id="memory-on-solana" level="h2" />
179+
180+
The Solana Virtual Machine (SVM) uses a three-tier memory architecture that strictly separates stack memory (local variables), heap memory (dynamic structures), and account space (persistent storage).
181+
182+
Understanding this architecture is crucial for writing high-performance Solana programs.
183+
184+
Programs operate within fixed virtual address spaces with predictable memory mapping:
185+
- **Program Code**: `0x100000000` - Where your compiled program bytecode lives
186+
- **Stack Data**: `0x200000000` - Local variables and function call frames
187+
- **Heap Data**: `0x300000000` - Dynamic allocations (expensive!)
188+
189+
This deterministic layout enables powerful optimizations but also creates strict performance constraints.
190+
191+
> Solana imposes strict memory limitations: 4KB stack frames per function call and 32KB total heap space per program execution.
192+
193+
### The Zero-Allocation Advantage
194+
195+
Pinocchio's main performance breakthrough comes from using references instead of heap allocations for everything.
196+
197+
This approach leverages a key insight: **the SVM already loads all your program inputs into memory**, so copying that data into new heap allocations is pure waste.
198+
199+
Heap allocation isn't inherently bad, but on Solana it's expensive and complex because every allocation consumes precious compute units: each allocation fragments the limited heap space and cleanup operations consume additional compute units.
200+
201+
### Zero-Allocation Techniques
202+
203+
1. Reference-Based Data Structures - Transform owned data into borrowed references:
204+
```rust
205+
// HEAP ALLOCATION:
206+
struct AccountInfo {
207+
key: Pubkey, // Owned data - copied to heap
208+
data: Vec<u8>, // Vector - definitely heap allocated
209+
}
210+
211+
// ZERO ALLOCATION:
212+
struct AccountInfo<'a> {
213+
key: &'a Pubkey, // Reference - no allocation
214+
data: &'a [u8], // Slice reference - no allocation
215+
}
216+
```
217+
218+
2. Zero-Copy Data Access - Access data in-place without deserialization:
219+
```rust
220+
// Instead of deserializing, access data in-place:
221+
pub fn process_transfer(accounts: &[u8], instruction_data: &[u8]) {
222+
// Parse accounts directly from byte slice - NO HEAP ALLOCATION
223+
let source_account = &accounts[0..36]; // Just slice references
224+
let dest_account = &accounts[36..72];
225+
226+
// Access fields through pointer arithmetic - NO ALLOCATION
227+
let amount = u64::from_le_bytes(instruction_data[0..8].try_into().unwrap());
228+
}
229+
```
230+
231+
3. No-Std Constraints - Prevent accidental heap usage at compile time:
232+
```rust
233+
// Enforces no-std to prevents accidental heap usage:
234+
#![no_std]
235+
```
236+
237+
### Pinocchio Memory Allocator
238+
239+
If you want to make sure that you don't allocate any heap, Pinocchio provide a specialized macro to make sure that this happen: `no_allocator!()`
240+
241+
> If not set, Pinocchio will use the `default_allocator!()` for programs requiring traditional heap operations.
242+
243+
Other than this, you can always write a better heap allocator that knows how to clean up after itself. If you're interested in this approach, [here's an example](https://github.com/solana-labs/solana-program-library/blob/master/examples/rust/custom-heap/src/entrypoint.rs).

0 commit comments

Comments
 (0)