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
Closed
feat: How to use profiler to optimize gas usage#267raizo07 wants to merge 5 commits intoNethermindEth:devfrom
raizo07 wants to merge 5 commits intoNethermindEth:devfrom
Conversation
* 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
julio4
suggested changes
Dec 2, 2024
Member
julio4
left a comment
There was a problem hiding this comment.
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; | ||
| }; |
Contributor
Author
There was a problem hiding this comment.
Hey @julio4 do you mean the listing folder?
Member
There was a problem hiding this comment.
Yes, listing folder. See this section of the contribution guide.
| ```cairo | ||
|
|
||
| #[starknet::contract] | ||
| mod OptimizedContract { |
| - Batch L1 messages when possible | ||
| - Compress data when sending to L1 | ||
|
|
||
| ## Common Optimization Patterns |
Member
There was a problem hiding this comment.
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 | ||
| ``` |
Member
There was a problem hiding this comment.
Can you add screenshot of pprof graphs on the Unoptimized and Optimized contracts. You can move this up a bit
Member
|
@raizo07 Do you want me to apply the last requested changes? |
Contributor
Author
Member
Contributor
Author
|
On it |
4328222 to
9267ef0
Compare
52ddbeb to
0dffcc5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.