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

Decouple size dictionary from EinExprs #45

Merged
merged 28 commits into from
Dec 28, 2023
Merged

Conversation

mofeing
Copy link
Member

@mofeing mofeing commented Dec 24, 2023

When manipulating EinExprs (e.g. creating, summing, ...), creation and merge of size dictionaries incurs into a large amount of allocations.

This PR decouples size dictionary from EinExpr and introduces SizedEinExpr: a EinExpr wrapper with a global index size dictionary.

julia> using EinExprs, BenchmarkTools

expr = sum([
    EinExpr([:j, :b, :i, :h], Dict(i => 2 for i in [:j, :b, :i, :h])),
    EinExpr([:a, :c, :e, :f], Dict(i => 2 for i in [:a, :c, :e, :f])),
    EinExpr([:j], Dict(i => 2 for i in [:j])),
    EinExpr([:e, :a, :g], Dict(i => 2 for i in [:e, :a, :g])),
    EinExpr([:f, :b], Dict(i => 2 for i in [:f, :b])),
    EinExpr([:i, :h, :d], Dict(i => 2 for i in [:i, :h, :d])),
    EinExpr([:d, :g, :c], Dict(i => 2 for i in [:d, :g, :c])),
])

@benchmark path = einexpr(Exhaustive(), expr)

Before

BenchmarkTools.Trial: 2442 samples with 1 evaluation.
 Range (min  max):  1.618 ms  7.713 ms  ┊ GC (min  max):  0.00%  42.34%
 Time  (median):     1.674 ms             ┊ GC (median):     0.00%
 Time  (mean ± σ):   2.046 ms ± 1.072 ms  ┊ GC (mean ± σ):  13.91% ± 16.30%

  █▆▂                                               ▁▁▁▁▁    
  ████▇▆▄▃▄▁▁▁▁▁▃▃▁▃▁▁▁▁▁▁▃▄▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▇██████▇ █
  1.62 ms     Histogram: log(frequency) by time     5.37 ms <

 Memory estimate: 4.51 MiB, allocs estimate: 65772.

After

BenchmarkTools.Trial: 9124 samples with 1 evaluation.
 Range (min  max):  400.167 μs   14.535 ms  ┊ GC (min  max):  0.00%  66.90%
 Time  (median):     425.625 μs               ┊ GC (median):     0.00%
 Time  (mean ± σ):   546.911 μs ± 966.583 μs  ┊ GC (mean ± σ):  11.38% ±  6.06%

  █▂                                                            ▁
  ██▆▅▄▄▃▁▃▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▃▇▆ █
  400 μs        Histogram: log(frequency) by time       8.27 ms <

 Memory estimate: 621.73 KiB, allocs estimate: 11344.

@mofeing mofeing linked an issue Dec 24, 2023 that may be closed by this pull request
Copy link

codecov bot commented Dec 28, 2023

Codecov Report

Attention: 20 lines in your changes are missing coverage. Please review.

Comparison is base (18f5783) 78.38% compared to head (47c64e5) 80.23%.
Report is 4 commits behind head on master.

Files Patch % Lines
src/SizedEinExpr.jl 77.50% 9 Missing ⚠️
src/Slicing.jl 56.25% 7 Missing ⚠️
src/Counters.jl 80.00% 2 Missing ⚠️
src/EinExpr.jl 95.23% 1 Missing ⚠️
src/Optimizers/Optimizers.jl 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #45      +/-   ##
==========================================
+ Coverage   78.38%   80.23%   +1.85%     
==========================================
  Files          11       12       +1     
  Lines         273      334      +61     
==========================================
+ Hits          214      268      +54     
- Misses         59       66       +7     

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

@mofeing
Copy link
Member Author

mofeing commented Dec 28, 2023

Greedy also exhibits a similar speedup and memory usage reduction.

Before

BenchmarkTools.Trial: 10000 samples with 7 evaluations.
 Range (min  max):  4.696 μs  309.470 μs  ┊ GC (min  max): 0.00%  96.32%
 Time  (median):     4.935 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   5.269 μs ±   9.726 μs  ┊ GC (mean ± σ):  6.01% ±  3.20%

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

 Memory estimate: 7.27 KiB, allocs estimate: 160.

After

BenchmarkTools.Trial: 10000 samples with 10 evaluations.
 Range (min  max):  1.913 μs  252.412 μs  ┊ GC (min  max): 0.00%  97.77%
 Time  (median):     1.988 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   2.117 μs ±   5.519 μs  ┊ GC (mean ± σ):  5.74% ±  2.18%

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

 Memory estimate: 2.39 KiB, allocs estimate: 43.

@mofeing mofeing merged commit 316069d into master Dec 28, 2023
6 checks passed
@mofeing mofeing deleted the refactor/decouple-size branch December 28, 2023 21: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.

Speedup Exhaustive optimizer by decoupling size from EinExpr
1 participant