witnet / elliptic-curve-solidity

Elliptic Curve arithmetic operations written in Solidity
MIT License
168 stars 41 forks source link

perf: cache output of sqrt(nn) #22

Closed tmpolaczyk closed 4 years ago

tmpolaczyk commented 4 years ago

Because nn is constant. It reuses a variable to avoid increasing the stack size, just pretend that instead of test[0] you see sqrt_nn.

The only affected method is _decomposeScalar, where the improvement is from 937567 average gas used to 73016 (12x).

Output of yarn gas-analysis:

Old:

········································|···························|·············|·····························
|  Methods                              ·               20 gwei/gas               ·       213.25 usd/eth       │
··················|·····················|·············|·············|·············|··············|··············
|  Contract       ·  Method             ·  Min        ·  Max        ·  Avg        ·  # calls     ·  usd (avg)  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _decomposeScalar   ·     653611  ·    1078579  ·     937567  ·         134  ·       4.00  │
··················|·····················|·············|·············|·············|··············|··············

New:

········································|···························|·············|·····························
|  Methods                              ·               20 gwei/gas               ·       212.91 usd/eth       │
··················|·····················|·············|·············|·············|··············|··············
|  Contract       ·  Method             ·  Min        ·  Max        ·  Avg        ·  # calls     ·  usd (avg)  │
··················|·····················|·············|·············|·············|··············|··············
|  EcGasHelper    ·  _decomposeScalar   ·      65071  ·      78215  ·      73016  ·         134  ·       0.31  │
··················|·····················|·············|·············|·············|··············|··············
tmpolaczyk commented 4 years ago

I had a better idea, see #23