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

@@ -49,12 +49,53 @@ pub enum Flag {
}
impl Flag {
/// Whether this selection flag is required for the revive codegen.
/// Returns all flags.
pub fn all() -> &'static [Self] {
&[
Self::ABI,
Self::Metadata,
Self::Devdoc,
Self::Userdoc,
Self::MethodIdentifiers,
Self::StorageLayout,
Self::AST,
Self::Yul,
Self::EVM,
Self::EVMLA,
Self::EVMBC,
Self::EVMDBC,
Self::Assembly,
Self::Ir,
]
}
/// Returns the EVM child flags.
/// Excludes the EVM legacy assembly as it does not have a PVM equivalent.
pub fn evm_children() -> &'static [Self] {
&[
Self::MethodIdentifiers,
Self::EVMBC,
Self::EVMDBC,
Self::Assembly,
]
}
/// Returns the flags required by our compilation process.
pub fn codegen_requirements() -> &'static [Self] {
&[
Self::AST,
Self::MethodIdentifiers,
Self::Metadata,
Self::Yul,
]
}
/// Whether this selection flag is specific for the solc backend only.
///
/// Specifically, EVM bytecode and related flags should never be requested.
/// It will be replaced by PVM code anyways.
pub fn is_required_for_codegen(&self) -> bool {
!matches!(
pub fn is_evm_codegen(&self) -> bool {
matches!(
self,
Flag::EVMBC | Flag::EVMDBC | Flag::EVMLA | Flag::EVM | Flag::Assembly
)
@@ -15,13 +15,14 @@ pub struct File {
/// The per-file output selections.
#[serde(default, rename = "", skip_serializing_if = "HashSet::is_empty")]
pub per_file: HashSet<SelectionFlag>,
/// The per-contract output selections.
/// The per-contract output selections, common for all contracts.
/// Only the "all" (`*`) wildcard is available for robustness reasons.
#[serde(default, rename = "*", skip_serializing_if = "HashSet::is_empty")]
pub per_contract: HashSet<SelectionFlag>,
}
impl File {
/// A shortcut constructor.
/// Creates the selection for all contracts with arbitrary `flags`.
pub fn new(flags: Vec<SelectionFlag>) -> Self {
let mut per_file = HashSet::new();
let mut per_contract = HashSet::new();
@@ -41,6 +42,11 @@ impl File {
}
}
/// Creates the selection required by our compilation process.
pub fn new_required_for_codegen() -> Self {
Self::new(SelectionFlag::codegen_requirements().into())
}
/// Creates the selection required for test compilation (includes EVM bytecode).
pub fn new_required_for_tests() -> Self {
Self {
@@ -62,44 +68,66 @@ impl File {
self
}
/// Returns flags that are going to be automatically added by the compiler,
/// but were not explicitly requested by the user.
/// Returns flags that were not explicitly requested by the user.
///
/// Afterwards, the flags are used to prune JSON output before returning it.
/// These flags are used to prune JSON output before returning it,
/// removing any output that was automatically added but not requested.
pub fn selection_to_prune(&self) -> Self {
let required_per_file = vec![SelectionFlag::AST];
let required_per_contract = vec![
SelectionFlag::MethodIdentifiers,
SelectionFlag::Metadata,
SelectionFlag::Yul,
];
let unset_per_file = SelectionFlag::all()
.iter()
.copied()
.filter(|flag| !self.per_file.contains(flag))
.collect();
let mut unset_per_file = HashSet::with_capacity(required_per_file.len());
let mut unset_per_contract = HashSet::with_capacity(required_per_contract.len());
let requests_evm_parent = self.contains(SelectionFlag::EVM);
let evm_children = SelectionFlag::evm_children();
let requests_evm_child = self.contains_any(evm_children);
let unset_per_contract: HashSet<_> = SelectionFlag::all()
.iter()
.copied()
.filter(|flag| {
// Never prune EVM children when the EVM parent is requested.
if requests_evm_parent && evm_children.contains(flag) {
return false;
}
// Never prune the EVM parent when any of its children are requested.
if requests_evm_child && *flag == SelectionFlag::EVM {
return false;
}
!self.per_contract.contains(flag)
})
.collect();
for flag in required_per_file {
if !self.per_file.contains(&flag) {
unset_per_file.insert(flag);
}
}
for flag in required_per_contract {
if !self.per_contract.contains(&flag) {
unset_per_contract.insert(flag);
}
}
Self {
per_file: unset_per_file,
per_contract: unset_per_contract,
}
}
/// Whether the flag is requested.
pub fn contains(&self, flag: &SelectionFlag) -> bool {
/// Checks whether the `flag` is requested.
pub fn contains(&self, flag: SelectionFlag) -> bool {
match flag {
flag @ SelectionFlag::AST => self.per_file.contains(flag),
flag => self.per_contract.contains(flag),
SelectionFlag::AST => self.per_file.contains(&flag),
_ => self.per_contract.contains(&flag),
}
}
/// Checks whether any of the `flags` is requested.
pub fn contains_any(&self, flags: &[SelectionFlag]) -> bool {
flags.iter().any(|&flag| self.contains(flag))
}
/// Checks whether code generation is requested.
pub fn requests_codegen(&self) -> bool {
self.contains_any(&[
SelectionFlag::EVM,
SelectionFlag::EVMBC,
SelectionFlag::EVMDBC,
SelectionFlag::Assembly,
])
}
/// Checks whether the selection is empty.
pub fn is_empty(&self) -> bool {
self.per_file.is_empty() && self.per_contract.is_empty()
@@ -44,15 +44,15 @@ impl PerFileSelection {
Self { files }
}
/// Returns whether `path` contains the `flag` or `None` if there is no selection for `path`.
pub fn contains(&self, path: &String, flag: &Flag) -> Option<bool> {
/// Checks whether `path` contains the `flag` or `None` if there is no selection for `path`.
pub fn contains(&self, path: &String, flag: Flag) -> Option<bool> {
if let Some(file) = self.files.get(path) {
return Some(file.contains(flag));
};
None
}
/// Returns whether this is the empty per file selection.
/// Checks whether this is the empty per file selection.
pub fn is_empty(&self) -> bool {
self.files.is_empty()
}
@@ -60,8 +60,8 @@ impl PerFileSelection {
/// Removes unneeded selections.
pub fn retain(&mut self) {
for file in self.files.values_mut() {
file.per_contract.retain(Flag::is_required_for_codegen);
file.per_file.retain(Flag::is_required_for_codegen);
file.per_contract.retain(|flag| !flag.is_evm_codegen());
file.per_file.retain(|flag| !flag.is_evm_codegen());
}
}
}
@@ -69,34 +69,53 @@ impl PerFileSelection {
/// The `solc --standard-json` output selection.
#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
pub struct Selection {
/// Only the 'all' wildcard is available for robustness reasons.
/// Common configuration for all files.
#[serde(default, rename = "*", skip_serializing_if = "FileSelection::is_empty")]
/// Individual file selection configuration, required for foundry.
pub all: FileSelection,
/// Individual file selection configuration, required for foundry.
#[serde(skip_serializing_if = "PerFileSelection::is_empty", flatten)]
files: PerFileSelection,
pub files: PerFileSelection,
}
impl Selection {
/// Creates the selection with arbitrary flags.
pub fn new(flags: Vec<Flag>) -> Self {
/// Creates the selection for all contracts in all files with arbitrary `flags`.
pub fn new_all(flags: Vec<Flag>) -> Self {
Self {
all: FileSelection::new(flags),
files: Default::default(),
}
}
/// Creates the selection required by our compilation process.
pub fn new_required() -> Self {
Self::new(vec![
Flag::AST,
Flag::MethodIdentifiers,
Flag::Metadata,
Flag::Yul,
])
/// Creates the selection required by our compilation process for all contracts in all files.
pub fn new_required_for_codegen_all() -> Self {
Self::new_all(Flag::codegen_requirements().into())
}
/// Creates the selection required for test compilation (includes EVM bytecode).
/// Creates the selection required by our compilation process for each contract in
/// `output_selection` requesting code generation.
pub fn new_required_for_codegen(output_selection: &Self) -> Self {
if output_selection.all.requests_codegen() {
return Self::new_required_for_codegen_all();
}
let mut files = PerFileSelection::default();
for (file_name, file_selection) in &output_selection.files.files {
if file_selection.requests_codegen() {
files.files.insert(
file_name.to_owned(),
FileSelection::new_required_for_codegen(),
);
}
}
Self {
all: Default::default(),
files,
}
}
/// Creates the selection required for test compilation (includes EVM bytecode) for all
/// contracts in all files.
pub fn new_required_for_tests() -> Self {
Self {
all: FileSelection::new_required_for_tests(),
@@ -106,7 +125,7 @@ impl Selection {
/// Creates the selection required by Yul validation process.
pub fn new_yul_validation() -> Self {
Self::new(vec![Flag::EVM])
Self::new_all(vec![Flag::EVM])
}
/// Extends the output selection with another one.
@@ -116,10 +135,10 @@ impl Selection {
self
}
/// Returns flags that are going to be automatically added by the compiler,
/// but were not explicitly requested by the user.
/// Returns flags that were not explicitly requested by the user.
///
/// Afterwards, the flags are used to prune JSON output before returning it.
/// These flags are used to prune JSON output before returning it,
/// removing any output that was automatically added but not requested.
pub fn selection_to_prune(&self) -> Self {
Self {
all: self.all.selection_to_prune(),
@@ -127,8 +146,8 @@ impl Selection {
}
}
/// Whether the flag is requested.
pub fn contains(&self, path: &String, flag: &Flag) -> bool {
/// Checks whether the `flag` is requested.
pub fn contains(&self, path: &String, flag: Flag) -> bool {
self.files
.contains(path, flag)
.unwrap_or(self.all.contains(flag))
@@ -136,8 +155,8 @@ impl Selection {
/// Removes unneeded selections.
pub fn retain(&mut self) {
self.all.per_file.retain(Flag::is_required_for_codegen);
self.all.per_contract.retain(Flag::is_required_for_codegen);
self.all.per_file.retain(|flag| !flag.is_evm_codegen());
self.all.per_contract.retain(|flag| !flag.is_evm_codegen());
self.files.retain();
}
}
@@ -10,6 +10,9 @@ use std::collections::BTreeSet;
use serde::Deserialize;
use serde::Serialize;
#[cfg(feature = "resolc")]
use crate::SolcStandardJsonInputSettingsSelectionFileFlag;
use self::evm::EVM;
/// The `solc --standard-json` output contract.
@@ -81,4 +84,60 @@ impl Contract {
&& self.factory_dependencies.is_empty()
&& self.missing_libraries.is_empty()
}
/// Sets the field corresponding to the given `flag` to its default value,
/// resulting in the field being skipped during serialization.
pub fn reset_field_by_flag(&mut self, flag: SolcStandardJsonInputSettingsSelectionFileFlag) {
match flag {
SolcStandardJsonInputSettingsSelectionFileFlag::ABI => {
self.abi = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::Metadata => {
self.metadata = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::Devdoc => {
self.devdoc = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::Userdoc => {
self.userdoc = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::StorageLayout => {
self.storage_layout = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::Yul => {
self.ir_optimized = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::Ir => {
self.ir = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::EVM => {
self.evm = Default::default();
}
SolcStandardJsonInputSettingsSelectionFileFlag::EVMBC => {
if let Some(evm) = self.evm.as_mut() {
evm.bytecode = Default::default();
}
}
SolcStandardJsonInputSettingsSelectionFileFlag::EVMDBC => {
if let Some(evm) = self.evm.as_mut() {
evm.deployed_bytecode = Default::default();
}
}
SolcStandardJsonInputSettingsSelectionFileFlag::Assembly => {
if let Some(evm) = self.evm.as_mut() {
evm.assembly_text = Default::default();
}
}
SolcStandardJsonInputSettingsSelectionFileFlag::MethodIdentifiers => {
if let Some(evm) = self.evm.as_mut() {
evm.method_identifiers = Default::default();
}
}
SolcStandardJsonInputSettingsSelectionFileFlag::AST
| SolcStandardJsonInputSettingsSelectionFileFlag::EVMLA => {
// Ignore AST (a per-file flag) and EVMLA
// as they have no contract field mappings.
}
}
}
}
@@ -63,7 +63,10 @@ impl Output {
path.to_owned(),
Source {
id: index,
ast: source.content().map(|x| serde_json::to_value(x).unwrap()),
ast: source
.content()
.map(|x| serde_json::to_value(x).unwrap())
.unwrap_or_default(),
},
)
})
@@ -100,26 +103,20 @@ impl Output {
mut self,
selection_to_prune: SolcStandardJsonInputSettingsSelection,
) -> ! {
for (path, source) in self.sources.iter_mut() {
if selection_to_prune.contains(
path,
crate::SolcStandardJsonInputSettingsSelectionFileFlag::AST,
) {
source.ast = Default::default();
}
}
for (path, contracts) in self.contracts.iter_mut() {
for contract in contracts.values_mut() {
if selection_to_prune.contains(
path,
&crate::SolcStandardJsonInputSettingsSelectionFileFlag::Metadata,
) {
contract.metadata = serde_json::Value::Null;
}
if selection_to_prune.contains(
path,
&crate::SolcStandardJsonInputSettingsSelectionFileFlag::Yul,
) {
contract.ir_optimized = String::new();
}
if let Some(ref mut evm) = contract.evm {
if selection_to_prune.contains(
path,
&crate::SolcStandardJsonInputSettingsSelectionFileFlag::MethodIdentifiers,
) {
evm.method_identifiers.clear();
for &flag in crate::SolcStandardJsonInputSettingsSelectionFileFlag::all() {
if selection_to_prune.contains(path, flag) {
contract.reset_field_by_flag(flag);
}
}
}
@@ -153,11 +150,7 @@ impl Output {
let messages: Vec<SolcStandardJsonOutputError> = iter
.flat_map(|(_path, source)| {
source
.ast
.as_ref()
.map(|ast| Source::get_messages(ast, &id_paths, sources, suppressed_warnings))
.unwrap_or_default()
Source::get_messages(&source.ast, &id_paths, sources, suppressed_warnings)
})
.collect();
self.errors.extend(messages);
@@ -20,7 +20,8 @@ pub struct Source {
/// The source code ID.
pub id: usize,
/// The source code AST.
pub ast: Option<serde_json::Value>,
#[serde(default, skip_serializing_if = "serde_json::Value::is_null")]
pub ast: serde_json::Value,
}
#[cfg(feature = "resolc")]
@@ -29,7 +30,10 @@ impl Source {
///
/// Is used for projects compiled without `solc`.
pub fn new(id: usize) -> Self {
Self { id, ast: None }
Self {
id,
ast: Default::default(),
}
}
/// Checks the AST node for the usage of send or transfer address methods.