Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify the query implementation #121

Merged
merged 6 commits into from
Jun 5, 2024
Merged

simplify the query implementation #121

merged 6 commits into from
Jun 5, 2024

Conversation

Krastanov
Copy link
Member

This code is drastically simpler than the original implementation, but also much slower:

Kept here as a reference and for potential future work that might be both simpler and faster.

## before any simplification

BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  134.635 μs …   6.296 ms  ┊ GC (min … max): 0.00% … 95.01%
 Time  (median):     142.902 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   160.930 μs ± 240.887 μs  ┊ GC (mean ± σ):  8.04% ±  5.30%

    ▂▇██▅▂
  ▃▅███████▇▆▅▅▅▅▅▅▅▄▄▄▃▃▃▃▃▃▃▃▃▃▃▂▂▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂ ▃
  135 μs           Histogram: frequency by time          205 μs <

 Memory estimate: 214.33 KiB, allocs estimate: 2649.

## after first round of simplification

BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  171.124 μs …  10.533 ms  ┊ GC (min … max): 0.00% … 96.97%
 Time  (median):     182.391 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   200.198 μs ± 291.337 μs  ┊ GC (mean ± σ):  6.32% ±  4.33%

    ▁▆███▇▃▂
  ▁▃██████████▇▇▆▅▄▄▄▃▃▃▂▂▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▃
  171 μs           Histogram: frequency by time          256 μs <

 Memory estimate: 226.64 KiB, allocs estimate: 2848.

## after second round of simplification

BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min … max):  331.740 μs …   6.718 ms  ┊ GC (min … max): 0.00% … 91.94%
 Time  (median):     344.995 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   368.845 μs ± 261.286 μs  ┊ GC (mean ± σ):  5.52% ±  7.07%

     ▅██▆▃▂
  ▂▃▇███████▆▆▅▄▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▁▁▁▁▁▂▂▁▁▁▁▁▁▂▂▂▁▂ ▃
  332 μs           Histogram: frequency by time          458 μs <

 Memory estimate: 436.03 KiB, allocs estimate: 8666.

@Krastanov Krastanov added Skip-Changelog label for control of CI: skips the changelog check completed experiment testing an approach to do something (but different approach taken at the end) labels Jun 4, 2024
@Krastanov
Copy link
Member Author

some more microbenchmarks showing more clearly the order-of-magnitude difference:

using QuantumSavory
using QuantumSavory.ProtocolZoo
using BenchmarkTools

reg = Register(5)
tag!(reg[3], EntanglementCounterpart, 1, 10)
tag!(reg[3], EntanglementCounterpart, 2, 21)
tag!(reg[3], EntanglementCounterpart, 3, 30)
tag!(reg[3], EntanglementCounterpart, 2, 22)
tag!(reg[3], EntanglementCounterpart, 1, 10)
tag!(reg[3], EntanglementCounterpart, 6, 60)
tag!(reg[3], EntanglementCounterpart, 2, 23)
tag!(reg[3], EntanglementCounterpart, 1, 10)

@benchmark query(reg, EntanglementCounterpart, 6, ❓; filo=true)

##
#=

# before

BenchmarkTools.Trial: 10000 samples with 217 evaluations.
 Range (min … max):  349.700 ns …  20.329 μs  ┊ GC (min … max): 0.00% … 97.32%
 Time  (median):     363.272 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   393.885 ns ± 410.384 ns  ┊ GC (mean ± σ):  3.76% ±  3.76%

   ▁▃▇█▇▅▃▁▁▃▃▂▁                                       ▁▂▂▂▂▂▁  ▂
  ████████████████▇▇▆▇▆▅▆▆▆▄▄▅▃▄▃▄▅▇█▆▇█▇▇▄▃▃▁▃▁▃▄▄▃▆▇█████████ █
  350 ns        Histogram: log(frequency) by time        519 ns <

 Memory estimate: 512 bytes, allocs estimate: 8.

# after

