Skip to content

Commit d2a2852

Browse files
committed
Miniev implementation
1 parent ec19e7e commit d2a2852

File tree

104 files changed

+2500
-1995
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+2500
-1995
lines changed

benchmark/src/main/scala/com/wavesplatform/state/DBState.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ abstract class DBState extends ScorexLogging {
3232
AddressScheme.current = new AddressScheme { override val chainId: Byte = 'W' }
3333

3434
lazy val environment = new WavesEnvironment(
35-
AddressScheme.current.chainId,
36-
Coeval.raiseError(new NotImplementedError("`tx` is not implemented")),
37-
Coeval(levelDBWriter.height),
38-
levelDBWriter,
39-
null,
40-
DirectiveSet.contractDirectiveSet,
41-
ByteStr.empty
42-
)
35+
???,
36+
???,
37+
ByteStr.empty,
38+
DirectiveSet.contractDirectiveSet,
39+
) {
40+
override def blockchain: Blockchain = ???
41+
}
4342

4443
@TearDown
4544
def close(): Unit = {

benchmark/src/test/scala/com/wavesplatform/common/SigVerifyBenchmark.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ class SigVerifyBenchmark {
2323
def sigVerify_1Kb(st: CurveSt1Kb, bh: Blackhole): Unit =
2424
bh.consume(Curve25519.verify(st.signature, st.message, st.publicKey))
2525

26+
@Benchmark
27+
def sigVerify_1Kb_prov(st: CurveSt1Kb, bh: Blackhole): Unit =
28+
bh.consume(com.wavesplatform.curve25519.Provider.verifySignature(st.publicKey, st.message, st.signature))
29+
2630
@Benchmark
2731
def sigVerify_5Kb(st: CurveSt5Kb, bh: Blackhole): Unit =
2832
bh.consume(Curve25519.verify(st.signature, st.message, st.publicKey))
@@ -43,6 +47,10 @@ class SigVerifyBenchmark {
4347
def sigVerify_16Kb(st: CurveSt16Kb, bh: Blackhole): Unit =
4448
bh.consume(Curve25519.verify(st.signature, st.message, st.publicKey))
4549

50+
@Benchmark
51+
def sigVerify_16Kb_prov(st: CurveSt16Kb, bh: Blackhole): Unit =
52+
bh.consume(com.wavesplatform.curve25519.Provider.verifySignature(st.publicKey, st.message, st.signature))
53+
4654
@Benchmark
4755
def sigVerify_32Kb(st: CurveSt32Kb, bh: Blackhole): Unit =
4856
bh.consume(Curve25519.verify(st.signature, st.message, st.publicKey))

benchmark/src/test/scala/com/wavesplatform/lang/v1/EnvironmentFunctionsBenchmark.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ object EnvironmentFunctionsBenchmark {
167167

168168
@State(Scope.Benchmark)
169169
class AddressFromString {
170-
val ctx: EvaluationContext[Environment, Id] =
170+
val ctx: EvaluationContext[Id] =
171171
WavesContext
172172
.build(Global, DirectiveSet(V4, Account, DApp).explicitGet(), true)
173173
.evaluationContext(environment)

benchmark/src/test/scala/com/wavesplatform/lang/v1/EvaluatorV2Benchmark.scala

Lines changed: 82 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,22 @@ import java.util.concurrent.TimeUnit
44

55
import cats.Id
66
import com.wavesplatform.lang.Common
7-
import com.wavesplatform.lang.directives.values.{V1, V3}
7+
import com.wavesplatform.lang.directives.values.{V1, V3, V5, V6}
88
import com.wavesplatform.lang.v1.EvaluatorV2Benchmark.*
9-
import com.wavesplatform.lang.v1.compiler.Terms.{EXPR, IF, TRUE}
9+
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_LONG, EXPR, IF, TRUE}
1010
import com.wavesplatform.lang.v1.compiler.TestCompiler
1111
import com.wavesplatform.lang.v1.evaluator.EvaluatorV2
12-
import com.wavesplatform.lang.v1.evaluator.ctx.{EvaluationContext, LoggedEvaluationContext}
1312
import com.wavesplatform.lang.v1.evaluator.ctx.impl.PureContext
14-
import com.wavesplatform.lang.v1.traits.Environment
13+
import com.wavesplatform.lang.v1.evaluator.ctx.{DisabledLogEvaluationContext, EvaluationContext, LoggedEvaluationContext}
1514
import org.openjdk.jmh.annotations.*
1615
import org.openjdk.jmh.infra.Blackhole
1716

1817
import scala.annotation.tailrec
1918

2019
object EvaluatorV2Benchmark {
21-
val pureContext: CTX[Environment] = PureContext.build(V1, useNewPowPrecision = true).withEnvironment[Environment]
22-
val pureEvalContext: EvaluationContext[Environment, Id] = pureContext.evaluationContext(Common.emptyBlockchainEnvironment())
23-
val evaluatorV2: EvaluatorV2 = new EvaluatorV2(LoggedEvaluationContext(_ => _ => (), pureEvalContext), V1, true, true)
20+
val pureContext: CTX = PureContext.build(V1, useNewPowPrecision = true)
21+
val pureEvalContext: EvaluationContext[Id] = pureContext.evaluationContext(Common.emptyBlockchainEnvironment())
22+
val evaluatorV2: EvaluatorV2 = new EvaluatorV2(LoggedEvaluationContext(_ => _ => (), pureEvalContext), V1, true, true)
2423
}
2524

2625
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@@ -44,6 +43,45 @@ class EvaluatorV2Benchmark {
4443

4544
@Benchmark
4645
def conditions(st: Conditions, bh: Blackhole): Unit = bh.consume(eval(pureEvalContext, st.expr, V1))
46+
47+
@Benchmark
48+
def recFunc(st: RecFunc, bh: Blackhole): Unit = bh.consume {
49+
val (_, _, res) = eval(pureEvalContext, st.expr, V1)
50+
require(res == Right(CONST_LONG(13631488)), s"$res")
51+
}
52+
53+
@Benchmark
54+
def overheadCallable(st: OverheadTest, bh: Blackhole): Unit = bh.consume {
55+
val (_, comp, res) = eval(pureEvalContext, st.expr.expr, V6)
56+
require((Int.MaxValue - comp) == 1048576, s"$comp")
57+
}
58+
59+
@Benchmark
60+
def mini_funcs(st: Funcs, bh: Blackhole): Unit = bh.consume(miniEv(st.expr, pureEvalContext))
61+
62+
@Benchmark
63+
def mini_lets(st: Lets, bh: Blackhole): Unit = bh.consume(miniEv(st.expr, pureEvalContext))
64+
65+
@Benchmark
66+
def mini_custom(st: CustomFunc, bh: Blackhole): Unit = bh.consume(miniEv(st.expr, pureEvalContext))
67+
68+
@Benchmark
69+
def mini_littleCustom(st: LittleCustomFunc, bh: Blackhole): Unit = bh.consume(miniEv(st.expr, pureEvalContext))
70+
71+
@Benchmark
72+
def mini_conditions(st: Conditions, bh: Blackhole): Unit = bh.consume(miniEv(st.expr, pureEvalContext))
73+
74+
@Benchmark
75+
def mini_recFunc(st: RecFunc, bh: Blackhole): Unit = bh.consume {
76+
val (log, spentComplexity, res) = miniEv(st.expr, pureEvalContext)
77+
require(res == Right(CONST_LONG(13631488)), s"$res")
78+
}
79+
80+
@Benchmark
81+
def mini_overheadCallable(st: OverheadTest, bh: Blackhole): Unit = bh.consume {
82+
val (_, comp, res) = miniEv(st.expr.expr, pureEvalContext, 52000)
83+
require(comp == 1048576, s"$comp")
84+
}
4785
}
4886

4987
@State(Scope.Benchmark)
@@ -58,7 +96,10 @@ class Funcs {
5896
| a$count() == a$count()
5997
""".stripMargin
6098

61-
val expr = TestCompiler(V3).compileExpression(script).expr.asInstanceOf[EXPR]
99+
val expr = {
100+
val sc = TestCompiler(V6).compileExpression(script, checkSize = false)
101+
sc.expr
102+
}
62103
}
63104

64105
@State(Scope.Benchmark)
@@ -71,7 +112,22 @@ class Lets {
71112
| a$count == a$count
72113
""".stripMargin
73114

74-
val expr = TestCompiler(V3).compileExpression(script).expr.asInstanceOf[EXPR]
115+
val expr = TestCompiler(V3).compileExpression(script, checkSize = false).expr
116+
}
117+
118+
@State(Scope.Benchmark)
119+
class RecFunc {
120+
def scriptStr(size: Int) =
121+
s"""func f1(i: Int) = i + 1
122+
|${(2 to size)
123+
.map { i =>
124+
s"func f$i(${(0 until i).map(idx => s"i$idx: Int").mkString(",")}) = ${(1 until i).map(fi => s"f$fi(${(1 to fi).map(ii => s"i$ii").mkString(",")})").mkString("+")}"
125+
}
126+
.mkString("\n")}
127+
|f${size}(${(1 to size).mkString(",")})
128+
|""".stripMargin
129+
private val script: String = scriptStr(22)
130+
val expr = TestCompiler(V6).compileExpression(script, checkSize = false).expr
75131
}
76132

77133
@State(Scope.Benchmark)
@@ -115,7 +171,7 @@ class CustomFunc {
115171
| f() && f() && f() && f() && f() && f() && f()
116172
""".stripMargin
117173

118-
val expr = TestCompiler(V3).compileExpression(script).expr.asInstanceOf[EXPR]
174+
val expr = TestCompiler(V6).compileExpression(script).expr
119175
}
120176

121177
@State(Scope.Benchmark)
@@ -159,7 +215,22 @@ class LittleCustomFunc {
159215
| f()
160216
""".stripMargin
161217

162-
val expr = TestCompiler(V3).compileExpression(script).expr.asInstanceOf[EXPR]
218+
val expr = TestCompiler(V3).compileExpression(script).expr
219+
}
220+
221+
@State(Scope.Benchmark)
222+
class OverheadTest {
223+
val expr = {
224+
val n = 20
225+
val scriptTest =
226+
s"""
227+
| func f0() = true
228+
| ${(0 until n).map(i => s"func f${i + 1}() = if (f$i()) then f$i() else f$i()").mkString("\n")}
229+
| f$n()
230+
""".stripMargin
231+
println(scriptTest)
232+
TestCompiler(V5).compileExpression(scriptTest)
233+
}
163234
}
164235

165236
@State(Scope.Benchmark)

benchmark/src/test/scala/com/wavesplatform/lang/v1/PureFunctionsRebenchmark.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ import java.util.concurrent.{ThreadLocalRandom, TimeUnit}
55
import cats.Id
66
import com.google.common.primitives.Longs
77
import com.wavesplatform.common.state.ByteStr
8-
import com.wavesplatform.common.utils._
8+
import com.wavesplatform.common.utils.*
99
import com.wavesplatform.lang.directives.DirectiveSet
10-
import com.wavesplatform.lang.directives.values._
11-
import com.wavesplatform.lang.utils._
10+
import com.wavesplatform.lang.directives.values.*
11+
import com.wavesplatform.lang.utils.*
1212
import com.wavesplatform.lang.v1.FunctionHeader.Native
13-
import com.wavesplatform.lang.v1.PureFunctionsRebenchmark._
13+
import com.wavesplatform.lang.v1.PureFunctionsRebenchmark.*
1414
import com.wavesplatform.lang.v1.compiler.Terms
15-
import com.wavesplatform.lang.v1.compiler.Terms._
15+
import com.wavesplatform.lang.v1.compiler.Terms.*
1616
import com.wavesplatform.lang.v1.evaluator.ctx.EvaluationContext
1717
import com.wavesplatform.lang.v1.evaluator.ctx.impl.PureContext
1818
import com.wavesplatform.lang.v1.evaluator.{FunctionIds, Log}
19-
import com.wavesplatform.lang.v1.traits.Environment
2019
import com.wavesplatform.lang.{Common, ExecutionError, v1}
21-
import org.openjdk.jmh.annotations._
20+
import org.openjdk.jmh.annotations.*
2221
import org.openjdk.jmh.infra.Blackhole
2322

2423
import scala.util.Random
@@ -217,7 +216,7 @@ class PureFunctionsRebenchmark {
217216
}
218217

219218
object PureFunctionsRebenchmark {
220-
val context: EvaluationContext[Environment, Id] =
219+
val context: EvaluationContext[Id] =
221220
lazyContexts((DirectiveSet(V5, Account, Expression).explicitGet(), true, true))()
222221
.evaluationContext(Common.emptyBlockchainEnvironment())
223222

benchmark/src/test/scala/com/wavesplatform/lang/v1/ScriptEvaluatorBenchmark.scala

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,27 @@ import cats.kernel.Monoid
77
import com.wavesplatform.common.state.ByteStr
88
import com.wavesplatform.common.utils.{Base58, EitherExt2}
99
import com.wavesplatform.crypto.Curve25519
10-
import com.wavesplatform.lang.Global
1110
import com.wavesplatform.lang.directives.values.{V1, V4}
1211
import com.wavesplatform.lang.v1.EnvironmentFunctionsBenchmark.{curve25519, randomBytes}
1312
import com.wavesplatform.lang.v1.FunctionHeader.Native
1413
import com.wavesplatform.lang.v1.ScriptEvaluatorBenchmark.*
1514
import com.wavesplatform.lang.v1.compiler.Terms.*
16-
import com.wavesplatform.lang.v1.evaluator.Contextful.NoContext
1715
import com.wavesplatform.lang.v1.evaluator.EvaluatorV1.*
1816
import com.wavesplatform.lang.v1.evaluator.FunctionIds.{FROMBASE58, SIGVERIFY, TOBASE58}
1917
import com.wavesplatform.lang.v1.evaluator.ctx.EvaluationContext
2018
import com.wavesplatform.lang.v1.evaluator.ctx.impl.{CryptoContext, PureContext}
2119
import com.wavesplatform.lang.v1.evaluator.{EvaluatorV1, FunctionIds}
20+
import com.wavesplatform.lang.{Common, Global}
2221
import org.openjdk.jmh.annotations.*
2322
import org.openjdk.jmh.infra.Blackhole
2423

2524
import scala.util.Random
2625

2726
object ScriptEvaluatorBenchmark {
2827
val version = V1
29-
val pureEvalContext: EvaluationContext[NoContext, Id] =
30-
PureContext.build(V1, useNewPowPrecision = true).evaluationContext
31-
val evaluatorV1: EvaluatorV1[Id, NoContext] = new EvaluatorV1[Id, NoContext]()
28+
val pureEvalContext: EvaluationContext[Id] =
29+
PureContext.build(V1, useNewPowPrecision = true).evaluationContext(Common.emptyBlockchainEnvironment())
30+
val evaluatorV1: EvaluatorV1[Id] = new EvaluatorV1[Id]()
3231
}
3332

3433
@OutputTimeUnit(TimeUnit.MICROSECONDS)
@@ -93,7 +92,7 @@ class ScriptEvaluatorBenchmark {
9392

9493
@State(Scope.Benchmark)
9594
class NestedBlocks {
96-
val context: EvaluationContext[NoContext, Id] = pureEvalContext
95+
val context: EvaluationContext[Id] = pureEvalContext
9796

9897
val expr: EXPR = {
9998
val blockCount = 300
@@ -107,8 +106,9 @@ class NestedBlocks {
107106

108107
@State(Scope.Benchmark)
109108
class Base58Perf {
110-
val context: EvaluationContext[NoContext, Id] =
111-
Monoid.combine(pureEvalContext, CryptoContext.build(Global, version).evaluationContext)
109+
val context: EvaluationContext[Id] =
110+
Monoid.combine(PureContext.build(V1, useNewPowPrecision = true), CryptoContext.build(Global, version))
111+
.evaluationContext(Common.emptyBlockchainEnvironment())
112112

113113
val encode: EXPR = {
114114
val base58Count = 120
@@ -150,8 +150,8 @@ class Base58Perf {
150150

151151
@State(Scope.Benchmark)
152152
class Signatures {
153-
val context: EvaluationContext[NoContext, Id] =
154-
Monoid.combine(pureEvalContext, CryptoContext.build(Global, version).evaluationContext)
153+
val context: EvaluationContext[Id] =
154+
Monoid.combine(PureContext.build(V1, useNewPowPrecision = true), CryptoContext.build(Global, version)).evaluationContext(Common.emptyBlockchainEnvironment())
155155

156156
val expr: EXPR = {
157157
val sigCount = 20
@@ -191,7 +191,7 @@ class Signatures {
191191

192192
@State(Scope.Benchmark)
193193
class Concat {
194-
val context: EvaluationContext[NoContext, Id] = pureEvalContext
194+
val context: EvaluationContext[Id] = pureEvalContext
195195

196196
private val Steps = 180
197197

@@ -218,7 +218,7 @@ class Concat {
218218

219219
@State(Scope.Benchmark)
220220
class Median {
221-
val context: EvaluationContext[NoContext, Id] = PureContext.build(V4, useNewPowPrecision = true).evaluationContext
221+
val context: EvaluationContext[Id] = PureContext.build(V4, useNewPowPrecision = true).evaluationContext(Common.emptyBlockchainEnvironment())
222222

223223
val randomElements: Array[EXPR] =
224224
(1 to 10000).map { _ =>
@@ -260,8 +260,8 @@ class Median {
260260

261261
@State(Scope.Benchmark)
262262
class SigVerify32Kb {
263-
val context: EvaluationContext[NoContext, Id] =
264-
Monoid.combine(PureContext.build(V4, useNewPowPrecision = true).evaluationContext, CryptoContext.build(Global, V4).evaluationContext)
263+
val context: EvaluationContext[Id] =
264+
Monoid.combine(PureContext.build(V4, useNewPowPrecision = true), CryptoContext.build(Global, V4)).evaluationContext(Common.emptyBlockchainEnvironment())
265265

266266
val expr: EXPR = {
267267
val (privateKey, publicKey) = curve25519.generateKeypair
@@ -281,11 +281,11 @@ class SigVerify32Kb {
281281

282282
@State(Scope.Benchmark)
283283
class ListRemoveByIndex {
284-
val context: EvaluationContext[NoContext, Id] =
284+
val context: EvaluationContext[Id] =
285285
Monoid.combine(
286-
PureContext.build(V4, useNewPowPrecision = true).evaluationContext,
287-
CryptoContext.build(Global, V4).evaluationContext
288-
)
286+
PureContext.build(V4, useNewPowPrecision = true),
287+
CryptoContext.build(Global, V4)
288+
).evaluationContext(Common.emptyBlockchainEnvironment())
289289

290290
val list: ARR = ARR(Vector.fill(1000)(CONST_LONG(Long.MaxValue)), limited = true).explicitGet()
291291

benchmark/src/test/scala/com/wavesplatform/lang/v1/package.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.wavesplatform.lang
22

33
import cats.Id
44
import com.wavesplatform.lang.directives.values.StdLibVersion
5+
import com.wavesplatform.lang.miniev.{Ev, State}
56
import com.wavesplatform.lang.v1.FunctionHeader.Native
67
import com.wavesplatform.lang.v1.compiler.Terms
78
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_BIGINT, CONST_LONG, EXPR, FUNCTION_CALL}
@@ -10,7 +11,6 @@ import com.wavesplatform.lang.v1.evaluator.FunctionIds.POW_BIGINT
1011
import com.wavesplatform.lang.v1.evaluator.ctx.EvaluationContext
1112
import com.wavesplatform.lang.v1.evaluator.ctx.impl.Rounding
1213
import com.wavesplatform.lang.v1.evaluator.{EvaluatorV2, Log}
13-
import com.wavesplatform.lang.v1.traits.Environment
1414

1515
package object v1 {
1616
def pow(base: BigInt, basePrecision: Int, exponent: BigInt, exponentPrecision: Int, resultPrecision: Int): EXPR =
@@ -27,9 +27,12 @@ package object v1 {
2727
)
2828

2929
def eval(
30-
ctx: EvaluationContext[Environment, Id],
30+
ctx: EvaluationContext[Id],
3131
expr: EXPR,
3232
stdLibVersion: StdLibVersion
3333
): (Log[Id], Int, Either[ExecutionError, Terms.EVALUATED]) =
3434
EvaluatorV2.applyCompleted(ctx, expr, LogExtraInfo(), stdLibVersion, newMode = true, correctFunctionCallScope = true)
35+
36+
def miniEv(expr: EXPR, ctx: EvaluationContext[Id], limit: Int = Int.MaxValue): (Log[Id], Int, Either[ExecutionError, Terms.EVALUATED]) =
37+
Ev.run(expr, ???)
3538
}

benchmark/src/test/scala/com/wavesplatform/state/WavesEnvironmentBenchmark.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ object WavesEnvironmentBenchmark {
139139
LevelDBFactory.factory.open(dir, new Options)
140140
}
141141

142-
val environment: Environment[Id] = {
142+
val environment: Environment[Id] = ???/*{
143143
val state = LevelDBWriter.readOnly(db, wavesSettings)
144144
new WavesEnvironment(
145145
AddressScheme.current.chainId,
@@ -150,7 +150,7 @@ object WavesEnvironmentBenchmark {
150150
DirectiveSet.contractDirectiveSet,
151151
ByteStr.empty
152152
)
153-
}
153+
}*/
154154

155155
@TearDown
156156
def close(): Unit = {

0 commit comments

Comments
 (0)