Skip to content

Conversation

prin-r
Copy link
Member

@prin-r prin-r commented Aug 20, 2025

  • fix additionalGasUsed
  • test cases
  • docs of the experiment

@prin-r prin-r changed the title Gas measurement [feat] adjust additionalGas calculation / gas measurement docs Aug 20, 2025
@prin-r prin-r changed the title [feat] adjust additionalGas calculation / gas measurement docs [feat] adjust additionalGas calculation + gas measurement docs Aug 20, 2025
@prin-r prin-r changed the title [feat] adjust additionalGas calculation + gas measurement docs [feat] adjust additionalGasUsed calculation + gas measurement docs Aug 20, 2025
@prin-r prin-r force-pushed the gas-measure branch 2 times, most recently from 32dc376 to daa69e6 Compare August 24, 2025 21:10
@prin-r prin-r requested a review from sorawit September 2, 2025 10:30
Comment on lines +53 to +68
int256 a11;
int256 a12;
int256 a13;
int256 a14;
int256 a21;
int256 a22;
int256 a23;
int256 a24;
int256 a31;
int256 a32;
int256 a33;
int256 a34;
int256 a41;
int256 a42;
int256 a43;
int256 a44;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curiosity why not use fixed size array?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, we can use fixed-size arrays.
I just think that writing m.a12 is a little shorter than m[1][2] and avoids indexing out of bounds (dimensional safety).

Comment on lines +569 to +572
RegressionTestHelper.Quadratic memory quad = RegressionTestHelper
.quadratic(trainingSet);
require(quad.c2 > 0 && quad.c1 > 0 && quad.c0 > 0, "bad quad fit");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we call this bad quad fit? can we use negative coeff to have a better fit?

Copy link
Member Author

@prin-r prin-r Sep 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allowing negative values violates physical priors based on the overhead per byte cost (linear) and memory expansion (quadratic)

Comment on lines 60 to 74
function unpackCoeffs(
uint256 packed
) public pure returns (uint256 c2, uint256 c1, uint256 c0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can coeff be a negative? why not?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they can be negative if the training calldata is overwhelmed by noise (a lot of '0')
however, they should not be negative in theory

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it linear though? (also L1)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The total gas on L2 is made up of L1 and L2 gas. In the experiment, the gas on L2 is strongly linear, despite the fact that memory expansion should still occur, but its effect on L2 is insignificant.
However, the behavior of L1 gas on L2 is linear, but the maximum error can be large due to the unknown behavior of the compression algorithm and the calldata itself.

@@ -0,0 +1,1938 @@
# Estimating Tunnel Relay Gas

This document explains the **Foundry** test design, the **polynomial models** (linear / quadratic / cubic), the **Monte-Carlo** validation, and the **conclusions** for the on-chain environment.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it Monte-Carlo validation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@prin-r prin-r force-pushed the gas-measure branch 2 times, most recently from a3af018 to ae2ffc4 Compare September 24, 2025 07:57
@prin-r prin-r requested a review from nkitlabs September 24, 2025 10:13
@RogerKSI RogerKSI requested a review from Copilot September 29, 2025 10:12
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a comprehensive overhaul of gas calculation for relay operations, replacing the previous constant additionalGasUsed with a dynamic quadratic model based on calldata size. It includes extensive documentation, test infrastructure, and calibration mechanisms.

  • Replaces constant gas overhead with a quadratic model based on calldata size
  • Adds comprehensive gas measurement test infrastructure and regression analysis
  • Introduces detailed documentation explaining the mathematical modeling approach

Reviewed Changes

Copilot reviewed 16 out of 27 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/router/L1RouterGasCalculator.sol New contract implementing quadratic gas calculation model with packed coefficients
src/router/BaseTunnelRouter.sol Updated to use dynamic gas calculation instead of constant additionalGasUsed
test/RelayGasMeasurementTest.t.sol Comprehensive test suite for measuring and validating gas usage patterns
test/helper/RegressionTestHelper.sol Mathematical library for polynomial regression analysis
docs/contracts/05-relaygasused.md Extensive documentation of gas measurement methodology and analysis

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@prin-r prin-r requested a review from RogerKSI October 7, 2025 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants