-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathCostItem.scala
79 lines (73 loc) · 3.02 KB
/
CostItem.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package sigma.ast
/** An item in the cost accumulation trace of a [[sigma.ast.ErgoTree]] evaluation. */
abstract class CostItem {
def opName: String
def cost: JitCost
}
/** An item in the cost accumulation trace of a [[sigma.ast.ErgoTree]] evaluation.
* Represents cost of simple operation.
* Used for debugging, testing and profiling of costing.
* @param opDesc descriptor of the ErgoTree operation
* @param costKind kind of the cost to be added to accumulator
*/
case class FixedCostItem(opDesc: OperationDesc, costKind: FixedCost) extends CostItem {
override def opName: String = opDesc.operationName
override def cost: JitCost = costKind.cost
}
object FixedCostItem {
def apply(companion: FixedCostValueCompanion): FixedCostItem = {
FixedCostItem(companion.opDesc, companion.costKind)
}
def apply(method: SMethod, costKind: FixedCost): FixedCostItem = {
FixedCostItem(MethodDesc(method), costKind)
}
}
/** An item in the cost accumulation trace of a [[sigma.ast.ErgoTree]] evaluation.
* Represents cost of an operation which depends on type (e.g. type of arguments).
* Used for debugging, testing and profiling of costing.
* @param opDesc descriptor of the ErgoTree operation
* @param costKind type based cost descriptor added to accumulator
* @param tpe concrete type on this the operation is executed
* @see [[LE]], [[GT]]
*/
case class TypeBasedCostItem(
opDesc: OperationDesc,
costKind: TypeBasedCost,
tpe: SType) extends CostItem {
override def opName: String = {
val name = opDesc.operationName
s"$name[$tpe]"
}
override def cost: JitCost = costKind.costFunc(tpe)
override def equals(obj: Any): Boolean =
(this eq obj.asInstanceOf[AnyRef]) || (obj != null && (obj match {
case that: TypeBasedCostItem =>
opDesc == that.opDesc && tpe == that.tpe
case _ => false
}))
override def hashCode(): Int = 31 * opDesc.hashCode() + tpe.hashCode()
}
object TypeBasedCostItem {
def apply(companion: ValueCompanion, tpe: SType): TypeBasedCostItem = {
TypeBasedCostItem(companion.opDesc, companion.costKind.asInstanceOf[TypeBasedCost], tpe)
}
}
/** An item in the cost accumulation trace of a [[sigma.ast.ErgoTree]] evaluation.
* Represents cost of a sequence of operation.
* Used for debugging, testing and profiling of costing.
*
* @param opDesc descriptor of the ErgoTree operation
* @param costKind descriptor of the cost added to accumulator
* @param nItems number of items in the sequence
*/
case class SeqCostItem(opDesc: OperationDesc, costKind: PerItemCost, nItems: Int)
extends CostItem {
override def opName: String = opDesc.operationName
override def cost: JitCost = costKind.cost(nItems)
/** How many data chunks in this cost item. */
def chunks: Int = costKind.chunks(nItems)
}
object SeqCostItem {
def apply(companion: PerItemCostValueCompanion, nItems: Int): SeqCostItem =
SeqCostItem(companion.opDesc, companion.costKind, nItems)
}