BenchmarkTools.Trial: 10000 samples with 7 evaluations.
 Range (min … max):  4.225 μs … 313.888 μs  ┊ GC (min … max): 0.00% … 96.58%
 Time  (median):     4.343 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   4.503 μs ±   5.284 μs  ┊ GC (mean ± σ):  1.98% ±  1.67%

   ▄▇█▇▅▃▂                                                    ▂
  ▆████████▇▆▇▇▇▆▆▅▅▆▄▇█▇▇▅▅▄▅▄▄▅▆▅▅▅▅▅▅▄▅▅▅▄▂▄▅▄▅▅▆▅▃▄▄▄▃▄▄▄ █
  4.23 μs      Histogram: log(frequency) by time       6.1 μs <

 Memory estimate: 3.53 KiB, allocs estimate: 79.

Krastanov added 3 commits June 4, 2024 15:36
```
reg = Register(5)
tag!(reg[3], EntanglementCounterpart, 1, 10)
tag!(reg[3], EntanglementCounterpart, 2, 21)
tag!(reg[3], EntanglementCounterpart, 3, 30)
tag!(reg[3], EntanglementCounterpart, 2, 22)
tag!(reg[3], EntanglementCounterpart, 1, 10)
tag!(reg[3], EntanglementCounterpart, 6, 60)
tag!(reg[3], EntanglementCounterpart, 2, 23)
tag!(reg[3], EntanglementCounterpart, 1, 10)

@benchmark QuantumSavory._query(reg, Val{false}(), Val{true}(), EntanglementCounterpart, 6, ❓)

BenchmarkTools.Trial: 10000 samples with 9 evaluations.
 Range (min … max):  2.281 μs … 511.600 μs  ┊ GC (min … max): 0.00% … 98.50%
 Time  (median):     2.343 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   2.505 μs ±   7.238 μs  ┊ GC (mean ± σ):  4.67% ±  1.70%

  ▂▆██▆▅▄▃▂▂▁▁                                                ▂
  ██████████████▇▆▆▆▅▅▄▆▆▅▆▇▆▅▆▆▆▄▅▁▄▃▅▆▆▆▆▆▅▄▃▅▅▆▅▄▄▆▄▄▅▄▅▄▄ █
  2.28 μs      Histogram: log(frequency) by time      3.46 μs <

 Memory estimate: 2.64 KiB, allocs estimate: 67.

after new: vararg union
BenchmarkTools.Trial: 10000 samples with 10 evaluations.
 Range (min … max):  1.239 μs … 181.067 μs  ┊ GC (min … max): 0.00% … 98.00%
 Time  (median):     1.315 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   1.491 μs ±   3.010 μs  ┊ GC (mean ± σ):  3.41% ±  1.69%

   ▄▇█▇▆▅▃▂▂▁▁                 ▁     ▂▃▂   ▁▂▂▁  ▁▁           ▂
  ▇███████████▇▅▆▄▃▄▃▄▄▃▅▂▄▄▆▆███▇▇▆█████████████████▇▇███▇▆▆ █
  1.24 μs      Histogram: log(frequency) by time      2.26 μs <

 Memory estimate: 2.34 KiB, allocs estimate: 63.

after new vararg union n
BenchmarkTools.Trial: 10000 samples with 129 evaluations.
 Range (min … max):  734.426 ns …  16.797 μs  ┊ GC (min … max): 0.00% … 92.30%
 Time  (median):     772.566 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   844.180 ns ± 737.678 ns  ┊ GC (mean ± σ):  4.46% ±  4.87%

   ▁▅██▆▃▃▂▂▂▂▁▁▁                                    ▁▁         ▂
  ▄████████████████▇▇▇▇▇▆▆▆▆▆▅▅▄▅▄▅▄▄▄▂▂▄▃▄▂▂▄▄▄▄▄▇██████▇▇▅▅▆▄ █
  734 ns        Histogram: log(frequency) by time       1.26 μs <

 Memory estimate: 1.44 KiB, allocs estimate: 38.

after cleaning up the `good` loop
BenchmarkTools.Trial: 10000 samples with 238 evaluations.
 Range (min … max):  315.601 ns …  13.549 μs  ┊ GC (min … max): 0.00% … 93.98%
 Time  (median):     344.792 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   410.885 ns ± 537.236 ns  ┊ GC (mean ± σ):  6.80% ±  5.07%

   ▄▆▇██▆▆▅▄▃▂▂▁▁▁                            ▂▃▃▃▄▃▂▁▁         ▂
  ███████████████████▇▇█▇▇▆▇▆▅▅▅▃▅▄▅▅▄▃▂▃▃▄▆▇███████████▇▇▇▇▆▅▆ █
  316 ns        Histogram: log(frequency) by time        640 ns <

 Memory estimate: 864 bytes, allocs estimate: 19.

after cleaning up the `good` loop and ending it early and adding inbounds
BenchmarkTools.Trial: 10000 samples with 250 evaluations.
 Range (min … max):  319.484 ns …  12.822 μs  ┊ GC (min … max): 0.00% … 95.55%
 Time  (median):     339.724 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   407.820 ns ± 559.269 ns  ┊ GC (mean ± σ):  7.25% ±  5.18%

  ▃▆██▇▅▅▄▃▃▂▁                               ▁▁▃▃▃▃▃▂▂▁▁▁       ▂
  █████████████▇▇▇▆▇▆▆▆▇▆▆▇▅▅▆▆▃▅▅▄▄▁▃▁▃▃▁▄▃▆██████████████▇▇▆▆ █
  319 ns        Histogram: log(frequency) by time        642 ns <

 Memory estimate: 864 bytes, allocs estimate: 19.

after adding @propagate_inbounds
BenchmarkTools.Trial: 10000 samples with 318 evaluations.
 Range (min … max):  271.113 ns …   8.674 μs  ┊ GC (min … max): 0.00% … 92.87%
 Time  (median):     287.305 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   330.077 ns ± 403.618 ns  ┊ GC (mean ± σ):  7.28% ±  5.74%

  ▁▅██▇▅▄▃▂▂▁▁                                          ▂▂▂▁▁   ▂
  ████████████████████▆█▇▇▇▇▄▆▆▅▅▅▄▄▄▅▅▄▅▅▃▄▅▁▄▅▄▄▄▃▁▄▆███████▆ █
  271 ns        Histogram: log(frequency) by time        544 ns <

 Memory estimate: 864 bytes, allocs estimate: 19.

after removing the `reverse`
BenchmarkTools.Trial: 10000 samples with 359 evaluations.
 Range (min … max):  254.019 ns …   6.402 μs  ┊ GC (min … max): 0.00% … 93.36%
 Time  (median):     261.972 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   300.217 ns ± 314.340 ns  ┊ GC (mean ± σ):  5.90% ±  5.42%

  ▃██▅▄▄▃▃▂▁                                        ▁▃▄▃▁▁      ▂
  ████████████▇▆▇▆▆▄▆▄▄▁▄▄▃▁▄██▅▅▆▄▁▃▃▃▄▃▅▅▃▁▄▁▃▄▁▅▇████████▇▆▆ █
  254 ns        Histogram: log(frequency) by time        468 ns <

 Memory estimate: 672 bytes, allocs estimate: 18.
```
after removing the `reverse`
BenchmarkTools.Trial: 10000 samples with 359 evaluations.
 Range (min … max):  254.019 ns …   6.402 μs  ┊ GC (min … max): 0.00% … 93.36%
 Time  (median):     261.972 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   300.217 ns ± 314.340 ns  ┊ GC (mean ± σ):  5.90% ±  5.42%

  ▃██▅▄▄▃▃▂▁                                        ▁▃▄▃▁▁      ▂
  ████████████▇▆▇▆▆▄▆▄▄▁▄▄▃▁▄██▅▅▆▄▁▃▃▃▄▃▅▅▃▁▄▁▃▄▁▅▇████████▇▆▆ █
  254 ns        Histogram: log(frequency) by time        468 ns <

 Memory estimate: 672 bytes, allocs estimate: 18.

