Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow configuring optimizations runs #146

Merged
merged 4 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion ape_solidity/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ class SolidityConfig(PluginConfig):
Compile with optimization. Defaults to ``True``.
"""

optimization_runs: int = DEFAULT_OPTIMIZATION_RUNS
"""
The number of runs specifies roughly how often each opcode of the
deployed code will be executed across the lifetime of the contract.
Lower values will optimize more for initial deployment cost, higher
values will optimize more for high-frequency usage.
"""

version: Optional[str] = None
"""
Hardcode a Solidity version to use. When not set,
Expand Down Expand Up @@ -379,7 +387,7 @@ def _get_settings_from_version_map(
settings: dict = {}
for solc_version, sources in version_map.items():
version_settings: dict[str, Union[Any, list[Any]]] = {
"optimizer": {"enabled": config.optimize, "runs": DEFAULT_OPTIMIZATION_RUNS},
"optimizer": {"enabled": config.optimize, "runs": config.optimization_runs},
"outputSelection": {
str(get_relative_path(p, pm.path)): {"*": OUTPUT_SELECTION, "": ["ast"]}
for p in sorted(sources)
Expand Down
6 changes: 5 additions & 1 deletion tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,10 @@ def test_get_version_map_full_project(project, compiler):

def test_get_compiler_settings(project, compiler):
path = project.sources.lookup("contracts/Imports.sol")

# Set this setting using an adhoc approach.
compiler.compiler_settings["optimization_runs"] = 190

actual = compiler.get_compiler_settings((path,), project=project)
# No reason (when alone) to not use
assert len(actual) == 1
Expand All @@ -344,7 +348,7 @@ def test_get_compiler_settings(project, compiler):
assert version > Version("0.8.12+commit.f00d7308")

settings = actual[version]
assert settings["optimizer"] == {"enabled": True, "runs": 200}
assert settings["optimizer"] == {"enabled": True, "runs": 190}

# NOTE: These should be sorted!
assert settings["remappings"] == [
Expand Down
Loading