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

Separate DependentCost into LightOperation and HeavyOperation #611

Closed
xgreenx opened this issue Oct 19, 2023 · 1 comment
Closed

Separate DependentCost into LightOperation and HeavyOperation #611

xgreenx opened this issue Oct 19, 2023 · 1 comment
Assignees

Comments

@xgreenx
Copy link
Collaborator

xgreenx commented Oct 19, 2023

Problem overview

A DependentCost is currently measured by the formula:

$cost(x) = base + \frac{x}{\alpha}$

where:

  • $base$ is the minimum cost (y intercept)
  • $\alpha$ is units per gas: $\alpha = \frac{U}{g}$. This is the number of units that will fit into 1 gas
  • $x$ is the number of units acting as input to the function being measured

This works well when the operation is relatively cheap, and $\alpha$ is a large number: we can fit (process) many units $U$ into a 1 gas. Integer division of $\frac{x}{\alpha}$ will provide sane values.

When the operation is expensive enough that we can fit 1 unit into 1 gas, $\alpha$ is 1. As an operation becomes increasingly expensive, $\alpha$ approaches 0: the number of units $U$ that can fit into 1 gas becomes an increasingly smaller fraction. Because cost is stored as integers, the "resolution" of the function becomes too low: anything more expensive than 1 gas per unit is a fraction $0 < \alpha < 1$ that cannot be accurately represented with integers.

Solution

For expensive operations, we can revaluate the above formula. This formula can also be written as:

$cost(x) = base + \beta * x = \frac{1}{\alpha} * x$

where:

  • $base$ is the minimum cost (y intercept)
  • $\beta$ is the gas per unit, equivalent to the inverse of the units per gas: $\beta = \frac{1}{\alpha} = \frac{g}{U}$ is the gas per unit.
  • $x$ is the number of units acting as input to the function being measured

Instead of representing expensive operations as the number fractional units that can fit into 1 gas, we represent them as the quantity of gas it takes to process a single unit.

To support both cases, we must introduce the concept of light operations (operations that can process many units with 1 gas) and heavy operations (operations that require more than 1 gas to process a single unit).

Implementation details

  • Refactor the DependentCost struct into an enum that supports variants LightOperation and HeavyOperation
  • LightOperations resolve with the formula for multiple-units-per-gas: $cost(x) = base + \frac{x}{\alpha}$
  • HeavyOperations resolve with the formula for multiple-gas-per-unit: $cost(x) = base + \beta * x$
  • Update fuel-vm to use new enum for DependentCost
  • Update fuel-core to use new enum for DependentCost in GraphQL
  • Update benchmarks collection: determine whether an operation is "light" or "heavy" and output the corresponding enum in the output
@xgreenx xgreenx assigned bvrooman and unassigned MitchTurner Oct 30, 2023
@bvrooman bvrooman changed the title Add an upper bound for sequential opcodes Separate DependentCost into LightOperation and HeavyOperation Nov 3, 2023
@xgreenx
Copy link
Collaborator Author

xgreenx commented Nov 20, 2023

This issue is fixed by #622. We will crate a separate issue to improve our benchmarks to use more mathematical model approach

@xgreenx xgreenx closed this as completed Nov 20, 2023
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

No branches or pull requests

3 participants