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

Specialize iterate #181

Merged
merged 3 commits into from
Aug 29, 2022
Merged

Specialize iterate #181

merged 3 commits into from
Aug 29, 2022

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Aug 14, 2022

On master:

julia> @btime fill(2, 300, 300);
  61.167 μs (2 allocations: 703.17 KiB)

julia> x = Fill(2, 300, 300);

julia> @btime [i for i in $x];
  77.769 μs (2 allocations: 703.17 KiB)

This PR makes them both equally fast:

julia> @btime [i for i in $x];
  60.280 μs (2 allocations: 703.17 KiB)

I've also removed some specializations of sum, as the specialized Base._mapreduce_dim covers these. On this PR:

julia> @btime sum(x->x^2, $x);
  3.355 ns (0 allocations: 0 bytes)

julia> @btime sum($x);
  3.358 ns (0 allocations: 0 bytes)

@codecov
Copy link

codecov bot commented Aug 15, 2022

Codecov Report

Merging #181 (f6f0fd0) into master (d04c596) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master     #181   +/-   ##
=======================================
  Coverage   96.94%   96.95%           
=======================================
  Files           4        4           
  Lines         655      657    +2     
=======================================
+ Hits          635      637    +2     
  Misses         20       20           
Impacted Files Coverage Δ
src/FillArrays.jl 95.57% <100.00%> (+0.02%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@@ -121,7 +121,7 @@ Fill{T,0}(x::T, ::Tuple{}) where T = Fill{T,0,Tuple{}}(x, ()) # ambiguity fix
@inline Fill{T,N,Axes}(F::Fill{T,N,Axes}) where {T,N,Axes} = F

@inline axes(F::Fill) = F.axes
@inline size(F::Fill) = length.(F.axes)
@inline size(F::Fill) = map(length, F.axes)
Copy link
Member Author

@jishnub jishnub Aug 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change helps in cases where types aren't inferred:

julia> x = (1:3, 1:3); y = Any[x];

julia> @btime (x -> map(length, x[1]))($y);
  23.936 ns (1 allocation: 32 bytes)

julia> @btime (x -> length.(x[1]))($y);
  367.806 ns (3 allocations: 128 bytes)

@jishnub
Copy link
Member Author

jishnub commented Aug 18, 2022

Gentle bump @dlfivefifty

1 similar comment
@jishnub
Copy link
Member Author

jishnub commented Aug 21, 2022

Gentle bump @dlfivefifty

@dlfivefifty dlfivefifty merged commit 6ed6ec5 into JuliaArrays:master Aug 29, 2022
@jishnub jishnub deleted the iterate branch August 30, 2022 02:51
@jishnub
Copy link
Member Author

jishnub commented Aug 30, 2022

For the record, this performance match is obtained on v1.9. The performance is still somewhat inferior on v1.8.
On v1.9

julia> x = Fill(2, 300, 300);

julia> @benchmark [i for i in $x]
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min  max):  60.455 μs   1.435 ms  ┊ GC (min  max): 0.00%   0.00%
 Time  (median):     68.263 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   75.443 μs ± 41.992 μs  ┊ GC (mean ± σ):  6.53% ± 10.20%

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

 Memory estimate: 703.17 KiB, allocs estimate: 2.

julia> @benchmark fill(2, 300, 300)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min  max):  59.974 μs  715.494 μs  ┊ GC (min  max): 0.00%  90.87%
 Time  (median):     69.090 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   79.698 μs ±  62.497 μs  ┊ GC (mean ± σ):  9.81% ± 10.92%

  ▇█▄▃▂▂▁                                                      ▂
  █████████▇▄▄▃▃▄▄▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▄▆▇▇ █
  60 μs         Histogram: log(frequency) by time       552 μs <

 Memory estimate: 703.17 KiB, allocs estimate: 2.

On Julia v1.8:

julia> @benchmark fill(2, 300, 300)
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min  max):  58.523 μs  576.305 μs  ┊ GC (min  max): 0.00%  55.65%
 Time  (median):     68.670 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   76.727 μs ±  40.618 μs  ┊ GC (mean ± σ):  6.07% ± 10.07%

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

 Memory estimate: 703.17 KiB, allocs estimate: 2.

julia> @benchmark [i for i in $x]
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min  max):  67.364 μs   2.151 ms  ┊ GC (min  max): 0.00%   0.00%
 Time  (median):     75.189 μs              ┊ GC (median):    0.00%
 Time  (mean ± σ):   86.099 μs ± 65.544 μs  ┊ GC (mean ± σ):  8.51% ± 10.57%

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

 Memory estimate: 703.17 KiB, allocs estimate: 2.

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.

2 participants