-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1085 from wavesplatform/EvalOpts
Evaluator Optimizations
- Loading branch information
Showing
18 changed files
with
194 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
benchmark/src/test/scala/com/wavesplatform/lang/v1/ScriptEvaluatorBenchmark.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.wavesplatform.lang.v1 | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
import com.wavesplatform.lang.TypeInfo._ | ||
import com.wavesplatform.lang.v1.ScriptEvaluatorBenchmark.St | ||
import com.wavesplatform.lang.v1.evaluator.EvaluatorV1 | ||
import com.wavesplatform.lang.v1.evaluator.ctx.impl.PureContext | ||
import org.openjdk.jmh.annotations._ | ||
import org.openjdk.jmh.infra.Blackhole | ||
|
||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@BenchmarkMode(Array(Mode.AverageTime)) | ||
@Threads(4) | ||
@Fork(1) | ||
@Warmup(iterations = 10) | ||
@Measurement(iterations = 10) | ||
class ScriptEvaluatorBenchmark { | ||
@Benchmark | ||
def apply_test(st: St, bh: Blackhole): Unit = bh.consume(EvaluatorV1[Boolean](st.context, st.expr)) | ||
} | ||
|
||
object ScriptEvaluatorBenchmark { | ||
class St extends BigSum { | ||
val context = PureContext.instance | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
lang/shared/src/main/scala/com/wavesplatform/lang/ExprEvaluator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package com.wavesplatform.lang | ||
|
||
trait ExprEvaluator extends Versioned { | ||
def apply[T: TypeInfo](ctx: version.CtxT, expr: version.ExprT): Either[(version.CtxT, ExecutionLog, ExecutionError), T] | ||
def apply[T: TypeInfo](ctx: version.CtxT, expr: version.ExprT): Either[(version.CtxT, ExecutionError), T] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
lang/shared/src/main/scala/com/wavesplatform/lang/v1/evaluator/CoevalRef.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.wavesplatform.lang.v1.evaluator | ||
|
||
import monix.eval.Coeval | ||
import monix.execution.atomic.{Atomic, _} | ||
|
||
sealed trait CoevalRef[A] { | ||
def read: Coeval[A] | ||
def write(a: A): Coeval[Unit] | ||
} | ||
|
||
object CoevalRef { | ||
def of[A, R <: Atomic[A]](a: A)(implicit ab: AtomicBuilder[A, R]): CoevalRef[A] = { | ||
new CoevalRef[A] { | ||
private val atom: Atomic[A] = Atomic(a) | ||
override def read: Coeval[A] = Coeval.delay(atom.get) | ||
override def write(a: A): Coeval[Unit] = Coeval.delay(atom.set(a)) | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
lang/shared/src/main/scala/com/wavesplatform/lang/v1/evaluator/EvalM.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.wavesplatform.lang.v1.evaluator | ||
|
||
import cats.Monad | ||
import cats.data.{EitherT, Kleisli} | ||
import cats.implicits._ | ||
import com.wavesplatform.lang.v1.evaluator.ctx.EvaluationContext | ||
import com.wavesplatform.lang.{ExecutionError, TrampolinedExecResult} | ||
import monix.eval.Coeval | ||
|
||
final case class EvalM[A](inner: Kleisli[Coeval, CoevalRef[EvaluationContext], Either[ExecutionError, A]]) { | ||
def ter(ctx: EvaluationContext): TrampolinedExecResult[A] = { | ||
val atom = CoevalRef.of(ctx) | ||
|
||
EitherT(inner.run(atom)) | ||
} | ||
|
||
def run(ctx: EvaluationContext): Either[(EvaluationContext, ExecutionError), A] = { | ||
val atom = CoevalRef.of(ctx) | ||
|
||
val action = inner | ||
.run(atom) | ||
|
||
(for { | ||
result <- action | ||
lastCtx <- atom.read | ||
} yield result.left.map(err => (lastCtx, err))).value | ||
} | ||
} | ||
|
||
object EvalM { | ||
|
||
private type MM[A] = Kleisli[Coeval, CoevalRef[EvaluationContext], A] | ||
private val M: Monad[MM] = implicitly | ||
|
||
implicit val monadInstance: Monad[EvalM] = new Monad[EvalM] { | ||
override def pure[A](x: A): EvalM[A] = | ||
EvalM(Kleisli.liftF[Coeval, CoevalRef[EvaluationContext], Either[ExecutionError, A]](Coeval.evalOnce(Right(x)))) | ||
|
||
override def flatMap[A, B](fa: EvalM[A])(f: A => EvalM[B]): EvalM[B] = { | ||
EvalM(fa.inner.flatMap({ | ||
case Right(v) => f(v).inner | ||
case Left(err) => Kleisli.pure(err.asLeft[B]) | ||
})) | ||
} | ||
|
||
override def tailRecM[A, B](a: A)(f: A => EvalM[Either[A, B]]): EvalM[B] = { | ||
EvalM(M.tailRecM(a)(f andThen (_.inner.map { | ||
case Left(err) => Right(Left(err)) | ||
case Right(Left(lv)) => Left(lv) | ||
case Right(Right(rv)) => Right(Right(rv)) | ||
}))) | ||
} | ||
} | ||
|
||
def getContext: EvalM[EvaluationContext] = | ||
EvalM(Kleisli[Coeval, CoevalRef[EvaluationContext], Either[ExecutionError, EvaluationContext]](ref => { | ||
ref.read.map(_.asRight[ExecutionError]) | ||
})) | ||
|
||
def setContext(ctx: EvaluationContext): EvalM[Unit] = | ||
EvalM(Kleisli[Coeval, CoevalRef[EvaluationContext], Either[ExecutionError, Unit]](ref => { | ||
ref.write(ctx).map(_.asRight[ExecutionError]) | ||
})) | ||
|
||
def updateContext(f: EvaluationContext => EvaluationContext): EvalM[Unit] = getContext >>= (f andThen setContext) | ||
|
||
def liftValue[A](a: A): EvalM[A] = monadInstance.pure(a) | ||
|
||
def liftError[A](err: ExecutionError): EvalM[A] = | ||
EvalM(Kleisli[Coeval, CoevalRef[EvaluationContext], Either[ExecutionError, A]](_ => Coeval.delay(err.asLeft[A]))) | ||
|
||
def liftTER[A](ter: Coeval[Either[ExecutionError, A]]): EvalM[A] = | ||
EvalM(Kleisli[Coeval, CoevalRef[EvaluationContext], Either[ExecutionError, A]](_ => ter)) | ||
} |
Oops, something went wrong.