From 076e62837f8ce2b4d3864ced40922cc910181719 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Sun, 28 Apr 2024 06:34:01 +0200 Subject: [PATCH] perf: re-order gas check (#15) --- .github/workflows/bench.yml | 59 +++++++++++++++++++++++ .github/workflows/ci.yml | 43 +---------------- crates/revm-jit-cli/benches/iai.rs | 26 +++++++--- crates/revm-jit/src/compiler/translate.rs | 7 +-- 4 files changed, 84 insertions(+), 51 deletions(-) create mode 100644 .github/workflows/bench.yml diff --git a/.github/workflows/bench.yml b/.github/workflows/bench.yml new file mode 100644 index 0000000..6afa884 --- /dev/null +++ b/.github/workflows/bench.yml @@ -0,0 +1,59 @@ +name: Benchmark + +on: + push: + branches: [main] + pull_request: + branches: [main] + workflow_dispatch: + +env: + CARGO_TERM_COLOR: always + LLVM_VERSION: "17" # Must be just the major version + +concurrency: + group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }} + cancel-in-progress: true + +jobs: + iai: + runs-on: ubuntu-latest + env: + BASELINE: base + IAI_CALLGRIND_RUNNER: iai-callgrind-runner + steps: + - uses: actions/checkout@v4 + - name: Install LLVM + run: sudo .github/scripts/install_llvm_ubuntu.sh ${{ env.LLVM_VERSION }} + - name: Install Valgrind + run: sudo apt update && sudo apt install valgrind + - uses: dtolnay/rust-toolchain@stable + - name: Install cargo-binstall + uses: taiki-e/install-action@cargo-binstall + - name: Install iai-callgrind-runner + run: | + echo "::group::Install" + version=$(cargo metadata --format-version=1 |\ + jq '.packages[] | select(.name == "iai-callgrind").version' |\ + tr -d '"' + ) + cargo binstall iai-callgrind-runner --version $version --no-confirm --no-symlinks --force + echo "::endgroup::" + echo "::group::Verification" + which iai-callgrind-runner + echo "::endgroup::" + - name: Checkout base + uses: actions/checkout@v4 + with: + ref: ${{ github.base_ref || 'main' }} + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + - name: Save baseline + run: cargo bench -p revm-jit-cli --bench iai -- --save-baseline=$BASELINE + - name: Checkout PR + uses: actions/checkout@v4 + with: + clean: false + - name: Compare PR benchmarks + run: cargo bench -p revm-jit-cli --bench iai -- --baseline=$BASELINE diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4c89775..df6737b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,6 +4,7 @@ on: push: branches: [main] pull_request: + branches: [main] env: CARGO_TERM_COLOR: always @@ -53,48 +54,6 @@ jobs: - name: test run: cargo test --all-features --workspace --profile ${{ matrix.profile }} - iai: - runs-on: ubuntu-latest - env: - BASELINE: base - IAI_CALLGRIND_RUNNER: iai-callgrind-runner - steps: - - uses: actions/checkout@v4 - - name: Install LLVM - run: sudo .github/scripts/install_llvm_ubuntu.sh ${{ env.LLVM_VERSION }} - - name: Install Valgrind - run: sudo apt update && sudo apt install valgrind - - uses: dtolnay/rust-toolchain@stable - - uses: Swatinem/rust-cache@v2 - with: - cache-on-failure: true - - name: Install cargo-binstall - uses: taiki-e/install-action@cargo-binstall - - name: Install iai-callgrind-runner - run: | - echo "::group::Install" - version=$(cargo metadata --format-version=1 |\ - jq '.packages[] | select(.name == "iai-callgrind").version' |\ - tr -d '"' - ) - cargo binstall iai-callgrind-runner --version $version --no-confirm --no-symlinks --force - echo "::endgroup::" - echo "::group::Verification" - which iai-callgrind-runner - echo "::endgroup::" - - name: Checkout base - uses: actions/checkout@v4 - with: - ref: ${{ github.base_ref || 'main' }} - - name: Save baseline - run: cargo bench -p revm-jit-cli --bench iai -- --save-baseline=$BASELINE - - name: Checkout PR - uses: actions/checkout@v4 - with: - clean: false - - name: Compare PR benchmarks - run: cargo bench -p revm-jit-cli --bench iai -- --baseline=$BASELINE - feature-checks: runs-on: ubuntu-latest timeout-minutes: 30 diff --git a/crates/revm-jit-cli/benches/iai.rs b/crates/revm-jit-cli/benches/iai.rs index 5daca7e..dbeb442 100644 --- a/crates/revm-jit-cli/benches/iai.rs +++ b/crates/revm-jit-cli/benches/iai.rs @@ -18,13 +18,17 @@ binary_benchmark_group!( ); fn setup_group(group: &mut BinaryBenchmarkGroup, is_ct: bool) { - let make_run = |name: &str| { + let make_run = |name: &str, small: bool| { let mut args = Vec::with_capacity(3); args.push(name); + // let out_dir = std::env::temp_dir().join("revm-jit-cli-iai"); + // let so = out_dir.join(name).join("a.so"); if is_ct { args.extend(["--aot", "--no-link"]); + // args.extend(["--aot", "--no-link", "-o", out_dir.to_str().unwrap()]); } else { args.push("1"); + // args.extend(["1", "--shared-library", so.to_str().unwrap()]); } let arg = Arg::new(name, args); let mut run = Run::with_cmd(CMD, arg); @@ -35,22 +39,32 @@ fn setup_group(group: &mut BinaryBenchmarkGroup, is_ct: bool) { let mut regression = RegressionConfig::default(); if is_ct { - regression.limits([(EventKind::EstimatedCycles, 10.0)]); + let cycles = if small { 50.0 } else { 20.0 }; + regression.limits([(EventKind::EstimatedCycles, cycles)]); } else { regression.limits([(EventKind::EstimatedCycles, 5.0)]); } run.regression(regression); - if !is_ci() { + if small && !is_ci() { let flamegraph = FlamegraphConfig::default(); run.flamegraph(flamegraph); } run }; - let benches = ["fibonacci", "counter", "push0_proxy", "weth", "hash_20k", "usdc_proxy"]; - for bench in &benches { - group.bench(make_run(bench)); + let benches = [ + ("fibonacci", true), + ("counter", true), + ("hash_20k", true), + ("usdc_proxy", false), + ("weth", false), + ]; + for (bench, small) in benches { + if !is_ct && !small { + continue; + } + group.bench(make_run(bench, small)); } } diff --git a/crates/revm-jit/src/compiler/translate.rs b/crates/revm-jit/src/compiler/translate.rs index 23e5105..a509d2d 100644 --- a/crates/revm-jit/src/compiler/translate.rs +++ b/crates/revm-jit/src/compiler/translate.rs @@ -1324,16 +1324,17 @@ impl<'a, B: Backend> FunctionCx<'a, B> { return; } - // `Gas::record_cost` + // Modified from `Gas::record_cost`. let gas_remaining = self.load_gas_remaining(); let (res, overflow) = self.bcx.usub_overflow(gas_remaining, cost); - self.build_check(overflow, InstructionResult::OutOfGas); let nomem = self.gas_remaining_nomem.load(&mut self.bcx, "gas_remaining_nomem"); let nomem = self.bcx.isub(nomem, cost); - self.gas_remaining_nomem.store(&mut self.bcx, nomem); self.store_gas_remaining(res); + self.gas_remaining_nomem.store(&mut self.bcx, nomem); + + self.build_check(overflow, InstructionResult::OutOfGas); } /*