mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-11-10 04:37:11 +00:00
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:
1 parent
95ae938ddf
commit
a4163512ff
52 files changed
+3827
-187
No files matched your search
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user