factoring out a single `query_good(N::Int, tag::Tag, queryargs...)`
BenchmarkTools.Trial: 10000 samples with 390 evaluations.
 Range (min … max):  248.241 ns …   6.743 μs  ┊ GC (min … max): 0.00% … 93.44%
 Time  (median):     257.900 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   278.917 ns ± 294.070 ns  ┊ GC (mean ± σ):  4.95% ±  4.51%

  ▁▄▆██▅▃▃▃▂▁                                             ▁▁▁▁▁ ▂
  █████████████▆▇▇▇▆▆▇▆▆▄▇▅▅▅▃▄▄▄▃▁▃▄▁▃▃▄▅▄▄▁▄▁▁▃▄▁▄▃▃▄▄▇██████ █
  248 ns        Histogram: log(frequency) by time        395 ns <

 Memory estimate: 448 bytes, allocs estimate: 11.

factoring out many macro-generated `query_good(n::Int, tag::Tag, a, ...)`
BenchmarkTools.Trial: 10000 samples with 839 evaluations.
 Range (min … max):  149.449 ns …   5.746 μs  ┊ GC (min … max): 0.00% … 96.28%
 Time  (median):     155.396 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   187.805 ns ± 199.312 ns  ┊ GC (mean ± σ):  7.36% ±  6.75%

  ▃▇█▆▅▄▃▁▁                                          ▂▄▄▃▃▂▁▁   ▂
  █████████████▇▅▆▆▃▅▄▄▄▃▄▃▃▃▃▄▄▃▃▄▁▄▄▃▁▄▃▄▁▃▄▁▄▁▄▇████████████ █
  149 ns        Histogram: log(frequency) by time        288 ns <

 Memory estimate: 448 bytes, allocs estimate: 11.
