<commit_before>package sigmastate.utxo

import org.ergoplatform.SigmaConstants
import sigmastate.{Downcast, Upcast}
import sigmastate.lang.SigmaParser
import sigmastate.lang.Terms.OperationId
import sigmastate.serialization.OpCodes
import sigmastate.serialization.OpCodes.{LastConstantCode, OpCode}
import scalan.util.Extensions.ByteOps
import sigmastate.serialization.ValueSerializer.getSerializer

import scala.collection.mutable

case class CostTable(operCosts: Map[OperationId, Int]) extends (OperationId => Int) {
  @inline private def cleanOperId(operId: OperationId): OperationId = {
    if (operId.opType.tpeParams.isEmpty) operId
    else operId.copy(opType = operId.opType.copy(tpeParams = Nil))
  }
  @inline final def get(operId: OperationId): Option[Int] = {
    val cleanId = cleanOperId(operId)
    operCosts.get(cleanId)
  }
  override def apply(operId: OperationId): Int = {
    val costOpt = this.get(operId)
    costOpt match {
      case Some(cost) =>
        // println(s"$operId -> $cost")
        cost
      case None => //costToInt(MinimalCost)
        sys.error(s"Cannot find cost in CostTable for $operId")
    }
  }
}

object CostTable {
  type ExpressionCost = Int

  val MinimalCost = 10

  val interpreterInitCost = 10000
  val perGraphNodeCost = 200

  /** Scaling factors to be applied to estimated cost (totalCost = cost * costFactorIncrease / costFactorDecrease */
  val costFactorIncrease: Int = 1
  val costFactorDecrease: Int = 1

  val expCost = 5000
  val multiplyGroup = 50
  val negateGroup = 50
  val groupElementConst = 1
  val decodePointCost = 1
  val constCost = 10
  val lambdaCost = 10

  /** Cost of creating new instances (kind of memory allocation cost).
    * When the instance already exists them the corresponding Access/Extract cost should be added.
    */
  val newPrimValueCost = 1
  val newCollValueCost = 1
  val newPairValueCost = 1
  val newOptionValueCost = 1
  val newAvlTreeCost = 10

  val plusMinus = 10
  val multiply = 10

  val plusMinusBigInt = 10
  val comparisonBigInt = 10
  val multiplyBigInt = 50
  val newBigIntPerItem = 1

  val hashPerKb = 100

  val avlTreeOp = hashPerKb * 2

  val collAccess = 5
  val collLength  = 5 // TODO costing: should be >= selectField
  val collByIndex = 5 // TODO costing: should be >= selectField

  val collToColl = 20
  val lambdaInvoke = 30  // interpreter overhead on each lambda invocation (map, filter, forall, etc)
  val concreteCollectionItemCost = 10  // since each item is a separate graph node
  val comparisonCost = 10
  val comparisonPerKbCost = 10

  val logicCost = 10
  val logicBaseCost = 0  // TODO soft-fork: increase it to 20

  val sigmaAndCost = 10
  val sigmaOrCost = 40
<commit_msg>documenting logicBaseCost<commit_after>package sigmastate.utxo

import org.ergoplatform.SigmaConstants
import sigmastate.{Downcast, Upcast}
import sigmastate.lang.SigmaParser
import sigmastate.lang.Terms.OperationId
import sigmastate.serialization.OpCodes
import sigmastate.serialization.OpCodes.{LastConstantCode, OpCode}
import scalan.util.Extensions.ByteOps
import sigmastate.serialization.ValueSerializer.getSerializer

import scala.collection.mutable

case class CostTable(operCosts: Map[OperationId, Int]) extends (OperationId => Int) {
  @inline private def cleanOperId(operId: OperationId): OperationId = {
    if (operId.opType.tpeParams.isEmpty) operId
    else operId.copy(opType = operId.opType.copy(tpeParams = Nil))
  }
  @inline final def get(operId: OperationId): Option[Int] = {
    val cleanId = cleanOperId(operId)
    operCosts.get(cleanId)
  }
  override def apply(operId: OperationId): Int = {
    val costOpt = this.get(operId)
    costOpt match {
      case Some(cost) =>
        // println(s"$operId -> $cost")
        cost
      case None => //costToInt(MinimalCost)
        sys.error(s"Cannot find cost in CostTable for $operId")
    }
  }
}

object CostTable {
  type ExpressionCost = Int

  val MinimalCost = 10

  val interpreterInitCost = 10000
  val perGraphNodeCost = 200

  /** Scaling factors to be applied to estimated cost (totalCost = cost * costFactorIncrease / costFactorDecrease */
  val costFactorIncrease: Int = 1
  val costFactorDecrease: Int = 1

  val expCost = 5000
  val multiplyGroup = 50
  val negateGroup = 50
  val groupElementConst = 1
  val decodePointCost = 1
  val constCost = 10
  val lambdaCost = 10

  /** Cost of creating new instances (kind of memory allocation cost).
    * When the instance already exists them the corresponding Access/Extract cost should be added.
    */
  val newPrimValueCost = 1
  val newCollValueCost = 1
  val newPairValueCost = 1
  val newOptionValueCost = 1
  val newAvlTreeCost = 10

  val plusMinus = 10
  val multiply = 10

  val plusMinusBigInt = 10
  val comparisonBigInt = 10
  val multiplyBigInt = 50
  val newBigIntPerItem = 1

  val hashPerKb = 100

  val avlTreeOp = hashPerKb * 2

  val collAccess = 5
  val collLength  = 5 // TODO costing: should be >= selectField
  val collByIndex = 5 // TODO costing: should be >= selectField

  val collToColl = 20
  val lambdaInvoke = 30  // interpreter overhead on each lambda invocation (map, filter, forall, etc)
  val concreteCollectionItemCost = 10  // since each item is a separate graph node
  val comparisonCost = 10
  val comparisonPerKbCost = 10

  val logicCost = 10

  /** Initial cost of starting OR/AND/XorOf operation which doesn't depend on the number
    * of elements in the arguments collection. The cost of each operation is `logicCost`.
    */
  val logicBaseCost = 0  // TODO soft-fork: increase it to 20

  val sigmaAndCost = 10
  val sigmaOrCost = 40

{'fraction_non_alphanumeric': 0.07517327172560867, 'fraction_numerical': 0.02452461347076595, 'mean_word_length': 3.6397361912613357, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3016530', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1709, 'n_words': 558}