This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
Add flamegraph target to Makefile #205
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
885ee78
Add flamegraph target to Makefile
MegaRedHand af27de7
Add dependency to target
MegaRedHand 5b6ac9c
Fix: activate venv before compiling contracts
MegaRedHand d71ae9d
Rename benches
MegaRedHand 33827d2
Add declare test
MegaRedHand 1deb2e2
Clone elements outside `scope`
MegaRedHand 3ae2125
Appease clippy
MegaRedHand a954eef
Add deploy test
MegaRedHand a62ce83
Reduce run count
MegaRedHand 492cb51
Fix: invert guard in `handle_empty_constructor`
MegaRedHand d0c7097
Use correct class_hash in test
MegaRedHand 567bb7a
Add invoke flamegraph
MegaRedHand 53d0076
Pin cargo-flamegraph version
MegaRedHand a8cbdb3
Add instructions to README
MegaRedHand e509256
Remove unnecessary clone
MegaRedHand 9a73a31
Merge branch 'main' into prof-flamegraph
MegaRedHand 10f9c96
Add ContractClass with include_str!
MegaRedHand 38637ad
Revert "Add ContractClass with include_str!"
MegaRedHand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
#![deny(warnings)] | ||
|
||
use felt::{felt_str, Felt}; | ||
use lazy_static::lazy_static; | ||
use num_traits::Zero; | ||
use starknet_rs::{ | ||
business_logic::{ | ||
fact_state::in_memory_state_reader::InMemoryStateReader, | ||
state::{cached_state::CachedState, state_api::State}, | ||
transaction::objects::{ | ||
internal_declare::InternalDeclare, internal_deploy::InternalDeploy, | ||
internal_deploy_account::InternalDeployAccount, | ||
internal_invoke_function::InternalInvokeFunction, | ||
}, | ||
}, | ||
core::contract_address::starknet_contract_address::compute_class_hash, | ||
definitions::general_config::StarknetChainId, | ||
public::abi::VALIDATE_ENTRY_POINT_SELECTOR, | ||
services::api::contract_class::ContractClass, | ||
utils::{felt_to_hash, Address}, | ||
}; | ||
use std::{hint::black_box, path::PathBuf}; | ||
|
||
lazy_static! { | ||
// include_str! doesn't seem to work in CI | ||
static ref CONTRACT_CLASS: ContractClass = ContractClass::try_from(PathBuf::from( | ||
"starknet_programs/account_without_validation.json", | ||
)) | ||
.unwrap(); | ||
static ref CLASS_HASH: [u8; 32] = felt_to_hash(&compute_class_hash( | ||
&CONTRACT_CLASS | ||
).unwrap()); | ||
static ref CONTRACT_ADDRESS: Address = Address(felt_str!( | ||
"3577223136242220508961486249701638158054969090851914040041358274796489907314" | ||
)); | ||
static ref SIGNATURE: Vec<Felt> = vec![ | ||
felt_str!("3233776396904427614006684968846859029149676045084089832563834729503047027074"), | ||
felt_str!("707039245213420890976709143988743108543645298941971188668773816813012281203"), | ||
]; | ||
} | ||
|
||
// This function just executes the given function. This adds a stack level | ||
// to the flamegraph with the label "scope". | ||
#[inline(never)] | ||
fn scope<T>(f: impl FnOnce() -> T) -> T { | ||
f() | ||
} | ||
|
||
// We don't use the cargo test harness because it uses | ||
// FnOnce calls for each test, that are merged in the flamegraph. | ||
fn main() { | ||
deploy_account(); | ||
declare(); | ||
deploy(); | ||
invoke(); | ||
|
||
// The black_box ensures there's no tail-call optimization. | ||
// If not, the flamegraph ends up less nice. | ||
black_box(()); | ||
} | ||
|
||
#[inline(never)] | ||
fn deploy_account() { | ||
const RUNS: usize = 500; | ||
|
||
let state_reader = InMemoryStateReader::default(); | ||
let mut state = CachedState::new(state_reader, Some(Default::default())); | ||
|
||
state | ||
.set_contract_class(&CLASS_HASH, &CONTRACT_CLASS) | ||
.unwrap(); | ||
|
||
let config = &Default::default(); | ||
|
||
for _ in 0..RUNS { | ||
let mut state_copy = state.clone(); | ||
let salt = Address(felt_str!( | ||
"2669425616857739096022668060305620640217901643963991674344872184515580705509" | ||
)); | ||
let class_hash = *CLASS_HASH; | ||
let signature = SIGNATURE.clone(); | ||
scope(|| { | ||
// new consumes more execution time than raw struct instantiation | ||
let internal_deploy_account = InternalDeployAccount::new( | ||
class_hash, | ||
0, | ||
0, | ||
Felt::zero(), | ||
vec![], | ||
signature, | ||
salt, | ||
StarknetChainId::TestNet, | ||
) | ||
.unwrap(); | ||
internal_deploy_account.execute(&mut state_copy, config) | ||
}) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
#[inline(never)] | ||
fn declare() { | ||
const RUNS: usize = 5; | ||
|
||
let state_reader = InMemoryStateReader::default(); | ||
let state = CachedState::new(state_reader, Some(Default::default())); | ||
|
||
let config = &Default::default(); | ||
|
||
for _ in 0..RUNS { | ||
let mut cloned_state = state.clone(); | ||
let class = CONTRACT_CLASS.clone(); | ||
let address = CONTRACT_ADDRESS.clone(); | ||
scope(|| { | ||
// new consumes more execution time than raw struct instantiation | ||
let declare_tx = InternalDeclare::new( | ||
class, | ||
StarknetChainId::TestNet.to_felt(), | ||
address, | ||
0, | ||
0, | ||
vec![], | ||
Felt::zero(), | ||
) | ||
.expect("couldn't create transaction"); | ||
|
||
declare_tx.execute(&mut cloned_state, config) | ||
}) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
#[inline(never)] | ||
fn deploy() { | ||
const RUNS: usize = 8; | ||
|
||
let state_reader = InMemoryStateReader::default(); | ||
let mut state = CachedState::new(state_reader, Some(Default::default())); | ||
|
||
state | ||
.set_contract_class(&CLASS_HASH, &CONTRACT_CLASS) | ||
.unwrap(); | ||
|
||
let config = &Default::default(); | ||
|
||
for _ in 0..RUNS { | ||
let mut state_copy = state.clone(); | ||
let salt = Address(felt_str!( | ||
"2669425616857739096022668060305620640217901643963991674344872184515580705509" | ||
)); | ||
let class = CONTRACT_CLASS.clone(); | ||
scope(|| { | ||
// new consumes more execution time than raw struct instantiation | ||
let internal_deploy = | ||
InternalDeploy::new(salt, class, vec![], StarknetChainId::TestNet.to_felt(), 0) | ||
.unwrap(); | ||
internal_deploy.execute(&mut state_copy, config) | ||
}) | ||
.unwrap(); | ||
} | ||
} | ||
|
||
#[inline(never)] | ||
fn invoke() { | ||
const RUNS: usize = 100; | ||
|
||
let state_reader = InMemoryStateReader::default(); | ||
let mut state = CachedState::new(state_reader, Some(Default::default())); | ||
|
||
state | ||
.set_contract_class(&CLASS_HASH, &CONTRACT_CLASS) | ||
.unwrap(); | ||
|
||
let config = &Default::default(); | ||
|
||
let salt = Address(felt_str!( | ||
"2669425616857739096022668060305620640217901643963991674344872184515580705509" | ||
)); | ||
let class = CONTRACT_CLASS.clone(); | ||
let internal_deploy = | ||
InternalDeploy::new(salt, class, vec![], StarknetChainId::TestNet.to_felt(), 0).unwrap(); | ||
internal_deploy.execute(&mut state, config).unwrap(); | ||
|
||
for _ in 0..RUNS { | ||
let mut state_copy = state.clone(); | ||
let address = CONTRACT_ADDRESS.clone(); | ||
let selector = VALIDATE_ENTRY_POINT_SELECTOR.clone(); | ||
let signature = SIGNATURE.clone(); | ||
let calldata = vec![address.0.clone(), selector.clone(), Felt::zero()]; | ||
scope(|| { | ||
// new consumes more execution time than raw struct instantiation | ||
let internal_invoke = InternalInvokeFunction::new( | ||
address, | ||
selector, | ||
0, | ||
calldata, | ||
signature, | ||
StarknetChainId::TestNet.to_felt(), | ||
Some(Felt::zero()), | ||
) | ||
.unwrap(); | ||
internal_invoke.execute(&mut state_copy, config) | ||
}) | ||
.unwrap(); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.
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.
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.
Is there any way to build the
ContractClass
from bytes? In that case you could useinclude_bytes!
instead of creating a path that needs to be read from.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.
Done!
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.
The GH workflow that runs clippy doesn't install deps and so cannot compile the cairo programs. I'll revert this change.