From aefd19e07fe9f61db455ab564fa81208bdfddeab Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Thu, 2 May 2024 18:32:22 +0300 Subject: [PATCH 1/3] removing unused SigmaConstants values --- .../scala/sigma/data/SigmaConstants.scala | 59 ++++--------------- .../sigma/serialization/CoreSerializer.scala | 2 - .../org/ergoplatform/ErgoLikeContext.scala | 3 - 3 files changed, 12 insertions(+), 52 deletions(-) diff --git a/core/shared/src/main/scala/sigma/data/SigmaConstants.scala b/core/shared/src/main/scala/sigma/data/SigmaConstants.scala index 7198d5dc09..ebd17c20c4 100644 --- a/core/shared/src/main/scala/sigma/data/SigmaConstants.scala +++ b/core/shared/src/main/scala/sigma/data/SigmaConstants.scala @@ -5,10 +5,9 @@ import sigma.util.CollectionUtil.TraversableOps // used in Scala 2.11 /** Descriptor of a constant which represents some size value. * @tparam T type of the constant value * @param value value of the constant - * @param id unique id of the constant * @param description description of the constant purpose */ -case class SizeConstant[T: Numeric](value: T, id: Short, description: String) +case class SizeConstant[T: Numeric](value: T, description: String) /** Constants facade that provide access to the values used in sigma's logic and checks. * All the constants are collected in a sequence. @@ -21,82 +20,48 @@ case class SizeConstant[T: Numeric](value: T, id: Short, description: String) */ object SigmaConstants { - object MaxBoxSize extends SizeConstant[Int](4 * 1024, 1, + object MaxBoxSize extends SizeConstant[Int](4 * 1024, "Box size should not be greater than provided value") { } - object MaxTreeDepth extends SizeConstant[Int](110, 2, + object MaxTreeDepth extends SizeConstant[Int](110, "Max tree depth should not be greater then provided value") { } - object MaxTokens extends SizeConstant[Int](255, 3, + object MaxTokens extends SizeConstant[Int](255, "Tokens count should not be greater than provided value") { } - object MaxRegisters extends SizeConstant[Int](10, 4, + object MaxRegisters extends SizeConstant[Int](10, "Registers count should not be greater than provided value") { } - object MaxPropositionBytes extends SizeConstant[Int](4096 /*4K*/, 5, + object MaxPropositionBytes extends SizeConstant[Int](4096 /*4K*/, "Max length of Box.propositionBytes collection") { } - object MaxBoxSizeWithoutRefs extends SizeConstant[Int]( - MaxBoxSize.value - (32/*CryptoConstants.hashLength*/ + 2/*size of Short*/), 6, - "Box size should not be greater than provided value") { - } - - object MaxBigIntSizeInBytes extends SizeConstant[Long](32L, 7, + object MaxBigIntSizeInBytes extends SizeConstant[Long](32L, "BigInt size in bytes should not be greater than provided value") { } - object MaxSigmaPropSizeInBytes extends SizeConstant[Long](1024L, 8, + object MaxSigmaPropSizeInBytes extends SizeConstant[Long](1024L, "SigmaProp size in bytes should not be greater than provided value") { } - object MaxTupleLength extends SizeConstant[Int](255, 9, + object MaxTupleLength extends SizeConstant[Int](255, "Tuple length should not be greater than provided value") { } - object MaxHeaders extends SizeConstant[Int](10, 10, - "Headers count should not be greater than provided value") { - } - - object MaxChildrenCountForAtLeastOp extends SizeConstant[Int](255, 11, + object MaxChildrenCountForAtLeastOp extends SizeConstant[Int](255, "Max children count should not be greater than provided value") { } - object MaxLoopLevelInCostFunction extends SizeConstant[Int](1, 13, - "Maximum allowed loop level in a cost function") { - } - - object VotesArraySize extends SizeConstant[Int](3, 14, + object VotesArraySize extends SizeConstant[Int](3, "Size of of Header.votes array") { } - object AutolykosPowSolutionNonceArraySize extends SizeConstant[Int](8, 15, + object AutolykosPowSolutionNonceArraySize extends SizeConstant[Int](8, "size of nonce array from Autolykos POW solution in Header.powNonce array") { } - /** List of registered size constants with unique ids. */ - val ConstTable: Seq[SizeConstant[_]] = { - val rows = Seq( - MaxBoxSize, - MaxTreeDepth, - MaxTokens, - MaxRegisters, - MaxPropositionBytes, - MaxBoxSizeWithoutRefs, - MaxBigIntSizeInBytes, - MaxSigmaPropSizeInBytes, - MaxTupleLength, - MaxHeaders, - MaxChildrenCountForAtLeastOp, - MaxLoopLevelInCostFunction, - VotesArraySize, - AutolykosPowSolutionNonceArraySize - ) - require(rows.length == rows.distinctBy(_.id).length, s"Duplicate constant id in $rows") - rows - } } diff --git a/core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala b/core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala index 938d3f22c1..f9a6dc53ec 100644 --- a/core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala +++ b/core/shared/src/main/scala/sigma/serialization/CoreSerializer.scala @@ -27,8 +27,6 @@ abstract class CoreSerializer[TFamily, T <: TFamily] extends Serializer[TFamily, } object CoreSerializer { - /** Max length of Box.propositionBytes collection */ - val MaxPropositionSize: Int = SigmaConstants.MaxPropositionBytes.value /** Max tree depth should not be greater then provided value */ val MaxTreeDepth: Int = SigmaConstants.MaxTreeDepth.value diff --git a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala index e421e8fdfe..02b2efc41a 100644 --- a/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala +++ b/interpreter/shared/src/main/scala/org/ergoplatform/ErgoLikeContext.scala @@ -211,9 +211,6 @@ object ErgoLikeContext { type Height = Int - /** Maximimum number of headers in `headers` collection of the context. */ - val MaxHeaders = SigmaConstants.MaxHeaders.value - /** Copies the given context allowing also to update fields. * NOTE: it can be used ONLY for instances of ErgoLikeContext. * @tparam T used here to limit use of this method to only ErgoLikeContext instances From 35e280565b699f139a9876554192cc3f2fe4ba76 Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Thu, 2 May 2024 18:43:33 +0300 Subject: [PATCH 2/3] removing unused code from SigmaDslTesting --- .../test/scala/sigma/SigmaDslTesting.scala | 25 ------------------- 1 file changed, 25 deletions(-) diff --git a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala index 46222d9fb1..d7056d4ccd 100644 --- a/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala +++ b/sc/shared/src/test/scala/sigma/SigmaDslTesting.scala @@ -58,16 +58,6 @@ class SigmaDslTesting extends AnyPropSpec override val okMeasureOperationTime: Boolean = true } - def checkEq2[A,B,R](f: (A, B) => R)(g: (A, B) => R): (A,B) => Unit = { (x: A, y: B) => - val r1 = f(x, y); val r2 = g(x, y) - assert(r1.getClass == r2.getClass) - assert(r1 == r2) - } - - def getArrayIndex(len: Int): Int = { - val index = Gen.choose(0, len - 1) - index.sample.get - } /** Generate indices for an array of a given length. * @return unordered array of indices with possibly repeated elements @@ -99,10 +89,6 @@ class SigmaDslTesting extends AnyPropSpec val LogScriptDefault: Boolean = false - val isNewVersion = new scala.util.DynamicVariable(false) - - val predefScripts = Seq[String]() - /** Descriptor of the language feature. * Each language feature is described by so called feature-function which exercises * some specific operation under test. @@ -433,17 +419,6 @@ class SigmaDslTesting extends AnyPropSpec |""".stripMargin) } - private def checkEqualResults(res1: Try[VerificationResult], res2: Try[VerificationResult]): Unit = { - (res1, res2) match { - case (Success((v1, c1)), Success((v2, c2))) => - v1 shouldBe v2 - case (Failure(t1), Failure(t2)) => - rootCause(t1) shouldBe rootCause(t2) - case _ => - res1 shouldBe res2 - } - } - private def checkExpectedResult( res: Try[VerificationResult], expectedCost: Option[Int]): Unit = { res match { From 16a60bb59e480c6c937d3e3bca13a7b80ae0fd40 Mon Sep 17 00:00:00 2001 From: Alexander Chepurnoy Date: Fri, 3 May 2024 18:54:58 +0300 Subject: [PATCH 3/3] eliminating ProductOps and DBufferOps --- .../main/scala/sigma/util/Extensions.scala | 26 +------------------ .../scala/sigmastate/eval/CProfiler.scala | 18 ++++++++++--- 2 files changed, 15 insertions(+), 29 deletions(-) diff --git a/core/shared/src/main/scala/sigma/util/Extensions.scala b/core/shared/src/main/scala/sigma/util/Extensions.scala index 624b3f5d6b..5b7b7ab089 100644 --- a/core/shared/src/main/scala/sigma/util/Extensions.scala +++ b/core/shared/src/main/scala/sigma/util/Extensions.scala @@ -231,19 +231,6 @@ object Extensions { def ?:(whenNone: => T): T = if (opt.isDefined) opt.get else whenNone } - implicit class ProductOps(val source: Product) extends AnyVal { - def toArray: Array[Any] = { - val arity = source.productArity - val res = new Array[Any](arity) - var i = 0 - while (i < arity) { - res(i) = source.productElement(i) - i += 1 - } - res - } - } - implicit class ByteBufferOps(val buf: ByteBuffer) extends AnyVal { def toBytes: Array[Byte] = { val res = new Array[Byte](buf.position()) @@ -304,18 +291,6 @@ object Extensions { def showToString: String = toECPoint.showECPoint } - implicit class DBufferOps[A](val buf: DBuffer[A]) extends AnyVal { - /** Sum all values in `buf` using the given Numeric. */ - def sumAll(implicit n: Numeric[A]): A = { - val limit = buf.length - var result: A = n.zero - cfor(0)(_ < limit, _ + 1) { i => - result = n.plus(result, buf.elems(i)) - } - result - } - } - implicit class SigmaBooleanOps(val sb: SigmaBoolean) extends AnyVal { /** Wraps SigmaBoolean into SigmaProp. */ def toSigmaProp: SigmaProp = CSigmaProp(sb) @@ -344,4 +319,5 @@ object Extensions { /** Extracts [[sigma.AvlTreeData]] from the AvlTree instance. */ def toAvlTreeData: AvlTreeData = tree.asInstanceOf[CAvlTree].wrappedValue } + } diff --git a/interpreter/shared/src/main/scala/sigmastate/eval/CProfiler.scala b/interpreter/shared/src/main/scala/sigmastate/eval/CProfiler.scala index d07153bd51..a65b16acbb 100644 --- a/interpreter/shared/src/main/scala/sigmastate/eval/CProfiler.scala +++ b/interpreter/shared/src/main/scala/sigmastate/eval/CProfiler.scala @@ -1,10 +1,10 @@ package sigmastate.eval -import debox.{sp, Buffer => DBuffer, Map => DMap} +import debox.{cfor, sp, Buffer => DBuffer, Map => DMap} import sigma.ast.{CostItem, FixedCost, FixedCostItem, JitCost, SMethod, SeqCostItem, TypeBasedCostItem} import sigma.ast.TypeCodes.LastConstantCode import sigma.ast.syntax._ -import sigma.util.Extensions.{ByteOps, DBufferOps} +import sigma.util.Extensions.ByteOps import sigma.ast.{MethodCall, PropertyCall} import sigma.eval.Profiler import sigma.serialization.ValueCodes.OpCode @@ -40,8 +40,18 @@ abstract class StatHolder[@sp (Long, Double) V] { class StatCollection[@sp(Int) K, @sp(Long, Double) V] (implicit n: Integral[V], ctK: ClassTag[K], ctV: ClassTag[V]) { + /** Sum all values in `buf` using the given Numeric. */ + private def sumAll[A](buf: DBuffer[A], n: Numeric[A]): A = { + val limit = buf.length + var result: A = n.zero + cfor(0)(_ < limit, _ + 1) { i => + result = n.plus(result, buf.elems(i)) + } + result + } + private def calcAvg(buf: DBuffer[V]): V = { - n.quot(buf.sumAll, n.fromInt(buf.length)) + n.quot(sumAll(buf, n), n.fromInt(buf.length)) } // NOTE: this class is mutable so better to keep it private @@ -58,7 +68,7 @@ class StatCollection[@sp(Int) K, @sp(Long, Double) V] } override def count: Int = dataPoints.length - override def sum: V = dataPoints.sumAll + override def sum: V = sumAll(dataPoints, n) override def avg: V = calcAvg(dataPoints) override def mean: (V, Int) = {