Skip to content

Commit 1f55b93

Browse files
committed
Miniev implementation
1 parent 40df842 commit 1f55b93

File tree

97 files changed

+2451
-1978
lines changed

Some content is hidden

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

97 files changed

+2451
-1978
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(rocksDBWriter.height),
38-
rocksDBWriter,
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/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 & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ package com.wavesplatform.lang.v1
33
import java.util.concurrent.TimeUnit
44
import cats.Id
55
import com.wavesplatform.lang.Common
6-
import com.wavesplatform.lang.directives.values.{V1, V3}
6+
import com.wavesplatform.lang.directives.values.{V1, V3, V5, V6}
77
import com.wavesplatform.lang.v1.EvaluatorV2Benchmark.*
8-
import com.wavesplatform.lang.v1.compiler.Terms.{EXPR, IF, TRUE}
8+
import com.wavesplatform.lang.v1.compiler.Terms.{CONST_LONG, EXPR, IF, TRUE}
99
import com.wavesplatform.lang.v1.compiler.TestCompiler
1010
import com.wavesplatform.lang.v1.evaluator.EvaluatorV2
1111
import com.wavesplatform.lang.v1.evaluator.ctx.{DisabledLogEvaluationContext, EvaluationContext}
1212
import com.wavesplatform.lang.v1.evaluator.ctx.impl.PureContext
13-
import com.wavesplatform.lang.v1.traits.Environment
13+
import com.wavesplatform.lang.v1.evaluator.ctx.{DisabledLogEvaluationContext, EvaluationContext, LoggedEvaluationContext}
1414
import org.openjdk.jmh.annotations.*
1515
import org.openjdk.jmh.infra.Blackhole
1616

1717
import scala.annotation.tailrec
1818

1919
object EvaluatorV2Benchmark {
20-
val pureContext: CTX[Environment] = PureContext.build(V1, useNewPowPrecision = true).withEnvironment[Environment]
21-
val pureEvalContext: EvaluationContext[Environment, Id] = pureContext.evaluationContext(Common.emptyBlockchainEnvironment())
22-
val evaluatorV2: EvaluatorV2 = new EvaluatorV2(DisabledLogEvaluationContext(pureEvalContext), V1, true, true, false)
20+
val pureContext: CTX = PureContext.build(V1, useNewPowPrecision = true)
21+
val pureEvalContext: EvaluationContext[Id] = pureContext.evaluationContext(Common.emptyBlockchainEnvironment())
22+
val evaluatorV2: EvaluatorV2 = new EvaluatorV2(DisabledLogEvaluationContext(pureEvalContext), V1, true, true, false)
2323
}
2424

2525
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@@ -43,6 +43,45 @@ class EvaluatorV2Benchmark {
4343

4444
@Benchmark
4545
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+
}
4685
}
4786

4887
@State(Scope.Benchmark)
@@ -57,7 +96,10 @@ class Funcs {
5796
| a$count() == a$count()
5897
""".stripMargin
5998

60-
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+
}
61103
}
62104

63105
@State(Scope.Benchmark)
@@ -70,7 +112,22 @@ class Lets {
70112
| a$count == a$count
71113
""".stripMargin
72114

73-
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
74131
}
75132

76133
@State(Scope.Benchmark)
@@ -114,7 +171,7 @@ class CustomFunc {
114171
| f() && f() && f() && f() && f() && f() && f()
115172
""".stripMargin
116173

117-
val expr = TestCompiler(V3).compileExpression(script).expr.asInstanceOf[EXPR]
174+
val expr = TestCompiler(V6).compileExpression(script).expr
118175
}
119176

120177
@State(Scope.Benchmark)
@@ -158,7 +215,22 @@ class LittleCustomFunc {
158215
| f()
159216
""".stripMargin
160217

161-
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+
}
162234
}
163235

164236
@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, enableExecutionLog = false)
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
@@ -135,7 +135,7 @@ object WavesEnvironmentBenchmark {
135135
RDB.open(wavesSettings.dbSettings)
136136
}
137137

138-
val environment: Environment[Id] = {
138+
val environment: Environment[Id] = ???/*{
139139
val state = new RocksDBWriter(rdb, wavesSettings.blockchainSettings, wavesSettings.dbSettings)
140140
new WavesEnvironment(
141141
AddressScheme.current.chainId,
@@ -146,7 +146,7 @@ object WavesEnvironmentBenchmark {
146146
DirectiveSet.contractDirectiveSet,
147147
ByteStr.empty
148148
)
149-
}
149+
}*/
150150

151151
@TearDown
152152
def close(): Unit = {

build.sbt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,13 @@ lazy val lang =
3333

3434
lazy val `lang-jvm` = lang.jvm
3535
.settings(
36-
name := "RIDE Compiler",
37-
normalizedName := "lang",
38-
description := "The RIDE smart contract language compiler",
39-
libraryDependencies += "org.scala-js" %% "scalajs-stubs" % "1.1.0" % Provided
36+
name := "RIDE Compiler",
37+
normalizedName := "lang",
38+
description := "The RIDE smart contract language compiler",
39+
libraryDependencies ++= Seq(
40+
"org.scala-js" %% "scalajs-stubs" % "1.1.0" % Provided,
41+
Dependencies.scalaLogging
42+
)
4043
)
4144

4245
lazy val `lang-js` = lang.js
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.wavesplatform.lang.utils
2+
3+
import org.scalajs.dom.*
4+
5+
trait Logging {
6+
def trace(message: => String): Unit = println(message)
7+
}

0 commit comments

Comments
 (0)