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>
This commit is contained in:
LJandxermicus authored and GitHub committed 2026-01-28 23:04:22 +01:00
1 parent 95ae938ddf
commit a4163512ff
52 files changed
+3827 -187

No files matched your search

+37
View File
@@ -26,6 +26,8 @@ pub const YUL_RETURN_CONTRACT_PATH: &str = "src/tests/data/yul/return.yul";
/// The standard JSON contracts test fixture path.
pub const STANDARD_JSON_CONTRACTS_PATH: &str =
"src/tests/data/standard_json/solidity_contracts.json";
/// The standard JSON contracts test fixture path that requests every single output.
pub const STANDARD_JSON_ALL_OUTPUTS_PATH: &str = "src/tests/data/standard_json/all_outputs.json";
/// The standard JSON no EVM codegen test fixture path.
///
/// This contains EVM bytecode selection flags with provided code
@@ -37,6 +39,41 @@ pub const STANDARD_JSON_NO_EVM_CODEGEN_PATH: &str =
/// infamous "Stack too deep" error in the EVM codegen.
pub const STANDARD_JSON_NO_EVM_CODEGEN_COMPLEX_PATH: &str =
"src/tests/data/standard_json/no_evm_codegen_complex.json";
/// The standard JSON PVM codegen all wildcard test fixture path.
///
/// These contracts are similar to ones used in an example project.
pub const STANDARD_JSON_PVM_CODEGEN_ALL_WILDCARD_PATH: &str =
"src/tests/data/standard_json/pvm_codegen_all_wildcard.json";
/// The standard JSON PVM codegen for all files on a per-file basis test fixture path.
///
/// These contracts are similar to ones used in an example project.
pub const STANDARD_JSON_PVM_CODEGEN_PER_FILE_PATH: &str =
"src/tests/data/standard_json/pvm_codegen_per_file.json";
/// The standard JSON PVM codegen for one out of many files test fixture path.
///
/// These contracts are similar to ones used in an example project.
pub const STANDARD_JSON_PVM_CODEGEN_ONE_FILE_PATH: &str =
"src/tests/data/standard_json/pvm_codegen_one_file.json";
/// The standard JSON no PVM codegen for any file on a per-file basis test fixture path.
///
/// This omits `evm` bytecode selection flags, which should thereby
/// prevent PVM bytecode generation.
///
/// These contracts are similar to ones used in an example project.
pub const STANDARD_JSON_NO_PVM_CODEGEN_PER_FILE_PATH: &str =
"src/tests/data/standard_json/no_pvm_codegen_per_file.json";
/// The standard JSON Yul contract PVM codegen test fixture path.
///
/// This requests the full `evm` output object, which should thereby
/// generate all `evm` child fields.
pub const STANDARD_JSON_YUL_PVM_CODEGEN_PATH: &str =
"src/tests/data/standard_json/yul_pvm_codegen.json";
/// The standard JSON Yul contract no PVM codegen test fixture path.
///
/// This omits `evm` bytecode selection flags, which should thereby prevent
/// PVM bytecode generation and only validate the Yul.
pub const STANDARD_JSON_YUL_NO_PVM_CODEGEN_PATH: &str =
"src/tests/data/standard_json/yul_no_pvm_codegen.json";
/// The `resolc` YUL mode flag.
pub const RESOLC_YUL_FLAG: &str = "--yul";