From 530ad0183ad1373df3f39c28f4bca50ba01e4f81 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 17 Oct 2024 16:20:28 +0200 Subject: [PATCH 1/5] Benchmark lazy infinite list to compute 10000 prime numbers --- test/Benchmarks/src/Sieve/Lazy_Sieve.enso | 43 +++++++++++++++++++++++ test/Benchmarks/src/Sieve/Main.enso | 6 ++++ 2 files changed, 49 insertions(+) create mode 100644 test/Benchmarks/src/Sieve/Lazy_Sieve.enso diff --git a/test/Benchmarks/src/Sieve/Lazy_Sieve.enso b/test/Benchmarks/src/Sieve/Lazy_Sieve.enso new file mode 100644 index 000000000000..a87640e1b0be --- /dev/null +++ b/test/Benchmarks/src/Sieve/Lazy_Sieve.enso @@ -0,0 +1,43 @@ +from Standard.Base import all + +type Stream + private Item head:Integer ~tail:Stream + +primes -> Stream = + is_prime s:Stream n:Integer = + if n%s.head == 0 then False else + if s.head*s.head > n then True else + @Tail_Call is_prime s.tail n + + find_next primes:Stream n:Integer = + n_is_prime = is_prime primes n + + if n_is_prime then Stream.Item n (find_next primes n+1) else + @Tail_Call find_next primes n+1 + + + p = Stream.Item 2 (find_next p 3) + p + + +compute_nth_prime n -> Integer = + take_nth s:Stream n:Integer -> Integer = + if n <= 1 then s.head else + @Tail_Call take_nth s.tail n-1 + + take_nth primes n + + + +main n=Nothing = + print_nth s:Stream n:Integer|Nothing -> Nothing = + if n.is_nothing.not && n <= 0 then IO.println "" else + IO.print s.head + IO.print " " + @Tail_Call print_nth s.tail (n.if_not_nothing n-1) + + compute_and_print_nth nth:Integer|Nothing = + @Tail_Call print_nth primes nth + + compute_and_print_nth n + diff --git a/test/Benchmarks/src/Sieve/Main.enso b/test/Benchmarks/src/Sieve/Main.enso index 7023cad7dd1a..2aaeebddfe22 100644 --- a/test/Benchmarks/src/Sieve/Main.enso +++ b/test/Benchmarks/src/Sieve/Main.enso @@ -2,6 +2,8 @@ from Standard.Base import all from Standard.Test import Bench +import project.Sieve.Lazy_Sieve + import project.Sieve.Sieve_Original import project.Sieve.Sieve_Ascribed import project.Sieve.Sieve_Ascribed_With_Return_Checks @@ -36,3 +38,7 @@ collect_benches = Bench.build builder-> assert_prime <| Sieve_Java_Script.compute_nth_prime_natural_in_js 100000 group_builder.specify "Java_Script_Filter" <| assert_prime <| Sieve_Java_Script.compute_nth_prime_filter_in_js 100000 + + builder.group "Lazy_Sieve" options group_builder-> + group_builder.specify "Original" <| + assert_prime <| Lazy_Sieve.compute_nth_prime 100000 From 563f116a2e1c5d61eaab139fc91cfa0a33812802 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 17 Oct 2024 16:39:53 +0200 Subject: [PATCH 2/5] Include insight-heap-tool --- project/GraalVM.scala | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/project/GraalVM.scala b/project/GraalVM.scala index b6e1957f48a7..b4efa925c1d2 100644 --- a/project/GraalVM.scala +++ b/project/GraalVM.scala @@ -91,7 +91,8 @@ object GraalVM { ) val insightPkgs = Seq( - "org.graalvm.tools" % "insight-tool" % version + "org.graalvm.tools" % "insight-tool" % version, + "org.graalvm.tools" % "insight-heap-tool" % version ) private val espressoPkgs = From 2afcfa5fa4049cddd5b642201c51151816f2d0ae Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 17 Oct 2024 17:04:39 +0200 Subject: [PATCH 3/5] Documenting use of --heap.dump option in Enso program --- test/Benchmarks/src/Sieve/Lazy_Sieve.enso | 3 +- .../src/Sieve/heap_dump_lazy_sieve.js | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/Benchmarks/src/Sieve/heap_dump_lazy_sieve.js diff --git a/test/Benchmarks/src/Sieve/Lazy_Sieve.enso b/test/Benchmarks/src/Sieve/Lazy_Sieve.enso index a87640e1b0be..d30c6fa4ad1b 100644 --- a/test/Benchmarks/src/Sieve/Lazy_Sieve.enso +++ b/test/Benchmarks/src/Sieve/Lazy_Sieve.enso @@ -37,7 +37,8 @@ main n=Nothing = @Tail_Call print_nth s.tail (n.if_not_nothing n-1) compute_and_print_nth nth:Integer|Nothing = - @Tail_Call print_nth primes nth + p = primes + print_nth p nth compute_and_print_nth n diff --git a/test/Benchmarks/src/Sieve/heap_dump_lazy_sieve.js b/test/Benchmarks/src/Sieve/heap_dump_lazy_sieve.js new file mode 100644 index 000000000000..d096de4c6cd7 --- /dev/null +++ b/test/Benchmarks/src/Sieve/heap_dump_lazy_sieve.js @@ -0,0 +1,50 @@ +// +// Demo script showing a "serialization" of an Enso atom structure +// to a `.hprof` file which can be opened and analyzed in VisualVM +// +// Based on following tutorial: +// https://www.graalvm.org/jdk21/tools/graalvm-insight/manual/#heap-dumping +// +// Execute from `sbt` as: +// ``` +// sbt:enso> runEngineDistribution \ +// --vm.D=polyglot.heap.dump=/tmp/sieve.hprof \ +// --vm.D=polyglot.insight=Benchmarks/src/Sieve/heap_dump_lazy_sieve.js \ +// --run test/Benchmarks/src/Sieve/Lazy_Sieve.enso +// 10000 +// ``` +// +// This GraalVM script waits for the end execution of `compute_and_print_nth` function +// to store value of its `p` local variable (containing linked list of prime numbers) +// into `/tmp/sieve.hprof` - a GraalVM serialization for Truffle languages! + +insight.on( + 'return', + (ctx, frame) => { + for (let p in frame) { + print(`found ${p} with ${frame[p]} value`) + } + let value = frame.p + heap.dump({ + format: '1.0', + depth: 5000, + events: [ + { + stack: [ + { + at: ctx, + frame: { + primes: value, + }, + }, + ], + }, + ], + }) + print('Heap dump generated!') + }, + { + roots: true, + rootNameFilter: '.*compute_and_print_nth', + }, +) From a1517edb6357a0786e4b6254400bc163f1d47070 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 17 Oct 2024 17:13:53 +0200 Subject: [PATCH 4/5] scalafmtSbt --- project/GraalVM.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project/GraalVM.scala b/project/GraalVM.scala index b4efa925c1d2..cea30af89bbb 100644 --- a/project/GraalVM.scala +++ b/project/GraalVM.scala @@ -91,7 +91,7 @@ object GraalVM { ) val insightPkgs = Seq( - "org.graalvm.tools" % "insight-tool" % version, + "org.graalvm.tools" % "insight-tool" % version, "org.graalvm.tools" % "insight-heap-tool" % version ) From 33607c22972b7061f4fb099f428239d734f9e58b Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Fri, 18 Oct 2024 05:46:37 +0200 Subject: [PATCH 5/5] Start the group name with Sieve_ to sort next to the other Sieve benchmarks --- test/Benchmarks/src/Sieve/Main.enso | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Benchmarks/src/Sieve/Main.enso b/test/Benchmarks/src/Sieve/Main.enso index 2aaeebddfe22..9bd1f6ea8ccd 100644 --- a/test/Benchmarks/src/Sieve/Main.enso +++ b/test/Benchmarks/src/Sieve/Main.enso @@ -39,6 +39,6 @@ collect_benches = Bench.build builder-> group_builder.specify "Java_Script_Filter" <| assert_prime <| Sieve_Java_Script.compute_nth_prime_filter_in_js 100000 - builder.group "Lazy_Sieve" options group_builder-> + builder.group "Sieve_Lazy" options group_builder-> group_builder.specify "Original" <| assert_prime <| Lazy_Sieve.compute_nth_prime 100000