Skip to content

Commit

Permalink
perf: remove unused ASTNode parsing (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
antazoey committed Jan 5, 2024
1 parent 289aa7e commit 03aafed
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,4 @@ version.py
*.swo

tests/node_modules
.benchmarks/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ Use the `add_library()` method from the `ape-solidity` compiler class to add the
A typical flow is:

1. Deploy the library.
1. Call `add_library()` using the Solidity compiler plugin, which will also re-compile contracts that need the library.
1. Deploy and use contracts that require the library.
2. Call `add_library()` using the Solidity compiler plugin, which will also re-compile contracts that need the library.
3. Deploy and use contracts that require the library.

For example:

Expand Down
16 changes: 2 additions & 14 deletions ape_solidity/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
from ape.utils import cached_property, get_relative_path
from eth_pydantic_types import HexBytes
from eth_utils import add_0x_prefix, is_0x_prefixed
from ethpm_types import ASTNode, PackageManifest
from ethpm_types.ast import ASTClassification
from ethpm_types import PackageManifest
from ethpm_types.source import Compiler, Content
from packaging.specifiers import SpecifierSet
from packaging.version import Version
Expand Down Expand Up @@ -455,7 +454,6 @@ def get_standard_input_json(
if not list(files_by_solc_version[solc_version]):
continue

logger.debug(f"Compiling using Solidity compiler '{solc_version}'")
cleaned_version = Version(solc_version.base_version)
solc_binary = get_executable(version=cleaned_version)
arguments = {"solc_binary": solc_binary, "solc_version": cleaned_version}
Expand Down Expand Up @@ -521,17 +519,8 @@ def compile(
if source_id in str(input_file_path):
input_contract_names.append(name)

def classify_ast(_node: ASTNode):
if _node.ast_type in ("FunctionDefinition", "FunctionDefinitionNode"):
_node.classification = ASTClassification.FUNCTION

for child in _node.children:
classify_ast(child)

for source_id, contracts_out in contracts.items():
ast_data = output["sources"][source_id]["ast"]
ast = ASTNode.model_validate(ast_data)
classify_ast(ast)
# ast_data = output["sources"][source_id]["ast"]

for contract_name, ct_data in contracts_out.items():
contract_path = base_path / source_id
Expand Down Expand Up @@ -579,7 +568,6 @@ def classify_ast(_node: ASTNode):
ct_data["userdoc"] = load_dict(ct_data["userdoc"])
ct_data["devdoc"] = load_dict(ct_data["devdoc"])
ct_data["sourcemap"] = evm_data["bytecode"]["sourceMap"]
ct_data["ast"] = ast
contract_type = ContractType.model_validate(ct_data)
contract_types.append(contract_type)
contract_versions[contract_name] = solc_version
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"pytest-cov", # Coverage analyzer plugin
"pytest-mock", # For creating and using mocks
"hypothesis>=6.2.0,<7.0", # Strategy-based fuzzer
"pytest-benchmark", # For performance tests
],
"lint": [
"black>=23.12.0,<24", # Auto-formatter and linter
Expand All @@ -21,6 +22,7 @@
"mdformat>=0.7.17", # Auto-formatter for markdown
"mdformat-gfm>=0.3.5", # Needed for formatting GitHub-flavored markdown
"mdformat-frontmatter>=0.4.1", # Needed for frontmatters-style headers in issue templates
"mdformat-pyproject>=0.0.1", # Allows configuring in pyproject.toml
],
"doc": [
"Sphinx>=6.1.3,<7", # Documentation generator
Expand Down
23 changes: 16 additions & 7 deletions tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from ape import reverts
from ape.contracts import ContractContainer
from ape.exceptions import CompilerError
from ethpm_types.ast import ASTClassification
from packaging.version import Version
from pkg_resources import get_distribution
from requests.exceptions import ConnectionError
Expand Down Expand Up @@ -50,6 +49,15 @@ def test_compile(project, contract):
assert contract.source_id == f"{contract.name}.sol"


def test_compile_performance(benchmark, compiler, project):
"""
See https://pytest-benchmark.readthedocs.io/en/latest/
"""
source_path = project.contracts_folder / "MultipleDefinitions.sol"
result = benchmark.pedantic(compiler.compile, args=([source_path],), rounds=1)
assert len(result) > 0


def test_compile_solc_not_installed(project, fake_no_installs):
assert len(project.load_contracts(use_cache=False)) > 0

Expand Down Expand Up @@ -477,12 +485,13 @@ def test_enrich_error_when_builtin(project, owner, connection):
contract.checkIndexOutOfBounds(sender=owner)


def test_ast(project, compiler):
source_path = project.contracts_folder / "MultipleDefinitions.sol"
actual = compiler.compile([source_path])[-1].ast
fn_node = actual.children[1].children[0]
assert actual.ast_type == "SourceUnit"
assert fn_node.classification == ASTClassification.FUNCTION
# TODO: Not yet used and super slow.
# def test_ast(project, compiler):
# source_path = project.contracts_folder / "MultipleDefinitions.sol"
# actual = compiler.compile([source_path])[-1].ast
# fn_node = actual.children[1].children[0]
# assert actual.ast_type == "SourceUnit"
# assert fn_node.classification == ASTClassification.FUNCTION


def test_via_ir(project, compiler):
Expand Down

0 comments on commit 03aafed

Please sign in to comment.