Skip to content

feat: forge build --maximize-optimizer-runs to find max number of optimizer runs #1342

Open
@mds1

Description

@mds1

Component

Forge

Describe the feature you would like

Setting the optimizer's --optimize-runs count to 1 results in the smallest contract size, but higher runtime gas costs. As that number increases, contract size increases and gas costs decrease.

Therefore, a useful method would be forge build --optimize-size. This would basically be a binary search that keeps re-compiling the contracts until they exceed the size limit. Specifically:

  • The maximum number of optimizer runs is 2^32 - 1. Start by compiling the contracts with the number of runs set to half this value.
  • If the contracts are above the size limit, reduce the optimizer runs by half and re-build.
  • If the contracts are below the size limit, increase the optimizer runs to mean(2^32 - 1, (2^32 - 1) / 2) and re-build.
  • Repeat until we find the maximum number of optimizer runs that results in all contracts being below the size limit, and print that number of runs
  • If all attempts fail and are over the size limit, exit with an error

In practice I've noticed diminishing returns as you increase the runs very high, with 10M+ runs all typically giving the same results. Therefore we can speed up the above search by assuming the max number of runs is smaller, such as 10M or 100M

Additional context

Pulled out of #992

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions