-
Notifications
You must be signed in to change notification settings - Fork 88
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
feat: How to use profiler to optimize gas usage #267
base: dev
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! But a few changes to make :)
```cairo | ||
#[starknet::contract] | ||
mod UnoptimizedContract { | ||
#[storage] | ||
struct Storage { | ||
balances: LegacyMap<ContractAddress, u256>, | ||
allowances: LegacyMap<(ContractAddress, ContractAddress), u256>, | ||
} | ||
|
||
#[external] | ||
fn transfer_all_from_list( | ||
ref self: ContractState, | ||
from: ContractAddress, | ||
to_list: Array<ContractAddress> | ||
) { | ||
// Unoptimized: Multiple storage reads/writes | ||
let len = to_list.len(); | ||
let mut i: usize = 0; | ||
loop { | ||
if i >= len { | ||
break; | ||
} | ||
let recipient = *to_list.at(i); | ||
let balance = self.balances.read(from); | ||
self.balances.write(from, 0); | ||
self.balances.write(recipient, balance); | ||
i += 1; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you move this in a listing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @julio4 do you mean the listing
folder?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, listing folder. See this section of the contribution guide.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay
```cairo | ||
|
||
#[starknet::contract] | ||
mod OptimizedContract { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same
- Batch L1 messages when possible | ||
- Compress data when sending to L1 | ||
|
||
## Common Optimization Patterns |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you benchmarked all of these?
## Resources and Tools | ||
|
||
1. **pprof visualization**: | ||
|
||
```cairo | ||
go tool pprof -http=:8080 profile.pb.gz | ||
``` | ||
|
||
2. **Flame graph generation**: | ||
|
||
```cairo | ||
cairo-profiler trace.bin --output-path flame.svg --format=flamegraph | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add screenshot of pprof graphs on the Unoptimized and Optimized contracts. You can move this up a bit
Issue: Close #261
Description
Feature on how to optimize gas usage with cairo-profiler to learn how and to analyze execution traces to help developers optimize L2 resource usage and debug their contracts.