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

feat: How to use profiler to optimize gas usage #267

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

raizo07
Copy link
Contributor

@raizo07 raizo07 commented Nov 29, 2024

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.

Copy link
Contributor

@julio4 julio4 left a 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 :)

Comment on lines +37 to +64
```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;
};
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Contributor Author

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 {
Copy link
Contributor

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
Copy link
Contributor

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?

Comment on lines 270 to 282
## 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
```
Copy link
Contributor

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

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.

Feat: How to use profiler to optimize gas usage
2 participants