generated from finos/software-project-blueprint
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Significantly faster IR Parsing (#384)
* Significantly faster IR Parsing * Cleanup
- Loading branch information
1 parent
41f0fc1
commit 89311e9
Showing
4 changed files
with
130 additions
and
31 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
50 changes: 50 additions & 0 deletions
50
morphir/runtime/test/jvm/src/org/finos/morphir/runtime/parsing/ParseTest.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,50 @@ | ||
package org.finos.morphir.runtime.parsing | ||
|
||
import org.finos.morphir.ir.MorphirIRFile | ||
import org.finos.morphir.ir.distribution.Distribution | ||
import org.finos.morphir.runtime.MorphirIRDecodingError | ||
import zio.System.os | ||
import zio.json.* | ||
import zio.{Runtime, Unsafe, ZIO} | ||
|
||
import java.nio.charset.StandardCharsets | ||
import java.nio.file.{Files, Paths} | ||
|
||
object ParseTest { | ||
def run[E, A](zio: ZIO[Any, E, A]) = Unsafe.unsafe { implicit u => | ||
Runtime.default.unsafe.run(zio).getOrThrowFiberFailure() | ||
} | ||
|
||
def loadDistributionFromFileZIO(fileName: String) = { | ||
import org.finos.morphir.ir.json.MorphirJsonSupport.* | ||
val start = System.currentTimeMillis() | ||
val output = | ||
run { | ||
for { | ||
fileContents <- ZIO.readFile(fileName) | ||
morphirIRFile <- ZIO.fromEither(fileContents.fromJson[MorphirIRFile]) | ||
.mapError(MorphirIRDecodingError(_)) | ||
} yield morphirIRFile | ||
} | ||
val end = System.currentTimeMillis() | ||
println(s"Total loading time: ${(end - start).toDouble / 1000.toDouble} for ${fileName}") | ||
output | ||
} | ||
|
||
def writeDistrubtionToFile(file: MorphirIRFile, path: String) = { | ||
import org.finos.morphir.ir.json.MorphirJsonSupport.* | ||
val newFileJson = file.toJson | ||
Files.write( | ||
Paths.get(path), | ||
newFileJson.getBytes(StandardCharsets.UTF_8) | ||
) | ||
} | ||
|
||
def main(args: Array[String]): Unit = { | ||
println("== Starting File Load experiment ==") | ||
val originalFile = loadDistributionFromFileZIO("examples/morphir-elm-projects/evaluator-tests/morphir-ir.json") | ||
writeDistrubtionToFile(originalFile, "examples/morphir-elm-projects/evaluator-tests/morphir-ir2.json") | ||
val newFile = loadDistributionFromFileZIO("examples/morphir-elm-projects/evaluator-tests/morphir-ir2.json") | ||
println("== Loaded New File ==") | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
morphir/toolkit/codec/zio/json/src/zio/json/TagBasedParser.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,42 @@ | ||
package zio.json | ||
|
||
import org.finos.morphir.ir.Value.Value | ||
import zio.json.JsonDecoder.{JsonError, UnsafeJson} | ||
import zio.json.internal.RetractReader | ||
|
||
import scala.util.control.Breaks.{break, breakable} | ||
|
||
case class TagBasedParser[T](pf: PartialFunction[String, JsonDecoder[T]]) extends JsonDecoder[T] { | ||
override def unsafeDecode(trace: List[JsonError], inRaw: RetractReader): T = { | ||
val in = new zio.json.internal.WithRecordingReader(inRaw, 64) | ||
|
||
def err(msg: String) = throw UnsafeJson(JsonError.Message(msg) :: trace) | ||
|
||
if (in.nextNonWhitespace() != '[') err("Expected first char to be '['") | ||
if (in.nextNonWhitespace() != '"') err("Expected second char to be quote ('\"')") | ||
|
||
val buff = new StringBuffer() | ||
var readChar: Char = 0 | ||
breakable { | ||
while (true) { | ||
readChar = in.nextNonWhitespace() | ||
if (readChar != 0 && readChar.isLetter) { | ||
// buff.append returns itself (so that you can chain it) but we don't care about that here | ||
val _ = buff.append(readChar) | ||
} else { | ||
break() | ||
} | ||
} | ||
} | ||
|
||
// rewind to the beginning of the clause | ||
in.rewind() | ||
val term = buff.toString | ||
|
||
val output: T = | ||
if (pf.isDefinedAt(term)) pf.apply(term).unsafeDecode(trace, in) | ||
else err(s"Undefined heading: '${term}'") | ||
|
||
output | ||
} | ||
} |