Files
revive/crates/resolc/benches/compile.rs
T
LJandxermicus a4163512ff Improve compilation time in standard JSON mode and honor requested output (#461)
# Description

Compilation times for resolc were significantly slower than solc when:
* Multiple files were included in the JSON input file via
`--standard-json`
* And none of the files requested code generation
* (If only a subset of files requested code generation, both resolc and
solc were very slow)

Reason:
* Resolc unconditionally generated bytecode for all contracts, even if
not requested.
* When compiling to PolkaVM bytecode, we need to pass additional flags
(e.g. `irOptimized`) to solc. If these flags were not explicitly
requested, resolc also added these to the requested output since PolkaVM
bytecode was always generated.

## Breaking Changes

The following changes are breaking, but fix the semantics to match its
intended behavior:

* Compiling with `--standard-json` now compiles to PolkaVM bytecode
***only*** for the contracts explicitly requested in the
`outputSelection`.
* Compiling with `--standard-json` now outputs ***only*** the explicitly
requested output (e.g. `evm.assembly`, `evm.bytecode`, `ast`, etc.).

```jsonc
// Example:
{
  "settings": {
    // ...
    "outputSelection": {
      "src/common/GasService.sol": {
        // Contracts in this file will generate bytecode.
        // Only these fields of the JSON output selection will be in the `contracts` output.
        "*": ["abi", "evm.methodIdentifiers", "metadata", "evm.bytecode"],
        // Only this field of the JSON output selection will be in the `sources` output.
        "": ["ast"]
      },
      "src/common/Gateway.sol": {
        // No contracts in this file will generate bytecode.
        "*": ["abi", "evm.methodIdentifiers", "metadata"],
        // No `ast` will be in the `sources` output (only the automatically added `id`,
        // similar to solc as this is not a configurable output selection).
        "": []
      },
    }
  }
}
```

## Performance Improvements

When e.g. the standard JSON input file includes multiple files and
requests output for every contract as per this setting:

```jsonc
{
  "settings": {
    // ...
    "optimizer": { "enabled": true, "runs": 1 },
    "viaIR": true,
    "outputSelection": {
      "src/common/GasService.sol": {
        "*": ["abi", "evm.methodIdentifiers", "metadata", "evm.bytecode"],
      },
      // Rest of files..
    }
  }
}
```

The differences in compilation times depending on whether `evm.bytecode`
is selected in every file, one file, or no files can be seen below:

| Solc Optimizer (runs=1) | ViaIR | Request `abi`,
`evm.methodIdentifiers`, `metadata` | Request `evm.bytecode` | resolc
before | resolc after | solc v0.8.33 |

|:-----------------------:|:-----:|:---------:|:---------:|:-------------:|:------------:|:----------:|
| ✅ | ✅ | All files | All files | `~14.5s` | `~14.5s` | `~13.5s` |
| ✅ | ✅ | All files | One file | `~14.5s` | `~450ms` | `~13.5s` |
| ✅ | ✅ | All files | No files | `~14.5s` | `~390ms` | `~350ms` |

**Observations:**

* `resolc before` is roughly the same no matter the request
  * This is due to unconditionally generating bytecode
* When not requesting bytecode for any contracts:
  * `resolc after` now compiles in ~390ms (vs ~14.5s), similar to `solc`
* Neither `solc` nor `resolc after` generates bytecode when none of the
contracts request it
* When requesting bytecode for all contracts in _only one_ file:
  * `resolc after` now compiles in ~450ms (vs ~14.5s)
* `solc` still compiles in the same time as when requesting bytecode for
all contracts in all files
* This is something they have tried to improve (see
[Solidity#15373](https://github.com/argotorg/solidity/issues/15373) and
[Solidity#15615](https://github.com/argotorg/solidity/issues/15615)),
and the intended behavior seems to now be that bytecode should only be
generated when explicitly requested for each contract.
* Even though PRs have merged to try to fix it, their issue seems to
persist as of v0.8.33.

# Open Questions

* Foundry:
  * [x] Pruning of the AST:
* Just saw [this past
commit](https://github.com/paritytech/revive/pull/385) to not prune the
AST for Foundry compatibility.
    * Can Foundry not explicitly request `"": "ast"`?
    * **Answer:**
* Pruning the AST when not requested should be fine, as long as Foundry
explicitly requests it when needed.
      * Will be tested by Foundry maintainer.

# Review Notes

Changes in `docs/` (~25 files) can be skipped as they are automatically
updated after adding content to `book/` and rebuilding it.

# Resolved Issues

Closes #326

---------

Co-authored-by: xermicus <cyrill@parity.io>
2026-01-28 23:04:22 +01:00

209 lines
6.3 KiB
Rust

//! The `resolc` compilation benchmarks.
//! The tests mimicking the commands run by these benchmarks exist in `src/tests/cli/bin.rs`.
use std::time::Duration;
use criterion::{
criterion_group, criterion_main,
measurement::{Measurement, WallTime},
BenchmarkGroup, Criterion,
};
use resolc::{
self,
cli_utils::{
absolute_path, execute_command, ResolcOptSettings, SolcOptSettings, SOLIDITY_CONTRACT_PATH,
SOLIDITY_DEPENDENCY_CONTRACT_PATH, SOLIDITY_LARGE_DIV_REM_CONTRACT_PATH,
STANDARD_JSON_NO_PVM_CODEGEN_PER_FILE_PATH, STANDARD_JSON_PVM_CODEGEN_ONE_FILE_PATH,
STANDARD_JSON_PVM_CODEGEN_PER_FILE_PATH, YUL_MEMSET_CONTRACT_PATH,
YUL_RETURN_CONTRACT_PATH,
},
SolcCompiler,
};
/// The function under test executes the `resolc` executable.
fn execute_resolc(arguments: &[&str], stdin_file_path: Option<&str>) {
execute_command(resolc::DEFAULT_EXECUTABLE_NAME, arguments, stdin_file_path);
}
/// The function under test executes the `solc` executable.
fn execute_solc(arguments: &[&str], stdin_file_path: Option<&str>) {
execute_command(
SolcCompiler::DEFAULT_EXECUTABLE_NAME,
arguments,
stdin_file_path,
);
}
fn group<'error, M>(c: &'error mut Criterion<M>, group_name: &str) -> BenchmarkGroup<'error, M>
where
M: Measurement,
{
c.benchmark_group(group_name)
}
fn bench(
mut group: BenchmarkGroup<'_, WallTime>,
resolc_arguments: &[&str],
solc_arguments: &[&str],
stdin_file_path: Option<&str>,
) {
group.bench_function("resolc", |b| {
b.iter(|| execute_resolc(resolc_arguments, stdin_file_path));
});
group.bench_function("solc", |b| {
b.iter(|| execute_solc(solc_arguments, stdin_file_path));
});
group.finish();
}
fn bench_empty(c: &mut Criterion) {
let mut group = group(c, "Empty");
group
.sample_size(100)
.measurement_time(Duration::from_secs(8));
let path = absolute_path(SOLIDITY_CONTRACT_PATH);
let resolc_arguments = &[&path, "--bin", ResolcOptSettings::PERFORMANCE];
let solc_arguments = &[
&path,
"--bin",
"--via-ir",
"--optimize",
"--optimize-runs",
SolcOptSettings::PERFORMANCE,
];
bench(group, resolc_arguments, solc_arguments, None);
}
fn bench_dependency(c: &mut Criterion) {
let mut group = group(c, "Dependency");
group
.sample_size(50)
.measurement_time(Duration::from_secs(10));
let path = absolute_path(SOLIDITY_DEPENDENCY_CONTRACT_PATH);
let resolc_arguments = &[&path, "--bin", ResolcOptSettings::PERFORMANCE];
let solc_arguments = &[
&path,
"--bin",
"--via-ir",
"--optimize",
"--optimize-runs",
SolcOptSettings::PERFORMANCE,
];
bench(group, resolc_arguments, solc_arguments, None);
}
fn bench_large_div_rem(c: &mut Criterion) {
let mut group = group(c, "LargeDivRem");
group
.sample_size(45)
.measurement_time(Duration::from_secs(9));
let path = absolute_path(SOLIDITY_LARGE_DIV_REM_CONTRACT_PATH);
let resolc_arguments = &[&path, "--bin", ResolcOptSettings::PERFORMANCE];
let solc_arguments = &[
&path,
"--bin",
"--via-ir",
"--optimize",
"--optimize-runs",
SolcOptSettings::PERFORMANCE,
];
bench(group, resolc_arguments, solc_arguments, None);
}
fn bench_memset(c: &mut Criterion) {
let mut group = group(c, "Yul Memset");
group
.sample_size(100)
.measurement_time(Duration::from_secs(8));
let path = absolute_path(YUL_MEMSET_CONTRACT_PATH);
let resolc_arguments = &[&path, "--yul", "--bin", ResolcOptSettings::PERFORMANCE];
let solc_arguments = &[
&path,
"--strict-assembly",
"--bin",
"--optimize",
"--optimize-runs",
SolcOptSettings::PERFORMANCE,
];
bench(group, resolc_arguments, solc_arguments, None);
}
fn bench_return(c: &mut Criterion) {
let mut group = group(c, "Yul Return");
group
.sample_size(100)
.measurement_time(Duration::from_secs(7));
let path = absolute_path(YUL_RETURN_CONTRACT_PATH);
let resolc_arguments = &[&path, "--yul", "--bin", ResolcOptSettings::PERFORMANCE];
let solc_arguments = &[
&path,
"--strict-assembly",
"--bin",
"--optimize",
"--optimize-runs",
SolcOptSettings::PERFORMANCE,
];
bench(group, resolc_arguments, solc_arguments, None);
}
fn bench_standard_json_codegen_per_file(c: &mut Criterion) {
let mut group = group(c, "Std JSON Codegen All Files");
group
.sample_size(10)
.measurement_time(Duration::from_secs(200));
let path = absolute_path(STANDARD_JSON_PVM_CODEGEN_PER_FILE_PATH);
let resolc_arguments = &["--standard-json"];
let solc_arguments = &["--standard-json"];
bench(group, resolc_arguments, solc_arguments, Some(&path));
}
/// Bytecode should only be generated for one of the requested files.
/// However, `solc` will compile in the same time as when requesting bytecode
/// for all contracts in all files, due to incorrectly generating bytecode for all.
fn bench_standard_json_codegen_one_file(c: &mut Criterion) {
let mut group = group(c, "Std JSON Codegen One of Many Files");
group
.sample_size(20)
.measurement_time(Duration::from_secs(15));
let path = absolute_path(STANDARD_JSON_PVM_CODEGEN_ONE_FILE_PATH);
let resolc_arguments = &["--standard-json"];
let solc_arguments = &["--standard-json"];
bench(group, resolc_arguments, solc_arguments, Some(&path));
}
fn bench_standard_json_no_codegen_per_file(c: &mut Criterion) {
let mut group = group(c, "Std JSON No Codegen");
group
.sample_size(20)
.measurement_time(Duration::from_secs(20));
let path = absolute_path(STANDARD_JSON_NO_PVM_CODEGEN_PER_FILE_PATH);
let resolc_arguments = &["--standard-json"];
let solc_arguments = &["--standard-json"];
bench(group, resolc_arguments, solc_arguments, Some(&path));
}
criterion_group!(
name = benches;
config = Criterion::default();
targets =
bench_empty,
bench_dependency,
bench_large_div_rem,
bench_memset,
bench_return,
bench_standard_json_codegen_per_file,
bench_standard_json_codegen_one_file,
bench_standard_json_no_codegen_per_file,
);
criterion_main!(benches);