…d avoiding Vararg)

same benchmark as previous commit

factoring out many macro-generated `query_good(n::Int, tag::Tag, a, ...)`
BenchmarkTools.Trial: 10000 samples with 839 evaluations.
 Range (min … max):  149.449 ns …   5.746 μs  ┊ GC (min … max): 0.00% … 96.28%
 Time  (median):     155.396 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   187.805 ns ± 199.312 ns  ┊ GC (mean ± σ):  7.36% ±  6.75%

  ▃▇█▆▅▄▃▁▁                                          ▂▄▄▃▃▂▁▁   ▂
  █████████████▇▅▆▆▃▅▄▄▄▃▄▃▃▃▃▄▄▃▃▄▁▄▄▃▁▄▃▄▁▃▄▁▄▁▄▇████████████ █
  149 ns        Histogram: log(frequency) by time        288 ns <

 Memory estimate: 448 bytes, allocs estimate: 11.

using @Cases in the macro-generated `query_good`
BenchmarkTools.Trial: 10000 samples with 962 evaluations.
 Range (min … max):  83.297 ns …  4.867 μs  ┊ GC (min … max): 0.00% … 97.14%
 Time  (median):     87.609 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   93.590 ns ± 67.695 ns  ┊ GC (mean ± σ):  2.36% ±  4.16%

   ▃█▆▆▇▄▃▂▂ ▁               ▁▁▁▁▁▁                           ▂
  ███████████████▇▇▇▇▇▆▆▆▅▅▆▆███████▇▇▇▆▆▆▇▇▆▄▆▅▃▄▄▃▄▅▁▄▄▃▄▁▄ █
  83.3 ns      Histogram: log(frequency) by time       156 ns <

 Memory estimate: 128 bytes, allocs estimate: 1.
@Krastanov Krastanov removed completed experiment testing an approach to do something (but different approach taken at the end) Skip-Changelog label for control of CI: skips the changelog check labels Jun 5, 2024
Copy link

codecov bot commented Jun 5, 2024

Codecov Report

Attention: Patch coverage is 88.70968% with 7 lines in your changes missing coverage. Please review.

Project coverage is 73.45%. Comparing base (10e94f0) to head (ddb00ea).

Files Patch % Lines
src/queries.jl 87.93% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #121      +/-   ##
==========================================
- Coverage   73.62%   73.45%   -0.18%     
==========================================
  Files          38       38              
  Lines        1695     1695              
==========================================
- Hits         1248     1245       -3     
- Misses        447      450       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Krastanov Krastanov force-pushed the simplifyquery branch 2 times, most recently from e4bb649 to 634daa3 Compare June 5, 2024 04:40
…than the original, chiefly by avoiding varargs (thanks to macro-expanding separate methods for each number of arguments)
@Krastanov Krastanov merged commit 825bb39 into master Jun 5, 2024
9 of 13 checks passed
@Krastanov Krastanov deleted the simplifyquery branch June 5, 2024 13:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant