From 0a95c7ea7f3110b4eb969d12bd44d6d44fd4dbe5 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 24 Aug 2023 18:16:18 -0400 Subject: [PATCH 1/2] use recursion -- slow --- benchmark/benchmarks.jl | 2 +- perf.md | 223 ++++++++++++++++++++-------------------- src/ScopedValues.jl | 41 +++++--- src/payloadlogger.jl | 6 +- test/runtests.jl | 2 +- 5 files changed, 144 insertions(+), 130 deletions(-) diff --git a/benchmark/benchmarks.jl b/benchmark/benchmarks.jl index 2b5b290..25137d1 100644 --- a/benchmark/benchmarks.jl +++ b/benchmark/benchmarks.jl @@ -30,7 +30,7 @@ SUITE["BASIC"]["scoped with assignment"] = @benchmarkable scoped(emptyf, svar => SUITE["BASIC"]["scoped with assignment & ref"] = @benchmarkable scoped(()->svar[], svar => 2) function setup_scope(N) - scope = nothing + scope = ScopedValues.DefaultScope() for i in 1:N scope = ScopedValues.Scope(scope, depth, i) end diff --git a/perf.md b/perf.md index 3e80cb9..cbe203e 100644 --- a/perf.md +++ b/perf.md @@ -1,7 +1,7 @@ # Benchmark Report for */home/vchuravy/src/ScopedVariables* ## Job Properties -* Time of benchmark: 18 Aug 2023 - 11:46 +* Time of benchmark: 24 Aug 2023 - 18:14 * Package commit: dirty * Julia commit: 661654 * Julia command flags: None @@ -17,106 +17,106 @@ An empty cell means that the value was zero. | ID | time | GC time | memory | allocations | |---------------------------------------------|----------------:|--------:|---------------:|------------:| -| `["BASIC", "scoped with assignment & ref"]` | 81.985 ns (5%) | | 112 bytes (1%) | 4 | -| `["BASIC", "scoped with assignment"]` | 73.731 ns (5%) | | 112 bytes (1%) | 4 | -| `["BASIC", "scoped"]` | 1.249 ns (5%) | | | | -| `["BASIC", "unscoped"]` | 2.980 ns (5%) | | | | -| `["DEPTH", "access, depth=1"]` | 2.981 ns (5%) | | | | -| `["DEPTH", "access, depth=10"]` | 7.013 ns (5%) | | | | -| `["DEPTH", "access, depth=11"]` | 7.750 ns (5%) | | | | -| `["DEPTH", "access, depth=12"]` | 8.206 ns (5%) | | | | -| `["DEPTH", "access, depth=13"]` | 8.729 ns (5%) | | | | -| `["DEPTH", "access, depth=14"]` | 9.677 ns (5%) | | | | -| `["DEPTH", "access, depth=15"]` | 10.180 ns (5%) | | | | -| `["DEPTH", "access, depth=16"]` | 10.683 ns (5%) | | | | -| `["DEPTH", "access, depth=17"]` | 11.491 ns (5%) | | | | -| `["DEPTH", "access, depth=18"]` | 12.627 ns (5%) | | | | -| `["DEPTH", "access, depth=19"]` | 13.409 ns (5%) | | | | -| `["DEPTH", "access, depth=2"]` | 3.723 ns (5%) | | | | -| `["DEPTH", "access, depth=20"]` | 14.412 ns (5%) | | | | -| `["DEPTH", "access, depth=21"]` | 15.200 ns (5%) | | | | -| `["DEPTH", "access, depth=22"]` | 16.382 ns (5%) | | | | -| `["DEPTH", "access, depth=23"]` | 17.172 ns (5%) | | | | -| `["DEPTH", "access, depth=24"]` | 18.127 ns (5%) | | | | -| `["DEPTH", "access, depth=25"]` | 19.128 ns (5%) | | | | -| `["DEPTH", "access, depth=26"]` | 20.103 ns (5%) | | | | -| `["DEPTH", "access, depth=27"]` | 21.088 ns (5%) | | | | -| `["DEPTH", "access, depth=28"]` | 22.105 ns (5%) | | | | -| `["DEPTH", "access, depth=29"]` | 23.316 ns (5%) | | | | -| `["DEPTH", "access, depth=3"]` | 3.971 ns (5%) | | | | -| `["DEPTH", "access, depth=30"]` | 24.054 ns (5%) | | | | -| `["DEPTH", "access, depth=31"]` | 25.028 ns (5%) | | | | -| `["DEPTH", "access, depth=32"]` | 26.045 ns (5%) | | | | -| `["DEPTH", "access, depth=4"]` | 3.964 ns (5%) | | | | -| `["DEPTH", "access, depth=5"]` | 3.970 ns (5%) | | | | -| `["DEPTH", "access, depth=6"]` | 4.474 ns (5%) | | | | -| `["DEPTH", "access, depth=7"]` | 4.971 ns (5%) | | | | -| `["DEPTH", "access, depth=8"]` | 5.700 ns (5%) | | | | -| `["DEPTH", "access, depth=9"]` | 6.452 ns (5%) | | | | -| `["DEPTH", "emtpyf, depth=1"]` | 80.947 ns (5%) | | 128 bytes (1%) | 5 | -| `["DEPTH", "emtpyf, depth=10"]` | 988.000 ns (5%) | | 1.25 KiB (1%) | 50 | -| `["DEPTH", "emtpyf, depth=11"]` | 1.065 μs (5%) | | 1.38 KiB (1%) | 55 | -| `["DEPTH", "emtpyf, depth=12"]` | 1.169 μs (5%) | | 1.50 KiB (1%) | 60 | -| `["DEPTH", "emtpyf, depth=13"]` | 1.318 μs (5%) | | 1.62 KiB (1%) | 65 | -| `["DEPTH", "emtpyf, depth=14"]` | 1.426 μs (5%) | | 1.75 KiB (1%) | 70 | -| `["DEPTH", "emtpyf, depth=15"]` | 1.496 μs (5%) | | 1.88 KiB (1%) | 75 | -| `["DEPTH", "emtpyf, depth=16"]` | 1.654 μs (5%) | | 2.00 KiB (1%) | 80 | -| `["DEPTH", "emtpyf, depth=17"]` | 1.703 μs (5%) | | 2.12 KiB (1%) | 85 | -| `["DEPTH", "emtpyf, depth=18"]` | 1.806 μs (5%) | | 2.25 KiB (1%) | 90 | -| `["DEPTH", "emtpyf, depth=19"]` | 1.934 μs (5%) | | 2.38 KiB (1%) | 95 | -| `["DEPTH", "emtpyf, depth=2"]` | 156.539 ns (5%) | | 256 bytes (1%) | 10 | -| `["DEPTH", "emtpyf, depth=20"]` | 2.118 μs (5%) | | 2.50 KiB (1%) | 100 | -| `["DEPTH", "emtpyf, depth=21"]` | 2.136 μs (5%) | | 2.62 KiB (1%) | 105 | -| `["DEPTH", "emtpyf, depth=22"]` | 2.208 μs (5%) | | 2.75 KiB (1%) | 110 | -| `["DEPTH", "emtpyf, depth=23"]` | 2.430 μs (5%) | | 2.88 KiB (1%) | 115 | -| `["DEPTH", "emtpyf, depth=24"]` | 2.466 μs (5%) | | 3.00 KiB (1%) | 120 | -| `["DEPTH", "emtpyf, depth=25"]` | 2.553 μs (5%) | | 3.12 KiB (1%) | 125 | -| `["DEPTH", "emtpyf, depth=26"]` | 2.800 μs (5%) | | 3.25 KiB (1%) | 130 | -| `["DEPTH", "emtpyf, depth=27"]` | 2.827 μs (5%) | | 3.38 KiB (1%) | 135 | -| `["DEPTH", "emtpyf, depth=28"]` | 2.991 μs (5%) | | 3.50 KiB (1%) | 140 | -| `["DEPTH", "emtpyf, depth=29"]` | 3.109 μs (5%) | | 3.62 KiB (1%) | 145 | -| `["DEPTH", "emtpyf, depth=3"]` | 224.435 ns (5%) | | 384 bytes (1%) | 15 | -| `["DEPTH", "emtpyf, depth=30"]` | 3.149 μs (5%) | | 3.75 KiB (1%) | 150 | -| `["DEPTH", "emtpyf, depth=31"]` | 3.416 μs (5%) | | 3.88 KiB (1%) | 155 | -| `["DEPTH", "emtpyf, depth=32"]` | 3.490 μs (5%) | | 4.00 KiB (1%) | 160 | -| `["DEPTH", "emtpyf, depth=4"]` | 329.633 ns (5%) | | 512 bytes (1%) | 20 | -| `["DEPTH", "emtpyf, depth=5"]` | 444.354 ns (5%) | | 640 bytes (1%) | 25 | -| `["DEPTH", "emtpyf, depth=6"]` | 554.111 ns (5%) | | 768 bytes (1%) | 30 | -| `["DEPTH", "emtpyf, depth=7"]` | 640.536 ns (5%) | | 896 bytes (1%) | 35 | -| `["DEPTH", "emtpyf, depth=8"]` | 755.546 ns (5%) | | 1.00 KiB (1%) | 40 | -| `["DEPTH", "emtpyf, depth=9"]` | 856.552 ns (5%) | | 1.12 KiB (1%) | 45 | -| `["DEPTH", "scope + access, depth=1"]` | 105.195 ns (5%) | | 128 bytes (1%) | 5 | -| `["DEPTH", "scope + access, depth=10"]` | 1.037 μs (5%) | | 1.25 KiB (1%) | 50 | -| `["DEPTH", "scope + access, depth=11"]` | 1.217 μs (5%) | | 1.38 KiB (1%) | 55 | -| `["DEPTH", "scope + access, depth=12"]` | 1.284 μs (5%) | | 1.50 KiB (1%) | 60 | -| `["DEPTH", "scope + access, depth=13"]` | 1.382 μs (5%) | | 1.62 KiB (1%) | 65 | -| `["DEPTH", "scope + access, depth=14"]` | 1.674 μs (5%) | | 1.75 KiB (1%) | 70 | -| `["DEPTH", "scope + access, depth=15"]` | 1.595 μs (5%) | | 1.88 KiB (1%) | 75 | -| `["DEPTH", "scope + access, depth=16"]` | 1.744 μs (5%) | | 2.00 KiB (1%) | 80 | -| `["DEPTH", "scope + access, depth=17"]` | 1.822 μs (5%) | | 2.12 KiB (1%) | 85 | -| `["DEPTH", "scope + access, depth=18"]` | 1.978 μs (5%) | | 2.25 KiB (1%) | 90 | -| `["DEPTH", "scope + access, depth=19"]` | 2.129 μs (5%) | | 2.38 KiB (1%) | 95 | -| `["DEPTH", "scope + access, depth=2"]` | 171.300 ns (5%) | | 256 bytes (1%) | 10 | -| `["DEPTH", "scope + access, depth=20"]` | 2.267 μs (5%) | | 2.50 KiB (1%) | 100 | -| `["DEPTH", "scope + access, depth=21"]` | 2.405 μs (5%) | | 2.62 KiB (1%) | 105 | -| `["DEPTH", "scope + access, depth=22"]` | 2.596 μs (5%) | | 2.75 KiB (1%) | 110 | -| `["DEPTH", "scope + access, depth=23"]` | 2.585 μs (5%) | | 2.88 KiB (1%) | 115 | -| `["DEPTH", "scope + access, depth=24"]` | 2.743 μs (5%) | | 3.00 KiB (1%) | 120 | -| `["DEPTH", "scope + access, depth=25"]` | 2.815 μs (5%) | | 3.12 KiB (1%) | 125 | -| `["DEPTH", "scope + access, depth=26"]` | 2.830 μs (5%) | | 3.25 KiB (1%) | 130 | -| `["DEPTH", "scope + access, depth=27"]` | 2.965 μs (5%) | | 3.38 KiB (1%) | 135 | -| `["DEPTH", "scope + access, depth=28"]` | 3.236 μs (5%) | | 3.50 KiB (1%) | 140 | -| `["DEPTH", "scope + access, depth=29"]` | 3.400 μs (5%) | | 3.62 KiB (1%) | 145 | -| `["DEPTH", "scope + access, depth=3"]` | 257.711 ns (5%) | | 384 bytes (1%) | 15 | -| `["DEPTH", "scope + access, depth=30"]` | 3.619 μs (5%) | | 3.75 KiB (1%) | 150 | -| `["DEPTH", "scope + access, depth=31"]` | 3.411 μs (5%) | | 3.88 KiB (1%) | 155 | -| `["DEPTH", "scope + access, depth=32"]` | 3.600 μs (5%) | | 4.00 KiB (1%) | 160 | -| `["DEPTH", "scope + access, depth=4"]` | 370.732 ns (5%) | | 512 bytes (1%) | 20 | -| `["DEPTH", "scope + access, depth=5"]` | 499.000 ns (5%) | | 640 bytes (1%) | 25 | -| `["DEPTH", "scope + access, depth=6"]` | 587.000 ns (5%) | | 768 bytes (1%) | 30 | -| `["DEPTH", "scope + access, depth=7"]` | 714.000 ns (5%) | | 896 bytes (1%) | 35 | -| `["DEPTH", "scope + access, depth=8"]` | 803.000 ns (5%) | | 1.00 KiB (1%) | 40 | -| `["DEPTH", "scope + access, depth=9"]` | 942.000 ns (5%) | | 1.12 KiB (1%) | 45 | +| `["BASIC", "scoped with assignment & ref"]` | 75.551 ns (5%) | | 112 bytes (1%) | 4 | +| `["BASIC", "scoped with assignment"]` | 74.602 ns (5%) | | 112 bytes (1%) | 4 | +| `["BASIC", "scoped"]` | 1.268 ns (5%) | | | | +| `["BASIC", "unscoped"]` | 4.399 ns (5%) | | | | +| `["DEPTH", "access, depth=1"]` | 4.282 ns (5%) | | | | +| `["DEPTH", "access, depth=10"]` | 22.877 ns (5%) | | | | +| `["DEPTH", "access, depth=11"]` | 25.429 ns (5%) | | | | +| `["DEPTH", "access, depth=12"]` | 27.387 ns (5%) | | | | +| `["DEPTH", "access, depth=13"]` | 29.693 ns (5%) | | | | +| `["DEPTH", "access, depth=14"]` | 31.946 ns (5%) | | | | +| `["DEPTH", "access, depth=15"]` | 34.265 ns (5%) | | | | +| `["DEPTH", "access, depth=16"]` | 38.866 ns (5%) | | | | +| `["DEPTH", "access, depth=17"]` | 41.714 ns (5%) | | | | +| `["DEPTH", "access, depth=18"]` | 44.204 ns (5%) | | | | +| `["DEPTH", "access, depth=19"]` | 47.115 ns (5%) | | | | +| `["DEPTH", "access, depth=2"]` | 7.531 ns (5%) | | | | +| `["DEPTH", "access, depth=20"]` | 49.395 ns (5%) | | | | +| `["DEPTH", "access, depth=21"]` | 52.426 ns (5%) | | | | +| `["DEPTH", "access, depth=22"]` | 54.459 ns (5%) | | | | +| `["DEPTH", "access, depth=23"]` | 56.313 ns (5%) | | | | +| `["DEPTH", "access, depth=24"]` | 62.031 ns (5%) | | | | +| `["DEPTH", "access, depth=25"]` | 60.474 ns (5%) | | | | +| `["DEPTH", "access, depth=26"]` | 62.889 ns (5%) | | | | +| `["DEPTH", "access, depth=27"]` | 66.122 ns (5%) | | | | +| `["DEPTH", "access, depth=28"]` | 68.368 ns (5%) | | | | +| `["DEPTH", "access, depth=29"]` | 70.513 ns (5%) | | | | +| `["DEPTH", "access, depth=3"]` | 9.791 ns (5%) | | | | +| `["DEPTH", "access, depth=30"]` | 78.812 ns (5%) | | | | +| `["DEPTH", "access, depth=31"]` | 75.381 ns (5%) | | | | +| `["DEPTH", "access, depth=32"]` | 89.478 ns (5%) | | | | +| `["DEPTH", "access, depth=4"]` | 11.801 ns (5%) | | | | +| `["DEPTH", "access, depth=5"]` | 12.727 ns (5%) | | | | +| `["DEPTH", "access, depth=6"]` | 15.802 ns (5%) | | | | +| `["DEPTH", "access, depth=7"]` | 17.058 ns (5%) | | | | +| `["DEPTH", "access, depth=8"]` | 19.301 ns (5%) | | | | +| `["DEPTH", "access, depth=9"]` | 20.939 ns (5%) | | | | +| `["DEPTH", "emtpyf, depth=1"]` | 81.754 ns (5%) | | 128 bytes (1%) | 5 | +| `["DEPTH", "emtpyf, depth=10"]` | 817.797 ns (5%) | | 1.25 KiB (1%) | 50 | +| `["DEPTH", "emtpyf, depth=11"]` | 882.281 ns (5%) | | 1.38 KiB (1%) | 55 | +| `["DEPTH", "emtpyf, depth=12"]` | 944.040 ns (5%) | | 1.50 KiB (1%) | 60 | +| `["DEPTH", "emtpyf, depth=13"]` | 1.080 μs (5%) | | 1.62 KiB (1%) | 65 | +| `["DEPTH", "emtpyf, depth=14"]` | 1.153 μs (5%) | | 1.75 KiB (1%) | 70 | +| `["DEPTH", "emtpyf, depth=15"]` | 1.189 μs (5%) | | 1.88 KiB (1%) | 75 | +| `["DEPTH", "emtpyf, depth=16"]` | 1.326 μs (5%) | | 2.00 KiB (1%) | 80 | +| `["DEPTH", "emtpyf, depth=17"]` | 1.363 μs (5%) | | 2.12 KiB (1%) | 85 | +| `["DEPTH", "emtpyf, depth=18"]` | 1.442 μs (5%) | | 2.25 KiB (1%) | 90 | +| `["DEPTH", "emtpyf, depth=19"]` | 1.520 μs (5%) | | 2.38 KiB (1%) | 95 | +| `["DEPTH", "emtpyf, depth=2"]` | 159.786 ns (5%) | | 256 bytes (1%) | 10 | +| `["DEPTH", "emtpyf, depth=20"]` | 1.637 μs (5%) | | 2.50 KiB (1%) | 100 | +| `["DEPTH", "emtpyf, depth=21"]` | 1.650 μs (5%) | | 2.62 KiB (1%) | 105 | +| `["DEPTH", "emtpyf, depth=22"]` | 1.716 μs (5%) | | 2.75 KiB (1%) | 110 | +| `["DEPTH", "emtpyf, depth=23"]` | 1.878 μs (5%) | | 2.88 KiB (1%) | 115 | +| `["DEPTH", "emtpyf, depth=24"]` | 1.903 μs (5%) | | 3.00 KiB (1%) | 120 | +| `["DEPTH", "emtpyf, depth=25"]` | 2.019 μs (5%) | | 3.12 KiB (1%) | 125 | +| `["DEPTH", "emtpyf, depth=26"]` | 2.120 μs (5%) | | 3.25 KiB (1%) | 130 | +| `["DEPTH", "emtpyf, depth=27"]` | 2.177 μs (5%) | | 3.38 KiB (1%) | 135 | +| `["DEPTH", "emtpyf, depth=28"]` | 2.313 μs (5%) | | 3.50 KiB (1%) | 140 | +| `["DEPTH", "emtpyf, depth=29"]` | 2.389 μs (5%) | | 3.62 KiB (1%) | 145 | +| `["DEPTH", "emtpyf, depth=3"]` | 245.156 ns (5%) | | 384 bytes (1%) | 15 | +| `["DEPTH", "emtpyf, depth=30"]` | 2.419 μs (5%) | | 3.75 KiB (1%) | 150 | +| `["DEPTH", "emtpyf, depth=31"]` | 2.573 μs (5%) | | 3.88 KiB (1%) | 155 | +| `["DEPTH", "emtpyf, depth=32"]` | 2.650 μs (5%) | | 4.00 KiB (1%) | 160 | +| `["DEPTH", "emtpyf, depth=4"]` | 340.785 ns (5%) | | 512 bytes (1%) | 20 | +| `["DEPTH", "emtpyf, depth=5"]` | 426.879 ns (5%) | | 640 bytes (1%) | 25 | +| `["DEPTH", "emtpyf, depth=6"]` | 508.067 ns (5%) | | 768 bytes (1%) | 30 | +| `["DEPTH", "emtpyf, depth=7"]` | 563.605 ns (5%) | | 896 bytes (1%) | 35 | +| `["DEPTH", "emtpyf, depth=8"]` | 649.516 ns (5%) | | 1.00 KiB (1%) | 40 | +| `["DEPTH", "emtpyf, depth=9"]` | 723.722 ns (5%) | | 1.12 KiB (1%) | 45 | +| `["DEPTH", "scope + access, depth=1"]` | 178.890 ns (5%) | | 128 bytes (1%) | 5 | +| `["DEPTH", "scope + access, depth=10"]` | 907.121 ns (5%) | | 1.25 KiB (1%) | 50 | +| `["DEPTH", "scope + access, depth=11"]` | 1.096 μs (5%) | | 1.38 KiB (1%) | 55 | +| `["DEPTH", "scope + access, depth=12"]` | 1.124 μs (5%) | | 1.50 KiB (1%) | 60 | +| `["DEPTH", "scope + access, depth=13"]` | 1.179 μs (5%) | | 1.62 KiB (1%) | 65 | +| `["DEPTH", "scope + access, depth=14"]` | 1.459 μs (5%) | | 1.75 KiB (1%) | 70 | +| `["DEPTH", "scope + access, depth=15"]` | 1.352 μs (5%) | | 1.88 KiB (1%) | 75 | +| `["DEPTH", "scope + access, depth=16"]` | 1.555 μs (5%) | | 2.00 KiB (1%) | 80 | +| `["DEPTH", "scope + access, depth=17"]` | 1.627 μs (5%) | | 2.12 KiB (1%) | 85 | +| `["DEPTH", "scope + access, depth=18"]` | 1.655 μs (5%) | | 2.25 KiB (1%) | 90 | +| `["DEPTH", "scope + access, depth=19"]` | 1.805 μs (5%) | | 2.38 KiB (1%) | 95 | +| `["DEPTH", "scope + access, depth=2"]` | 191.756 ns (5%) | | 256 bytes (1%) | 10 | +| `["DEPTH", "scope + access, depth=20"]` | 1.925 μs (5%) | | 2.50 KiB (1%) | 100 | +| `["DEPTH", "scope + access, depth=21"]` | 2.047 μs (5%) | | 2.62 KiB (1%) | 105 | +| `["DEPTH", "scope + access, depth=22"]` | 2.063 μs (5%) | | 2.75 KiB (1%) | 110 | +| `["DEPTH", "scope + access, depth=23"]` | 2.185 μs (5%) | | 2.88 KiB (1%) | 115 | +| `["DEPTH", "scope + access, depth=24"]` | 2.288 μs (5%) | | 3.00 KiB (1%) | 120 | +| `["DEPTH", "scope + access, depth=25"]` | 2.828 μs (5%) | | 3.12 KiB (1%) | 125 | +| `["DEPTH", "scope + access, depth=26"]` | 2.505 μs (5%) | | 3.25 KiB (1%) | 130 | +| `["DEPTH", "scope + access, depth=27"]` | 2.642 μs (5%) | | 3.38 KiB (1%) | 135 | +| `["DEPTH", "scope + access, depth=28"]` | 2.809 μs (5%) | | 3.50 KiB (1%) | 140 | +| `["DEPTH", "scope + access, depth=29"]` | 2.892 μs (5%) | | 3.62 KiB (1%) | 145 | +| `["DEPTH", "scope + access, depth=3"]` | 335.853 ns (5%) | | 384 bytes (1%) | 15 | +| `["DEPTH", "scope + access, depth=30"]` | 2.973 μs (5%) | | 3.75 KiB (1%) | 150 | +| `["DEPTH", "scope + access, depth=31"]` | 3.015 μs (5%) | | 3.88 KiB (1%) | 155 | +| `["DEPTH", "scope + access, depth=32"]` | 3.151 μs (5%) | | 4.00 KiB (1%) | 160 | +| `["DEPTH", "scope + access, depth=4"]` | 381.936 ns (5%) | | 512 bytes (1%) | 20 | +| `["DEPTH", "scope + access, depth=5"]` | 579.090 ns (5%) | | 640 bytes (1%) | 25 | +| `["DEPTH", "scope + access, depth=6"]` | 654.612 ns (5%) | | 768 bytes (1%) | 30 | +| `["DEPTH", "scope + access, depth=7"]` | 724.135 ns (5%) | | 896 bytes (1%) | 35 | +| `["DEPTH", "scope + access, depth=8"]` | 746.733 ns (5%) | | 1.00 KiB (1%) | 40 | +| `["DEPTH", "scope + access, depth=9"]` | 830.096 ns (5%) | | 1.12 KiB (1%) | 45 | ## Benchmark Group List Here's a list of all the benchmark groups executed by this job: @@ -131,15 +131,18 @@ Commit 6616549950e (2023-07-25 17:43 UTC) Platform Info: OS: Linux (x86_64-linux-gnu) "Arch Linux" - uname: Linux 6.3.2-arch1-1 #1 SMP PREEMPT_DYNAMIC Thu, 11 May 2023 16:40:42 +0000 x86_64 unknown - CPU: AMD Ryzen 7 3700X 8-Core Processor: - speed user nice sys idle irq - #1-16 4050 MHz 1096215 s 282 s 42091 s 21012008 s 9766 s - Memory: 125.69889831542969 GB (111567.15234375 MB free) - Uptime: 143486.41 sec - Load Avg: 1.75 1.55 1.61 + uname: Linux 6.4.3-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 11 Jul 2023 05:13:39 +0000 x86_64 unknown + CPU: Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz: + speed user nice sys idle irq + #1 3799 MHz 12616 s 39 s 2660 s 50817 s 1525 s + #2 3800 MHz 11737 s 50 s 2505 s 53029 s 289 s + #3 3799 MHz 13360 s 42 s 2698 s 51296 s 265 s + #4 3800 MHz 10704 s 32 s 2518 s 53844 s 465 s + Memory: 15.38076400756836 GB (5243.88671875 MB free) + Uptime: 27595.11 sec + Load Avg: 0.95 0.91 0.95 WORD_SIZE: 64 LIBM: libopenlibm - LLVM: libLLVM-15.0.7 (ORCJIT, znver2) - Threads: 1 on 16 virtual cores + LLVM: libLLVM-15.0.7 (ORCJIT, skylake) + Threads: 1 on 4 virtual cores ``` \ No newline at end of file diff --git a/src/ScopedValues.jl b/src/ScopedValues.jl index eb97c2a..93dfa2b 100644 --- a/src/ScopedValues.jl +++ b/src/ScopedValues.jl @@ -37,10 +37,27 @@ end Base.eltype(::Type{ScopedValue{T}}) where {T} = T + +struct DefaultScope end + +Base.getindex(::DefaultScope, var::ScopedValue) = var.initial_value + +""" + current_scope()::AbstractScope + +Return the current dynamic scope. +""" +function current_scope end + +function Base.getindex(var::ScopedValue{T})::T where T + scope = current_scope() + return scope[var] +end + # If we wanted to be really fancy we could implement Scope, # as Ctrie mutable struct Scope - const parent::Union{Nothing, Scope} + const parent::Union{Scope, DefaultScope} const key::ScopedValue const value::Any Scope(parent, key::ScopedValue{T}, value::T) where T = new(parent, key, value) @@ -48,17 +65,15 @@ end Scope(parent, key::ScopedValue{T}, value) where T = Scope(parent, key, convert(T, value)) -""" - current_scope()::Union{Nothing, Scope} +const AbstractScope = Union{DefaultScope, Scope} + -Return the current dynamic scope. -""" -function current_scope end function Base.show(io::IO, scope::Scope) print(io, Scope, "(") seen = Set{ScopedValue}() - while scope !== nothing + while scope !== DefaultScope() + # TODO make this general if scope.key ∉ seen if !isempty(seen) print(io, ", ") @@ -74,15 +89,11 @@ function Base.show(io::IO, scope::Scope) print(io, ")") end -function Base.getindex(var::ScopedValue{T})::T where T - scope = current_scope() - while scope !== nothing - if scope.key === var - return scope.value::T - end - scope = scope.parent +function Base.getindex(scope::Scope, var::ScopedValue{T}) where T + if scope.key == var + return scope.value::T end - return var.initial_value + return scope.parent[var]::T end function Base.show(io::IO, var::ScopedValue) diff --git a/src/payloadlogger.jl b/src/payloadlogger.jl index 4086fd7..4abed5a 100644 --- a/src/payloadlogger.jl +++ b/src/payloadlogger.jl @@ -2,7 +2,7 @@ using Logging: AbstractLogger, Logging struct ScopePayloadLogger <: AbstractLogger logger::AbstractLogger - scope::Scope + scope::AbstractScope end function current_scope() @@ -10,7 +10,7 @@ function current_scope() if logger isa ScopePayloadLogger return logger.scope end - return nothing + return DefaultScope() end function enter_scope(f, scope) @@ -38,7 +38,7 @@ function with_logger(f, logger::AbstractLogger) if cpl isa ScopePayloadLogger scope = cpl.scope else - scope = Scope(nothing) + scope = DefaultScope() end return Logging.with_logger(f, ScopePayloadLogger(logger, scope)) end diff --git a/test/runtests.jl b/test/runtests.jl index 49fa42b..914da15 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -62,7 +62,7 @@ end @testset "show" begin @test sprint(show, svar) == "ScopedValue{$Int}(1)" - @test sprint(show, ScopedValues.current_scope()) == "nothing" + @test sprint(show, ScopedValues.current_scope()) == "ScopedValues.DefaultScope()" scoped(svar => 2.0) do @test sprint(show, svar) == "ScopedValue{$Int}(2)" objid = sprint(show, Base.objectid(svar)) From dddb8863bc74d0bb38357bc10d01374ada8e6548 Mon Sep 17 00:00:00 2001 From: Valentin Churavy Date: Thu, 24 Aug 2023 19:28:25 -0400 Subject: [PATCH 2/2] add IdDict scope --- perf.md | 216 ++++++++++++++++++++++---------------------- src/ScopedValues.jl | 96 +++++++++++++------- test/runtests.jl | 17 +++- 3 files changed, 189 insertions(+), 140 deletions(-) diff --git a/perf.md b/perf.md index cbe203e..2f6fa85 100644 --- a/perf.md +++ b/perf.md @@ -1,7 +1,7 @@ # Benchmark Report for */home/vchuravy/src/ScopedVariables* ## Job Properties -* Time of benchmark: 24 Aug 2023 - 18:14 +* Time of benchmark: 24 Aug 2023 - 19:0 * Package commit: dirty * Julia commit: 661654 * Julia command flags: None @@ -17,106 +17,106 @@ An empty cell means that the value was zero. | ID | time | GC time | memory | allocations | |---------------------------------------------|----------------:|--------:|---------------:|------------:| -| `["BASIC", "scoped with assignment & ref"]` | 75.551 ns (5%) | | 112 bytes (1%) | 4 | -| `["BASIC", "scoped with assignment"]` | 74.602 ns (5%) | | 112 bytes (1%) | 4 | -| `["BASIC", "scoped"]` | 1.268 ns (5%) | | | | -| `["BASIC", "unscoped"]` | 4.399 ns (5%) | | | | -| `["DEPTH", "access, depth=1"]` | 4.282 ns (5%) | | | | -| `["DEPTH", "access, depth=10"]` | 22.877 ns (5%) | | | | -| `["DEPTH", "access, depth=11"]` | 25.429 ns (5%) | | | | -| `["DEPTH", "access, depth=12"]` | 27.387 ns (5%) | | | | -| `["DEPTH", "access, depth=13"]` | 29.693 ns (5%) | | | | -| `["DEPTH", "access, depth=14"]` | 31.946 ns (5%) | | | | -| `["DEPTH", "access, depth=15"]` | 34.265 ns (5%) | | | | -| `["DEPTH", "access, depth=16"]` | 38.866 ns (5%) | | | | -| `["DEPTH", "access, depth=17"]` | 41.714 ns (5%) | | | | -| `["DEPTH", "access, depth=18"]` | 44.204 ns (5%) | | | | -| `["DEPTH", "access, depth=19"]` | 47.115 ns (5%) | | | | -| `["DEPTH", "access, depth=2"]` | 7.531 ns (5%) | | | | -| `["DEPTH", "access, depth=20"]` | 49.395 ns (5%) | | | | -| `["DEPTH", "access, depth=21"]` | 52.426 ns (5%) | | | | -| `["DEPTH", "access, depth=22"]` | 54.459 ns (5%) | | | | -| `["DEPTH", "access, depth=23"]` | 56.313 ns (5%) | | | | -| `["DEPTH", "access, depth=24"]` | 62.031 ns (5%) | | | | -| `["DEPTH", "access, depth=25"]` | 60.474 ns (5%) | | | | -| `["DEPTH", "access, depth=26"]` | 62.889 ns (5%) | | | | -| `["DEPTH", "access, depth=27"]` | 66.122 ns (5%) | | | | -| `["DEPTH", "access, depth=28"]` | 68.368 ns (5%) | | | | -| `["DEPTH", "access, depth=29"]` | 70.513 ns (5%) | | | | -| `["DEPTH", "access, depth=3"]` | 9.791 ns (5%) | | | | -| `["DEPTH", "access, depth=30"]` | 78.812 ns (5%) | | | | -| `["DEPTH", "access, depth=31"]` | 75.381 ns (5%) | | | | -| `["DEPTH", "access, depth=32"]` | 89.478 ns (5%) | | | | -| `["DEPTH", "access, depth=4"]` | 11.801 ns (5%) | | | | -| `["DEPTH", "access, depth=5"]` | 12.727 ns (5%) | | | | -| `["DEPTH", "access, depth=6"]` | 15.802 ns (5%) | | | | -| `["DEPTH", "access, depth=7"]` | 17.058 ns (5%) | | | | -| `["DEPTH", "access, depth=8"]` | 19.301 ns (5%) | | | | -| `["DEPTH", "access, depth=9"]` | 20.939 ns (5%) | | | | -| `["DEPTH", "emtpyf, depth=1"]` | 81.754 ns (5%) | | 128 bytes (1%) | 5 | -| `["DEPTH", "emtpyf, depth=10"]` | 817.797 ns (5%) | | 1.25 KiB (1%) | 50 | -| `["DEPTH", "emtpyf, depth=11"]` | 882.281 ns (5%) | | 1.38 KiB (1%) | 55 | -| `["DEPTH", "emtpyf, depth=12"]` | 944.040 ns (5%) | | 1.50 KiB (1%) | 60 | -| `["DEPTH", "emtpyf, depth=13"]` | 1.080 μs (5%) | | 1.62 KiB (1%) | 65 | -| `["DEPTH", "emtpyf, depth=14"]` | 1.153 μs (5%) | | 1.75 KiB (1%) | 70 | -| `["DEPTH", "emtpyf, depth=15"]` | 1.189 μs (5%) | | 1.88 KiB (1%) | 75 | -| `["DEPTH", "emtpyf, depth=16"]` | 1.326 μs (5%) | | 2.00 KiB (1%) | 80 | -| `["DEPTH", "emtpyf, depth=17"]` | 1.363 μs (5%) | | 2.12 KiB (1%) | 85 | -| `["DEPTH", "emtpyf, depth=18"]` | 1.442 μs (5%) | | 2.25 KiB (1%) | 90 | -| `["DEPTH", "emtpyf, depth=19"]` | 1.520 μs (5%) | | 2.38 KiB (1%) | 95 | -| `["DEPTH", "emtpyf, depth=2"]` | 159.786 ns (5%) | | 256 bytes (1%) | 10 | -| `["DEPTH", "emtpyf, depth=20"]` | 1.637 μs (5%) | | 2.50 KiB (1%) | 100 | -| `["DEPTH", "emtpyf, depth=21"]` | 1.650 μs (5%) | | 2.62 KiB (1%) | 105 | -| `["DEPTH", "emtpyf, depth=22"]` | 1.716 μs (5%) | | 2.75 KiB (1%) | 110 | -| `["DEPTH", "emtpyf, depth=23"]` | 1.878 μs (5%) | | 2.88 KiB (1%) | 115 | -| `["DEPTH", "emtpyf, depth=24"]` | 1.903 μs (5%) | | 3.00 KiB (1%) | 120 | -| `["DEPTH", "emtpyf, depth=25"]` | 2.019 μs (5%) | | 3.12 KiB (1%) | 125 | -| `["DEPTH", "emtpyf, depth=26"]` | 2.120 μs (5%) | | 3.25 KiB (1%) | 130 | -| `["DEPTH", "emtpyf, depth=27"]` | 2.177 μs (5%) | | 3.38 KiB (1%) | 135 | -| `["DEPTH", "emtpyf, depth=28"]` | 2.313 μs (5%) | | 3.50 KiB (1%) | 140 | -| `["DEPTH", "emtpyf, depth=29"]` | 2.389 μs (5%) | | 3.62 KiB (1%) | 145 | -| `["DEPTH", "emtpyf, depth=3"]` | 245.156 ns (5%) | | 384 bytes (1%) | 15 | -| `["DEPTH", "emtpyf, depth=30"]` | 2.419 μs (5%) | | 3.75 KiB (1%) | 150 | -| `["DEPTH", "emtpyf, depth=31"]` | 2.573 μs (5%) | | 3.88 KiB (1%) | 155 | -| `["DEPTH", "emtpyf, depth=32"]` | 2.650 μs (5%) | | 4.00 KiB (1%) | 160 | -| `["DEPTH", "emtpyf, depth=4"]` | 340.785 ns (5%) | | 512 bytes (1%) | 20 | -| `["DEPTH", "emtpyf, depth=5"]` | 426.879 ns (5%) | | 640 bytes (1%) | 25 | -| `["DEPTH", "emtpyf, depth=6"]` | 508.067 ns (5%) | | 768 bytes (1%) | 30 | -| `["DEPTH", "emtpyf, depth=7"]` | 563.605 ns (5%) | | 896 bytes (1%) | 35 | -| `["DEPTH", "emtpyf, depth=8"]` | 649.516 ns (5%) | | 1.00 KiB (1%) | 40 | -| `["DEPTH", "emtpyf, depth=9"]` | 723.722 ns (5%) | | 1.12 KiB (1%) | 45 | -| `["DEPTH", "scope + access, depth=1"]` | 178.890 ns (5%) | | 128 bytes (1%) | 5 | -| `["DEPTH", "scope + access, depth=10"]` | 907.121 ns (5%) | | 1.25 KiB (1%) | 50 | -| `["DEPTH", "scope + access, depth=11"]` | 1.096 μs (5%) | | 1.38 KiB (1%) | 55 | -| `["DEPTH", "scope + access, depth=12"]` | 1.124 μs (5%) | | 1.50 KiB (1%) | 60 | -| `["DEPTH", "scope + access, depth=13"]` | 1.179 μs (5%) | | 1.62 KiB (1%) | 65 | -| `["DEPTH", "scope + access, depth=14"]` | 1.459 μs (5%) | | 1.75 KiB (1%) | 70 | -| `["DEPTH", "scope + access, depth=15"]` | 1.352 μs (5%) | | 1.88 KiB (1%) | 75 | -| `["DEPTH", "scope + access, depth=16"]` | 1.555 μs (5%) | | 2.00 KiB (1%) | 80 | -| `["DEPTH", "scope + access, depth=17"]` | 1.627 μs (5%) | | 2.12 KiB (1%) | 85 | -| `["DEPTH", "scope + access, depth=18"]` | 1.655 μs (5%) | | 2.25 KiB (1%) | 90 | -| `["DEPTH", "scope + access, depth=19"]` | 1.805 μs (5%) | | 2.38 KiB (1%) | 95 | -| `["DEPTH", "scope + access, depth=2"]` | 191.756 ns (5%) | | 256 bytes (1%) | 10 | -| `["DEPTH", "scope + access, depth=20"]` | 1.925 μs (5%) | | 2.50 KiB (1%) | 100 | -| `["DEPTH", "scope + access, depth=21"]` | 2.047 μs (5%) | | 2.62 KiB (1%) | 105 | -| `["DEPTH", "scope + access, depth=22"]` | 2.063 μs (5%) | | 2.75 KiB (1%) | 110 | -| `["DEPTH", "scope + access, depth=23"]` | 2.185 μs (5%) | | 2.88 KiB (1%) | 115 | -| `["DEPTH", "scope + access, depth=24"]` | 2.288 μs (5%) | | 3.00 KiB (1%) | 120 | -| `["DEPTH", "scope + access, depth=25"]` | 2.828 μs (5%) | | 3.12 KiB (1%) | 125 | -| `["DEPTH", "scope + access, depth=26"]` | 2.505 μs (5%) | | 3.25 KiB (1%) | 130 | -| `["DEPTH", "scope + access, depth=27"]` | 2.642 μs (5%) | | 3.38 KiB (1%) | 135 | -| `["DEPTH", "scope + access, depth=28"]` | 2.809 μs (5%) | | 3.50 KiB (1%) | 140 | -| `["DEPTH", "scope + access, depth=29"]` | 2.892 μs (5%) | | 3.62 KiB (1%) | 145 | -| `["DEPTH", "scope + access, depth=3"]` | 335.853 ns (5%) | | 384 bytes (1%) | 15 | -| `["DEPTH", "scope + access, depth=30"]` | 2.973 μs (5%) | | 3.75 KiB (1%) | 150 | -| `["DEPTH", "scope + access, depth=31"]` | 3.015 μs (5%) | | 3.88 KiB (1%) | 155 | -| `["DEPTH", "scope + access, depth=32"]` | 3.151 μs (5%) | | 4.00 KiB (1%) | 160 | -| `["DEPTH", "scope + access, depth=4"]` | 381.936 ns (5%) | | 512 bytes (1%) | 20 | -| `["DEPTH", "scope + access, depth=5"]` | 579.090 ns (5%) | | 640 bytes (1%) | 25 | -| `["DEPTH", "scope + access, depth=6"]` | 654.612 ns (5%) | | 768 bytes (1%) | 30 | -| `["DEPTH", "scope + access, depth=7"]` | 724.135 ns (5%) | | 896 bytes (1%) | 35 | -| `["DEPTH", "scope + access, depth=8"]` | 746.733 ns (5%) | | 1.00 KiB (1%) | 40 | -| `["DEPTH", "scope + access, depth=9"]` | 830.096 ns (5%) | | 1.12 KiB (1%) | 45 | +| `["BASIC", "scoped with assignment & ref"]` | 75.104 ns (5%) | | 112 bytes (1%) | 4 | +| `["BASIC", "scoped with assignment"]` | 70.622 ns (5%) | | 112 bytes (1%) | 4 | +| `["BASIC", "scoped"]` | 1.267 ns (5%) | | | | +| `["BASIC", "unscoped"]` | 4.281 ns (5%) | | | | +| `["DEPTH", "access, depth=1"]` | 5.285 ns (5%) | | | | +| `["DEPTH", "access, depth=10"]` | 11.250 ns (5%) | | | | +| `["DEPTH", "access, depth=11"]` | 12.424 ns (5%) | | | | +| `["DEPTH", "access, depth=12"]` | 13.291 ns (5%) | | | | +| `["DEPTH", "access, depth=13"]` | 14.199 ns (5%) | | | | +| `["DEPTH", "access, depth=14"]` | 15.914 ns (5%) | | | | +| `["DEPTH", "access, depth=15"]` | 17.592 ns (5%) | | | | +| `["DEPTH", "access, depth=16"]` | 18.650 ns (5%) | | | | +| `["DEPTH", "access, depth=17"]` | 20.556 ns (5%) | | | | +| `["DEPTH", "access, depth=18"]` | 21.733 ns (5%) | | | | +| `["DEPTH", "access, depth=19"]` | 23.699 ns (5%) | | | | +| `["DEPTH", "access, depth=2"]` | 5.437 ns (5%) | | | | +| `["DEPTH", "access, depth=20"]` | 24.617 ns (5%) | | | | +| `["DEPTH", "access, depth=21"]` | 25.872 ns (5%) | | | | +| `["DEPTH", "access, depth=22"]` | 28.169 ns (5%) | | | | +| `["DEPTH", "access, depth=23"]` | 29.698 ns (5%) | | | | +| `["DEPTH", "access, depth=24"]` | 30.707 ns (5%) | | | | +| `["DEPTH", "access, depth=25"]` | 32.066 ns (5%) | | | | +| `["DEPTH", "access, depth=26"]` | 33.927 ns (5%) | | | | +| `["DEPTH", "access, depth=27"]` | 35.619 ns (5%) | | | | +| `["DEPTH", "access, depth=28"]` | 36.474 ns (5%) | | | | +| `["DEPTH", "access, depth=29"]` | 37.920 ns (5%) | | | | +| `["DEPTH", "access, depth=3"]` | 6.113 ns (5%) | | | | +| `["DEPTH", "access, depth=30"]` | 39.423 ns (5%) | | | | +| `["DEPTH", "access, depth=31"]` | 45.101 ns (5%) | | | | +| `["DEPTH", "access, depth=32"]` | 42.416 ns (5%) | | | | +| `["DEPTH", "access, depth=4"]` | 6.318 ns (5%) | | | | +| `["DEPTH", "access, depth=5"]` | 7.068 ns (5%) | | | | +| `["DEPTH", "access, depth=6"]` | 7.176 ns (5%) | | | | +| `["DEPTH", "access, depth=7"]` | 8.277 ns (5%) | | | | +| `["DEPTH", "access, depth=8"]` | 9.328 ns (5%) | | | | +| `["DEPTH", "access, depth=9"]` | 10.143 ns (5%) | | | | +| `["DEPTH", "emtpyf, depth=1"]` | 81.881 ns (5%) | | 128 bytes (1%) | 5 | +| `["DEPTH", "emtpyf, depth=10"]` | 843.228 ns (5%) | | 1.25 KiB (1%) | 50 | +| `["DEPTH", "emtpyf, depth=11"]` | 909.930 ns (5%) | | 1.38 KiB (1%) | 55 | +| `["DEPTH", "emtpyf, depth=12"]` | 974.960 ns (5%) | | 1.50 KiB (1%) | 60 | +| `["DEPTH", "emtpyf, depth=13"]` | 1.084 μs (5%) | | 1.62 KiB (1%) | 65 | +| `["DEPTH", "emtpyf, depth=14"]` | 1.165 μs (5%) | | 1.75 KiB (1%) | 70 | +| `["DEPTH", "emtpyf, depth=15"]` | 1.229 μs (5%) | | 1.88 KiB (1%) | 75 | +| `["DEPTH", "emtpyf, depth=16"]` | 1.340 μs (5%) | | 2.00 KiB (1%) | 80 | +| `["DEPTH", "emtpyf, depth=17"]` | 1.375 μs (5%) | | 2.12 KiB (1%) | 85 | +| `["DEPTH", "emtpyf, depth=18"]` | 1.465 μs (5%) | | 2.25 KiB (1%) | 90 | +| `["DEPTH", "emtpyf, depth=19"]` | 1.617 μs (5%) | | 2.38 KiB (1%) | 95 | +| `["DEPTH", "emtpyf, depth=2"]` | 165.580 ns (5%) | | 256 bytes (1%) | 10 | +| `["DEPTH", "emtpyf, depth=20"]` | 1.666 μs (5%) | | 2.50 KiB (1%) | 100 | +| `["DEPTH", "emtpyf, depth=21"]` | 1.694 μs (5%) | | 2.62 KiB (1%) | 105 | +| `["DEPTH", "emtpyf, depth=22"]` | 1.757 μs (5%) | | 2.75 KiB (1%) | 110 | +| `["DEPTH", "emtpyf, depth=23"]` | 1.894 μs (5%) | | 2.88 KiB (1%) | 115 | +| `["DEPTH", "emtpyf, depth=24"]` | 2.041 μs (5%) | | 3.00 KiB (1%) | 120 | +| `["DEPTH", "emtpyf, depth=25"]` | 2.050 μs (5%) | | 3.12 KiB (1%) | 125 | +| `["DEPTH", "emtpyf, depth=26"]` | 2.142 μs (5%) | | 3.25 KiB (1%) | 130 | +| `["DEPTH", "emtpyf, depth=27"]` | 2.202 μs (5%) | | 3.38 KiB (1%) | 135 | +| `["DEPTH", "emtpyf, depth=28"]` | 2.303 μs (5%) | | 3.50 KiB (1%) | 140 | +| `["DEPTH", "emtpyf, depth=29"]` | 2.366 μs (5%) | | 3.62 KiB (1%) | 145 | +| `["DEPTH", "emtpyf, depth=3"]` | 246.950 ns (5%) | | 384 bytes (1%) | 15 | +| `["DEPTH", "emtpyf, depth=30"]` | 2.426 μs (5%) | | 3.75 KiB (1%) | 150 | +| `["DEPTH", "emtpyf, depth=31"]` | 2.579 μs (5%) | | 3.88 KiB (1%) | 155 | +| `["DEPTH", "emtpyf, depth=32"]` | 2.655 μs (5%) | | 4.00 KiB (1%) | 160 | +| `["DEPTH", "emtpyf, depth=4"]` | 357.575 ns (5%) | | 512 bytes (1%) | 20 | +| `["DEPTH", "emtpyf, depth=5"]` | 434.925 ns (5%) | | 640 bytes (1%) | 25 | +| `["DEPTH", "emtpyf, depth=6"]` | 508.259 ns (5%) | | 768 bytes (1%) | 30 | +| `["DEPTH", "emtpyf, depth=7"]` | 578.924 ns (5%) | | 896 bytes (1%) | 35 | +| `["DEPTH", "emtpyf, depth=8"]` | 665.710 ns (5%) | | 1.00 KiB (1%) | 40 | +| `["DEPTH", "emtpyf, depth=9"]` | 740.889 ns (5%) | | 1.12 KiB (1%) | 45 | +| `["DEPTH", "scope + access, depth=1"]` | 134.991 ns (5%) | | 128 bytes (1%) | 5 | +| `["DEPTH", "scope + access, depth=10"]` | 845.333 ns (5%) | | 1.25 KiB (1%) | 50 | +| `["DEPTH", "scope + access, depth=11"]` | 978.500 ns (5%) | | 1.38 KiB (1%) | 55 | +| `["DEPTH", "scope + access, depth=12"]` | 1.065 μs (5%) | | 1.50 KiB (1%) | 60 | +| `["DEPTH", "scope + access, depth=13"]` | 1.117 μs (5%) | | 1.62 KiB (1%) | 65 | +| `["DEPTH", "scope + access, depth=14"]` | 1.251 μs (5%) | | 1.75 KiB (1%) | 70 | +| `["DEPTH", "scope + access, depth=15"]` | 1.257 μs (5%) | | 1.88 KiB (1%) | 75 | +| `["DEPTH", "scope + access, depth=16"]` | 1.374 μs (5%) | | 2.00 KiB (1%) | 80 | +| `["DEPTH", "scope + access, depth=17"]` | 1.498 μs (5%) | | 2.12 KiB (1%) | 85 | +| `["DEPTH", "scope + access, depth=18"]` | 1.521 μs (5%) | | 2.25 KiB (1%) | 90 | +| `["DEPTH", "scope + access, depth=19"]` | 1.641 μs (5%) | | 2.38 KiB (1%) | 95 | +| `["DEPTH", "scope + access, depth=2"]` | 183.568 ns (5%) | | 256 bytes (1%) | 10 | +| `["DEPTH", "scope + access, depth=20"]` | 1.750 μs (5%) | | 2.50 KiB (1%) | 100 | +| `["DEPTH", "scope + access, depth=21"]` | 1.883 μs (5%) | | 2.62 KiB (1%) | 105 | +| `["DEPTH", "scope + access, depth=22"]` | 1.888 μs (5%) | | 2.75 KiB (1%) | 110 | +| `["DEPTH", "scope + access, depth=23"]` | 2.009 μs (5%) | | 2.88 KiB (1%) | 115 | +| `["DEPTH", "scope + access, depth=24"]` | 2.163 μs (5%) | | 3.00 KiB (1%) | 120 | +| `["DEPTH", "scope + access, depth=25"]` | 2.176 μs (5%) | | 3.12 KiB (1%) | 125 | +| `["DEPTH", "scope + access, depth=26"]` | 2.186 μs (5%) | | 3.25 KiB (1%) | 130 | +| `["DEPTH", "scope + access, depth=27"]` | 2.288 μs (5%) | | 3.38 KiB (1%) | 135 | +| `["DEPTH", "scope + access, depth=28"]` | 2.415 μs (5%) | | 3.50 KiB (1%) | 140 | +| `["DEPTH", "scope + access, depth=29"]` | 2.548 μs (5%) | | 3.62 KiB (1%) | 145 | +| `["DEPTH", "scope + access, depth=3"]` | 319.961 ns (5%) | | 384 bytes (1%) | 15 | +| `["DEPTH", "scope + access, depth=30"]` | 2.562 μs (5%) | | 3.75 KiB (1%) | 150 | +| `["DEPTH", "scope + access, depth=31"]` | 2.670 μs (5%) | | 3.88 KiB (1%) | 155 | +| `["DEPTH", "scope + access, depth=32"]` | 2.851 μs (5%) | | 4.00 KiB (1%) | 160 | +| `["DEPTH", "scope + access, depth=4"]` | 383.212 ns (5%) | | 512 bytes (1%) | 20 | +| `["DEPTH", "scope + access, depth=5"]` | 497.503 ns (5%) | | 640 bytes (1%) | 25 | +| `["DEPTH", "scope + access, depth=6"]` | 542.046 ns (5%) | | 768 bytes (1%) | 30 | +| `["DEPTH", "scope + access, depth=7"]` | 629.495 ns (5%) | | 896 bytes (1%) | 35 | +| `["DEPTH", "scope + access, depth=8"]` | 729.067 ns (5%) | | 1.00 KiB (1%) | 40 | +| `["DEPTH", "scope + access, depth=9"]` | 806.753 ns (5%) | | 1.12 KiB (1%) | 45 | ## Benchmark Group List Here's a list of all the benchmark groups executed by this job: @@ -134,13 +134,13 @@ Platform Info: uname: Linux 6.4.3-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 11 Jul 2023 05:13:39 +0000 x86_64 unknown CPU: Intel(R) Core(TM) i7-7660U CPU @ 2.50GHz: speed user nice sys idle irq - #1 3799 MHz 12616 s 39 s 2660 s 50817 s 1525 s - #2 3800 MHz 11737 s 50 s 2505 s 53029 s 289 s - #3 3799 MHz 13360 s 42 s 2698 s 51296 s 265 s - #4 3800 MHz 10704 s 32 s 2518 s 53844 s 465 s - Memory: 15.38076400756836 GB (5243.88671875 MB free) - Uptime: 27595.11 sec - Load Avg: 0.95 0.91 0.95 + #1 3649 MHz 16374 s 46 s 3408 s 73235 s 2156 s + #2 3756 MHz 15733 s 60 s 3276 s 75717 s 385 s + #3 3701 MHz 16962 s 44 s 3480 s 74365 s 368 s + #4 3674 MHz 13882 s 34 s 3246 s 77359 s 593 s + Memory: 15.38076400756836 GB (4820.5859375 MB free) + Uptime: 30374.39 sec + Load Avg: 1.23 1.09 0.99 WORD_SIZE: 64 LIBM: libopenlibm LLVM: libLLVM-15.0.7 (ORCJIT, skylake) diff --git a/src/ScopedValues.jl b/src/ScopedValues.jl index 93dfa2b..a2ba276 100644 --- a/src/ScopedValues.jl +++ b/src/ScopedValues.jl @@ -37,10 +37,14 @@ end Base.eltype(::Type{ScopedValue{T}}) where {T} = T +abstract type AbstractScope end -struct DefaultScope end - -Base.getindex(::DefaultScope, var::ScopedValue) = var.initial_value +struct DefaultScope <: AbstractScope end +const default = DefaultScope() +parent(::DefaultScope) = default # To avoid Union{Nothing, AbstractScope} +depth(::DefaultScope) = 0 # To avoid Union{Nothing, AbstractScope} +haskey(::DefaultScope, val::ScopedValue) = true +Base.getindex(::DefaultScope, val::ScopedValue) = val.initial_value """ current_scope()::AbstractScope @@ -49,53 +53,80 @@ Return the current dynamic scope. """ function current_scope end -function Base.getindex(var::ScopedValue{T})::T where T - scope = current_scope() - return scope[var] -end - # If we wanted to be really fancy we could implement Scope, # as Ctrie -mutable struct Scope - const parent::Union{Scope, DefaultScope} +mutable struct Scope <: AbstractScope + const parent::AbstractScope + const depth::Int const key::ScopedValue const value::Any - Scope(parent, key::ScopedValue{T}, value::T) where T = new(parent, key, value) + Scope(parent, key::ScopedValue{T}, value::T) where T = new(parent, depth(parent)+1, key, value) end Scope(parent, key::ScopedValue{T}, value) where T = Scope(parent, key, convert(T, value)) -const AbstractScope = Union{DefaultScope, Scope} +parent(scope::Scope) = scope.parent +depth(scope::Scope) = scope.depth +haskey(scope::Scope, val::ScopedValue) = scope.key == val +function Base.getindex(scope::Scope, ::ScopedValue{T}) where T + scope.value::T +end +mutable struct DictScope <: AbstractScope + const parent::AbstractScope + const depth::Int + const values::IdDict{ScopedValue, Any} + DictScope(parent, values) = new(parent, depth(parent)+1, values) +end +parent(scope::DictScope) = scope.parent +depth(scope::DictScope) = scope.depth +haskey(scope::DictScope, val::ScopedValue) = Base.haskey(scope.values, val) +function Base.getindex(scope::DictScope, val::ScopedValue{T}) where T + scope.values[val]::T +end -function Base.show(io::IO, scope::Scope) - print(io, Scope, "(") - seen = Set{ScopedValue}() - while scope !== DefaultScope() - # TODO make this general - if scope.key ∉ seen - if !isempty(seen) - print(io, ", ") +function collapse(scope) + values = IdDict{ScopedValue, Any}() + while scope !== default + if scope isa Scope + if !Base.haskey(values, scope.key) + values[scope.key] = scope.value + end + elseif scope isa DictScope + for (key, value) in scope.values + if !Base.haskey(values, key) + values[key] = value + end end - print(io, typeof(scope.key), "@") - show(io, Base.objectid(scope.key)) - print(io, " => ") - show(IOContext(io, :typeinfo => eltype(scope.key)), scope.value) - push!(seen, scope.key) end - scope = scope.parent + scope = parent(scope) end - print(io, ")") + return DictScope(DefaultScope(), values) end -function Base.getindex(scope::Scope, var::ScopedValue{T}) where T - if scope.key == var - return scope.value::T + +function Base.show(io::IO, scope::AbstractScope) + print(io, typeof(scope), "()") +end + +# Is this the correct way? I want to force union-split on haskey/getindex +# for large +Base.Experimental.@max_methods 255 function lookup end + +@inline function lookup(val::ScopedValue{T})::T where T + scope = current_scope() + while true + if haskey(scope, val) + return scope[val] + end + scope = parent(scope) end - return scope.parent[var]::T + @assert false end +@inline Base.getindex(val::ScopedValue) = lookup(val) + function Base.show(io::IO, var::ScopedValue) print(io, ScopedValue) print(io, '{', eltype(var), '}') @@ -115,6 +146,9 @@ function scoped(f, pair::Pair{<:ScopedValue}, rest::Pair{<:ScopedValue}...) for pair in rest scope = Scope(scope, pair...) end + if depth(scope) >= 64 # measure cutof + scope = collapse(scope) + end enter_scope(scope) do f() end diff --git a/test/runtests.jl b/test/runtests.jl index 914da15..4a06b83 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -66,7 +66,7 @@ end scoped(svar => 2.0) do @test sprint(show, svar) == "ScopedValue{$Int}(2)" objid = sprint(show, Base.objectid(svar)) - @test sprint(show, ScopedValues.current_scope()) == "ScopedValues.Scope(ScopedValue{$Int}@$objid => 2)" + @test sprint(show, ScopedValues.current_scope()) == "ScopedValues.Scope()" end end @@ -107,3 +107,18 @@ end end end end + +scoped(svar_float=>2.0) do + nth_scoped(30) do + scope = ScopedValues.collapse(ScopedValues.current_scope()) + ScopedValues.enter_scope(scope) do + @test svar_float[] == 2.0 + end + end +end + +scoped(svar_float=>2.0) do + nth_scoped(96) do + @test svar_float[] == 2.0 + end +end