cairovm.codes icon indicating copy to clipboard operation
cairovm.codes copied to clipboard

Add costs to libfunctions

Open kwkr opened this issue 1 year ago • 1 comments

There is a field costs returned on the requests that runs the code that contains the information about costs for specific libfunctions. The costs object looks as follows:

{
  "costs": {
    "function_costs": {
      "main::main::main": {
        "const_cost": 200,
        "step": 0,
        "hole": 0,
        "range_checks": 0,
        "pedersen": 0,
        "poseidon": 0,
        "bitwise": 0,
        "ec_op": 0
      }
    },
    "variable_costs": {},
    "statements_costs": {
      "0": [
        {
          "statement_type": "Invocation",
          "statement_index": 0,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "1": [
        {
          "statement_type": "Invocation",
          "statement_index": 1,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],

      "2": [
        {
          "statement_type": "Invocation",
          "statement_index": 2,
          "costs": {
            "const_cost": 100,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "3": [
        {
          "statement_type": "Invocation",
          "statement_index": 3,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "4": [
        {
          "statement_type": "Invocation",
          "statement_index": 4,
          "costs": {
            "const_cost": 100,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ],
      "5": [
        {
          "statement_type": "Return",
          "statement_index": 5,
          "costs": {
            "const_cost": 0,
            "step": 0,
            "hole": 0,
            "range_checks": 0,
            "pedersen": 0,
            "poseidon": 0,
            "bitwise": 0,
            "ec_op": 0
          }
        }
      ]
    }
  }
}

The costs should be included next to the specific statements in the Sierra code here: image For this purpose, the property statements_costs should be used. It contains a mapping statement_index => array of costs for a specific statement. If there are multiple elements in the array, they should be just listed next to each other as they represent costs for different branches. The value that should be displayed is the const_cost from the costs object.

The costs above are for the following sample code

use core::felt252;

fn main() -> felt252 {
    let n = 2 + 3;
    n
}

kwkr avatar Jun 04 '24 19:06 kwkr

@kwkr can you please assign this issue to me?

ScottyDavies avatar Aug 27 '24 16:08 ScottyDavies