From 07c5fb29b7eb5dc9aa59b27f2fd0493e31cc7ecb Mon Sep 17 00:00:00 2001 From: Sergei Winitzki Date: Thu, 11 Jul 2024 23:32:40 +0200 Subject: [PATCH] wip --- .../io/chymyst/dhall/unit/PerfTest.scala | 22 +++++++------------ .../main/scala/io/chymyst/dhall/Grammar.scala | 2 ++ 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/scall-cli/src/test/scala/io/chymyst/dhall/unit/PerfTest.scala b/scall-cli/src/test/scala/io/chymyst/dhall/unit/PerfTest.scala index 460b24fc..9f2fb705 100644 --- a/scall-cli/src/test/scala/io/chymyst/dhall/unit/PerfTest.scala +++ b/scall-cli/src/test/scala/io/chymyst/dhall/unit/PerfTest.scala @@ -12,20 +12,6 @@ import java.nio.file.{Files, Paths} class PerfTest extends FunSuite with ResourceFiles with TestTimings { - test("parse schema.dhall 1") { - val file = resourceAsFile("yaml-perftest/schema.dhall").get - val results = (1 to 20).map { i => - val (_, elapsed) = elapsedNanos(Parser.parseDhallStream(new FileInputStream(file)).get.value.value) - println(s"iteration $i : schema.dhall parsed in ${elapsed / 1e9} seconds") - } - } - - test("parse schema.dhall 2") { - val file = resourceAsFile("yaml-perftest/schema.dhall").get - val (_, elapsed) = elapsedNanos(Parser.parseDhallStream(new FileInputStream(file)).get.value.value) - println(s"schema.dhall parsed in ${elapsed / 1e9} seconds") - } - test("create yaml from realistic example 1") { val file = resourceAsFile("yaml-perftest/create_yaml.dhall").get val options = YamlOptions() @@ -44,6 +30,14 @@ class PerfTest extends FunSuite with ResourceFiles with TestTimings { expect(resultYaml == expectedYaml) } + test("parse schema.dhall 20 times") { + val file = resourceAsFile("yaml-perftest/schema.dhall").get + val results = (1 to 20).map { i => + val (_, elapsed) = elapsedNanos(Parser.parseDhallStream(new FileInputStream(file)).get.value.value) + println(s"iteration $i : schema.dhall parsed in ${elapsed / 1e9} seconds") + } + } + test("parse Prelude/JSON/renderAs.dhall") { val file = resourceAsFile("dhall-lang/Prelude/JSON/renderAs.dhall").get val (_, elapsed) = elapsedNanos(Parser.parseDhallStream(new FileInputStream(file)).get.value.value) diff --git a/scall-core/src/main/scala/io/chymyst/dhall/Grammar.scala b/scall-core/src/main/scala/io/chymyst/dhall/Grammar.scala index 487df499..eefd6874 100644 --- a/scall-core/src/main/scala/io/chymyst/dhall/Grammar.scala +++ b/scall-core/src/main/scala/io/chymyst/dhall/Grammar.scala @@ -1121,6 +1121,7 @@ object Grammar { def import_expression[$: P]: P[Expression] = P( import_only | completion_expression ) + .memoize def completion_expression[$: P]: P[Expression] = P( selector_expression ~ (whsp ~ complete ~ whsp ~ selector_expression).? @@ -1132,6 +1133,7 @@ object Grammar { def selector_expression[$: P]: P[Expression] = P( primitive_expression ~ (whsp ~ "." ~ whsp ~ /* No cut here, or else (List ./imported.file) cannot be parsed. */ selector).rep ).map { case (base, selectors) => selectors.foldLeft(base)((prev, selector) => selector.chooseExpression(prev)) } + .memoize sealed trait ExpressionSelector { def chooseExpression(base: Expression): Expression = this match {