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

add specialized reverse implementation for NTuple #50113

Merged
merged 1 commit into from
Feb 6, 2024

Conversation

sjkelly
Copy link
Contributor

@sjkelly sjkelly commented Jun 8, 2023

This adds a specialized version of reverse for NTuple
using a simple constructor.

1.9:

julia> a = tuple(rand(Int, 256)...);

julia> @benchmark reverse($a)
BenchmarkTools.Trial: 7099 samples with 1 evaluation.
 Range (min … max):  611.280 μs …   5.698 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     670.392 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   700.950 μs ± 144.952 μs  ┊ GC (mean ± σ):  1.37% ± 5.70%

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

 Memory estimate: 794.42 KiB, allocs estimate: 33663.

This commit:

julia> @benchmark reverse($a)
BenchmarkTools.Trial: 10000 samples with 995 evaluations.
 Range (min … max):  26.252 ns … 52.545 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     26.285 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   26.578 ns ±  0.940 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

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

 Memory estimate: 0 bytes, allocs estimate: 0.

This adds a specialized version of `reverse` for NTuple
using a simple constructor.

1.9:
```
julia> a = tuple(rand(Int, 256)...);

julia> @benchmark reverse($a)
BenchmarkTools.Trial: 7099 samples with 1 evaluation.
 Range (min … max):  611.280 μs …   5.698 ms  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     670.392 μs               ┊ GC (median):    0.00%
 Time  (mean ± σ):   700.950 μs ± 144.952 μs  ┊ GC (mean ± σ):  1.37% ± 5.70%

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

 Memory estimate: 794.42 KiB, allocs estimate: 33663.
```

This commit:
```
julia> @benchmark reverse($a)
BenchmarkTools.Trial: 10000 samples with 995 evaluations.
 Range (min … max):  26.252 ns … 52.545 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     26.285 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   26.578 ns ±  0.940 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

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

 Memory estimate: 0 bytes, allocs estimate: 0.
```
@sjkelly sjkelly changed the title add specialize reverse implementation for NTuple add specialized reverse implementation for NTuple Jun 8, 2023
@sjkelly sjkelly changed the title add specialized reverse implementation for NTuple add specialized reverse implementation for NTuple Jun 8, 2023
@oscardssmith oscardssmith added the performance Must go faster label Jun 8, 2023
@KristofferC
Copy link
Sponsor Member

Many other tuple functions have a limit size for when they stop specialising. Shouldn't that be the case here too?

@sjkelly
Copy link
Contributor Author

sjkelly commented Jun 9, 2023

Yes, that happens automatically with the non-Val version of ntuple. I will benchmark the tradeoff.

@vtjnash vtjnash merged commit 321cc2d into JuliaLang:master Feb 6, 2024
7 of 9 checks passed
@vtjnash
Copy link
Sponsor Member

vtjnash commented Feb 6, 2024

The other reverse call doesn't handle large tuples either, so this seems fine


# Specialized extensions for NTuple
function reverse(t::NTuple{N}) where N
ntuple(Val{N}()) do i
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

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

this is better than Val(N)?

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

It doesn't matter. Usually Val(N) is preferred as it is more concise, but it desugars to effectively the same thing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants