This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement low level stack more efficiently
The high level interpreter creates a lot of objects whenever a new frame is pushed, or even at every interpretation step. Whereas this is ok for simple scripts, it is quite costly and adds a lot of overhead at runtime. The idea is to make pushing frames object creation free in the end. This lower level representation of a thread frame, is one step towards that result.
- Loading branch information
Showing
16 changed files
with
1,719 additions
and
1,358 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2018 Lucas Satabin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package swam | ||
package runtime | ||
package exports | ||
|
||
import runtime.{imports => i} | ||
import formats._ | ||
import internals.compiler._ | ||
|
||
import cats._ | ||
import cats.implicits._ | ||
|
||
import scala.language.higherKinds | ||
|
||
abstract class EFunction3[P1, P2, P3, Ret, F[_]] private (f: Function[F], m: Option[Memory[F]])( | ||
implicit F: MonadError[F, Throwable], | ||
writer1: ValueWriter[F, P1], | ||
writer2: ValueWriter[F, P2], | ||
writer3: ValueWriter[F, P3]) | ||
extends EFunction[Ret, F] | ||
with Function3[P1, P2, P3, F[Ret]] { | ||
def apply(p1: P1, p2: P2, p3: P3): F[Ret] = | ||
for { | ||
p1 <- writer1.write(p1, m) | ||
p2 <- writer2.write(p2, m) | ||
p3 <- writer3.write(p3, m) | ||
v <- f.invoke(Vector(p1, p2, p3), m) | ||
v <- wrap(v) | ||
} yield v | ||
} | ||
|
||
object EFunction3 { | ||
import EFunction._ | ||
|
||
def apply[P1, P2, P3, F[_]](name: String, self: Instance[F])( | ||
implicit F: MonadError[F, Throwable], | ||
writer1: ValueWriter[F, P1], | ||
writer2: ValueWriter[F, P2], | ||
writer3: ValueWriter[F, P3]): F[EFunction3[P1, P2, P3, Unit, F]] = | ||
self.exps.get(name) match { | ||
case Some(f: Function[F]) => | ||
val expectedt = FuncType(Vector(writer1.swamType, writer2.swamType, writer3.swamType), Vector()) | ||
if (f.tpe == expectedt) | ||
F.pure(new EFunction3[P1, P2, P3, Unit, F](f, self.memories.headOption) { | ||
def wrap(res: Option[Value]): F[Unit] = EFunction.wrapUnit[F](res) | ||
}) | ||
else | ||
F.raiseError(new RuntimeException(s"invalid return type (expected $expectedt but got ${f.tpe}")) | ||
case Some(fld) => | ||
F.raiseError(new RuntimeException(s"cannot get a function for type ${fld.tpe}")) | ||
case None => | ||
F.raiseError(new RuntimeException(s"unknown function named $name")) | ||
} | ||
|
||
def apply[P1, P2, P3, Ret, F[_]](name: String, self: Instance[F])( | ||
implicit F: MonadError[F, Throwable], | ||
writer1: ValueWriter[F, P1], | ||
writer2: ValueWriter[F, P2], | ||
writer3: ValueWriter[F, P3], | ||
reader: ValueReader[F, Ret]): F[EFunction3[P1, P2, P3, Ret, F]] = | ||
self.exps.get(name) match { | ||
case Some(f: Function[F]) => | ||
val expectedt = FuncType(Vector(writer1.swamType, writer2.swamType, writer3.swamType), Vector(reader.swamType)) | ||
if (f.tpe == expectedt) | ||
F.pure(new EFunction3[P1, P2, P3, Ret, F](f, self.memories.headOption) { | ||
def wrap(res: Option[Value]): F[Ret] = EFunction.wrap[F, Ret](res, self.memories.headOption) | ||
}) | ||
else | ||
F.raiseError(new RuntimeException(s"invalid return type (expected $expectedt but got ${f.tpe}")) | ||
case Some(fld) => | ||
F.raiseError(new RuntimeException(s"cannot get a function for type ${fld.tpe}")) | ||
case None => | ||
F.raiseError(new RuntimeException(s"unknown function named $name")) | ||
} | ||
|
||
} |
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,96 @@ | ||
/* | ||
* Copyright 2018 Lucas Satabin | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package swam | ||
package runtime | ||
package exports | ||
|
||
import runtime.{imports => i} | ||
import formats._ | ||
import internals.compiler._ | ||
|
||
import cats._ | ||
import cats.implicits._ | ||
|
||
import scala.language.higherKinds | ||
|
||
abstract class EFunction4[P1, P2, P3, P4, Ret, F[_]] private (f: Function[F], m: Option[Memory[F]])( | ||
implicit F: MonadError[F, Throwable], | ||
writer1: ValueWriter[F, P1], | ||
writer2: ValueWriter[F, P2], | ||
writer3: ValueWriter[F, P3], | ||
writer4: ValueWriter[F, P4]) | ||
extends EFunction[Ret, F] | ||
with Function4[P1, P2, P3, P4, F[Ret]] { | ||
def apply(p1: P1, p2: P2, p3: P3, p4: P4): F[Ret] = | ||
for { | ||
p1 <- writer1.write(p1, m) | ||
p2 <- writer2.write(p2, m) | ||
p3 <- writer3.write(p3, m) | ||
p4 <- writer4.write(p4, m) | ||
v <- f.invoke(Vector(p1, p2, p3, p4), m) | ||
v <- wrap(v) | ||
} yield v | ||
} | ||
|
||
object EFunction4 { | ||
import EFunction._ | ||
|
||
def apply[P1, P2, P3, P4, F[_]](name: String, self: Instance[F])( | ||
implicit F: MonadError[F, Throwable], | ||
writer1: ValueWriter[F, P1], | ||
writer2: ValueWriter[F, P2], | ||
writer3: ValueWriter[F, P3], | ||
writer4: ValueWriter[F, P4]): F[EFunction4[P1, P2, P3, P4, Unit, F]] = | ||
self.exps.get(name) match { | ||
case Some(f: Function[F]) => | ||
val expectedt = | ||
FuncType(Vector(writer1.swamType, writer2.swamType, writer3.swamType, writer4.swamType), Vector()) | ||
if (f.tpe == expectedt) | ||
F.pure(new EFunction4[P1, P2, P3, P4, Unit, F](f, self.memories.headOption) { | ||
def wrap(res: Option[Value]): F[Unit] = EFunction.wrapUnit[F](res) | ||
}) | ||
else | ||
F.raiseError(new RuntimeException(s"invalid return type (expected $expectedt but got ${f.tpe}")) | ||
case Some(fld) => | ||
F.raiseError(new RuntimeException(s"cannot get a function for type ${fld.tpe}")) | ||
case None => | ||
F.raiseError(new RuntimeException(s"unknown function named $name")) | ||
} | ||
|
||
def apply[P1, P2, P3, P4, Ret, F[_]](name: String, self: Instance[F])( | ||
implicit F: MonadError[F, Throwable], | ||
writer1: ValueWriter[F, P1], | ||
writer2: ValueWriter[F, P2], | ||
writer3: ValueWriter[F, P3], | ||
writer4: ValueWriter[F, P4], | ||
reader: ValueReader[F, Ret]): F[EFunction4[P1, P2, P3, P4, Ret, F]] = | ||
self.exps.get(name) match { | ||
case Some(f: Function[F]) => | ||
val expectedt = FuncType(Vector(writer1.swamType, writer2.swamType, writer3.swamType), Vector(reader.swamType)) | ||
if (f.tpe == expectedt) | ||
F.pure(new EFunction4[P1, P2, P3, P4, Ret, F](f, self.memories.headOption) { | ||
def wrap(res: Option[Value]): F[Ret] = EFunction.wrap[F, Ret](res, self.memories.headOption) | ||
}) | ||
else | ||
F.raiseError(new RuntimeException(s"invalid return type (expected $expectedt but got ${f.tpe}")) | ||
case Some(fld) => | ||
F.raiseError(new RuntimeException(s"cannot get a function for type ${fld.tpe}")) | ||
case None => | ||
F.raiseError(new RuntimeException(s"unknown function named $name")) | ||
} | ||
|
||
} |
Oops, something went wrong.