Skip to content
This repository was archived by the owner on Aug 1, 2025. It is now read-only.

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

Closed
raizo07 wants to merge 5 commits intoNethermindEth:devfrom
raizo07:profiler
Closed

feat: How to use profiler to optimize gas usage#267
raizo07 wants to merge 5 commits intoNethermindEth:devfrom
raizo07:profiler

Conversation

@raizo07
Copy link
Copy Markdown
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.

julio4 and others added 2 commits November 21, 2024 16:37
* dep: fix patch to latest shikijs for cairo hl

* fix(app): correct sidebar placement

* fix(app): responsive content centering

* fix(app): responsive content centering

* doc: branch guidelines

* fix(app): top section nav links
Copy link
Copy Markdown
Member

@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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
Member

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
Copy Markdown
Member

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
Copy Markdown
Member

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

@julio4
Copy link
Copy Markdown
Member

julio4 commented Dec 17, 2024

@raizo07 Do you want me to apply the last requested changes?

@raizo07
Copy link
Copy Markdown
Contributor Author

raizo07 commented Dec 17, 2024

@raizo07 Do you want me to apply the last requested changes?

Hey @julio4 could you be more explicit in the requests, so I can apply them today for your review.

@julio4
Copy link
Copy Markdown
Member

julio4 commented Dec 17, 2024

@raizo07 Do you want me to apply the last requested changes?

Hey @julio4 could you be more explicit in the requests, so I can apply them today for your review.

You can read and resolve all comments from previous reviews

@raizo07
Copy link
Copy Markdown
Contributor Author

raizo07 commented Dec 17, 2024

On it

@julio4 julio4 force-pushed the dev branch 3 times, most recently from 4328222 to 9267ef0 Compare January 28, 2025 22:34
@julio4 julio4 force-pushed the dev branch 2 times, most recently from 52ddbeb to 0dffcc5 Compare February 8, 2025 15:53
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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