Skip to content

Commit

Permalink
dev: loop profiling (#1256)
Browse files Browse the repository at this point in the history
<!--- Please provide a general summary of your changes in the title
above -->

<!-- Give an estimate of the time you spent on this PR in terms of work
days.
Did you spend 0.5 days on this PR or rather 2 days?  -->

Time spent on this PR:

## Pull request type

<!-- Please try to limit your pull request to one type,
submit multiple pull requests if needed. -->

Please check the type of change your PR introduces:

- [ ] Bugfix
- [ ] Feature
- [ ] Code style update (formatting, renaming)
- [ ] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] Documentation content changes
- [X] Other (please describe): profiling

## What is the current behavior?

<!-- Please describe the current behavior that you are modifying,
or link to a relevant issue. -->

Resolves #1249

## What is the new behavior?

<!-- Please describe the behavior or changes that are being added by
this PR. -->

For max = 10:

![profile001](https://github.com/kkrt-labs/kakarot/assets/92337658/1de65da6-64f6-490c-9090-a3a6ce58c9f1)


For max = 50

![profile002](https://github.com/kkrt-labs/kakarot/assets/92337658/efc15393-17d4-4607-ac79-13a828ed25af)

<!-- Reviewable:start -->
- - -
This change is [<img src="https://reviewable.io/review_button.svg"
height="34" align="absmiddle"
alt="Reviewable"/>](https://reviewable.io/reviews/kkrt-labs/kakarot/1256)
<!-- Reviewable:end -->
  • Loading branch information
obatirou committed Jul 12, 2024
1 parent 5587a08 commit 93cd65e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
8 changes: 8 additions & 0 deletions solidity_contracts/src/PlainOpcodes/PlainOpcodes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ contract PlainOpcodes {
return hash;
}

function loopProfiling(uint256 max) public pure returns (uint256) {
uint256 val = 0;
for (uint256 i = 0; i < max; i++) {
val += i;
}
return val;
}

receive() external payable {}
fallback() external payable {}
}
2 changes: 1 addition & 1 deletion tests/fixtures/starknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _factory(entrypoint, **kwargs) -> list:
static_locals={"debug_info": debug_info(program)},
vm_class=VmWithCoverage,
)
run_resources = RunResources(n_steps=4_000_000)
run_resources = RunResources(n_steps=10_000_000)
try:
runner.run_until_pc(end, run_resources)
except Exception as e:
Expand Down
19 changes: 19 additions & 0 deletions tests/src/kakarot/test_kakarot.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,3 +385,22 @@ async def test_failing_contract(self, cairo_run):
data="0xADD_DATA",
)
assert not evm["reverted"]

class TestLoopProfiling:
@pytest.mark.slow
@pytest.mark.NoCI
@pytest.mark.parametrize("steps", [10, 50, 100, 200])
@SyscallHandler.patch("IAccount.is_valid_jumpdest", lambda addr, data: [1])
def test_loop_profiling(self, get_contract, steps):
plain_opcodes = get_contract("PlainOpcodes", "PlainOpcodes")
initial_state = {
CONTRACT_ADDRESS: {
"code": list(plain_opcodes.bytecode_runtime),
"storage": {},
"balance": 0,
"nonce": 0,
}
}
with SyscallHandler.patch_state(parse_state(initial_state)):
res = plain_opcodes.loopProfiling(steps)
assert res == sum(x for x in range(steps))

0 comments on commit 93cd65e

Please sign in to comment.