-
Notifications
You must be signed in to change notification settings - Fork 20.4k
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
Optimize gas estimation #28589
Optimize gas estimation #28589
Conversation
From my tests, it reduces the number of tx simulations for each gas estimation down from 16-18 to just 2 for most transactions (actually for all the transactions that I've tried).
Hmm, this is a very interesting idea. Would be nice to see if reducing it further works or this is the "minimum" (at least for what percentage of the txs that's the case). By reducing it further I mean as a "test", not in live code. Just to evaluate the idea better. |
@karalabe thanks for taking a look so quickly! From my tests (just a couple) it returns the same value as the binary search approach does. I personally can't come up with a hypothetical scenario where it doesn't, but I can be missing some EVM internals knowledge. |
16-18 really? Sounds very large, but I may be wrong. The thing is, the gas estimator is not a 'balanced' binary searcher between floor and ceiling, it is skewed to stick closer to the floor, and so it should land pretty close already. // Most txs don't need much higher gas limit than their gas used, and most txs don't
// require near the full block limit of gas, so the selection of where to bisect the
// range here is skewed to favor the low side.
mid = lo * 2 |
The 16-18 is correct FWIW, that's what the current one does. |
Wrote up a tester that runs various gas estimation algos and their combinations on every transaction that comes in on mainnet. The idea in this PR does capture a few transactions, reducing their estimation iterations to 2. However, most of the time it's not because it captures the refunds correctly, rather because there are no refunds. For simpler transactions, the initial gascap iteration calculates the It is relatively rare that this optimization produces a hit however. I verified this by running both the early return in this PR and a modified version which does a hi-cap instead of returning. It is very rare that the early return version produces a valid estimation but the hi-capper iterates to calculate a result. That kind of means, that for non-trivial transactions, this optimization fails to pick a correct gas estimate. My guess is that the optimization is borked by two EVM mechanisms: the 2300 gas stipend and the 63/64 gas forwarding rules. In both of these cases, the outer call needs more gas than gasUsed (with or without gasRefund). I need to check what happens if I bump the optimistic gas still further up by +2300 + 1/64. That should (TM) cover these two machanics. |
This captures almost all transactions successfully. |
Oh nice catches! And great work creating that "test on any new TX" setup for validating these different ideas - I had the same thought. You say it captures "almost all transactions successfully" - can you elaborate or give examples for transactions that this doesn't work for? |
Unsure, will look into them a bit more Monday. I'm cross building from arm to amd64 docker images (to deploy on our test infra) so trying every idea takes 6 minutes+, which is a very annoying way to debug. |
I don't think that #28600 implemented optimizations, just moved it to a separate package |
Yeah, I wanted to do the clean move first and then mess with the optimizations when they can be reviewed alone. |
I've cherry picked this PR onto the rest of the optimizations I've introduced in #28618, closing this, but PTAL at the new one. |
From my tests, it reduces the number of tx simulations for each gas estimation down from 16-18 to just 2 for most transactions (actually for all the transactions that I've tried).
The gist is that after the first simulation, it optimistically tries a second simulation with
gasLimit
set tousedGas + gasRefund
and if it succeeds, it returns that value.This change was inspired by @karalabe's tweet:
https://twitter.com/peter_szilagyi/status/1726884606492999754