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

inline OffsetArray constructors #164

Merged
merged 3 commits into from
Oct 9, 2020
Merged

inline OffsetArray constructors #164

merged 3 commits into from
Oct 9, 2020

Conversation

johnnychen94
Copy link
Member

@johnnychen94 johnnychen94 commented Oct 9, 2020

To reduce the overhead

julia> a = zeros(5,5,5,5);

julia> @btime OffsetArray($a, -2:2, -2:2, -2:2, -2:2);
  3.088 ns (0 allocations: 0 bytes) # this PR; 1.5.2
  2.463 ns (0 allocations: 0 bytes) # this PR; 1.6-dev
  16.180 ns (0 allocations: 0 bytes) # master; 1.5.2
  15.693 ns (0 allocations: 0 bytes) # master; 1.6-dev

@codecov
Copy link

codecov bot commented Oct 9, 2020

Codecov Report

Merging #164 into master will decrease coverage by 0.26%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #164      +/-   ##
==========================================
- Coverage   99.21%   98.95%   -0.27%     
==========================================
  Files           4        8       +4     
  Lines         256      477     +221     
==========================================
+ Hits          254      472     +218     
- Misses          2        5       +3     
Impacted Files Coverage Δ
src/OffsetArrays.jl 99.44% <100.00%> (-0.01%) ⬇️
D:/a/OffsetArrays.jl/OffsetArrays.jl/src/utils.jl 100.00% <0.00%> (ø)
D:/a/OffsetArrays.jl/OffsetArrays.jl/src/origin.jl 100.00% <0.00%> (ø)
D:/a/OffsetArrays.jl/OffsetArrays.jl/src/axes.jl 97.56% <0.00%> (ø)
...ffsetArrays.jl/OffsetArrays.jl/src/OffsetArrays.jl 98.70% <0.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 08f969b...ea29ae7. Read the comment docs.

Copy link
Member

@timholy timholy left a comment

Choose a reason for hiding this comment

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

It doesn't get inlined now due to length(::UnitRange{Int}) having two checked-arithmetic operations. (See Base.print_statement_costs on 1.6.) This seems reasonable, and gives Julia a chance to elide the wrapper in the caller.

@boundscheck overflow_check.(axes(parent), offsets)
overflow_check.(axes(parent), offsets)
Copy link
Member

Choose a reason for hiding this comment

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

It seems like this would allocate an array, is that completely elided?

Copy link
Member

Choose a reason for hiding this comment

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

Good thought. Really this should be a foreach call.

Copy link
Member Author

Choose a reason for hiding this comment

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

Hmmm, turns out map is faster here.

  • map: 3.004ns
  • foreach: 5.261 ns

Is there a way to check if a method is properly inlined?

Copy link
Member

@timholy timholy Oct 9, 2020

Choose a reason for hiding this comment

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

foreach doesn't force-specialize on the function. It could probably use some love...

I would guess, though, that it's elided. It should be creating a tuple (not an array) and that should get elided.

Is there a way to check if a method is properly inlined?

@code_typed or Cthulhu

Copy link
Member

@fredrikekre fredrikekre Oct 9, 2020

Choose a reason for hiding this comment

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

I would guess, though, that it's elided. It should be creating a tuple

Yea looks from the benchmarks that it is elided, it's just not immediatly obvious from the code (I didnt't think about the inputs beeing tuples). map is probably fine then (with a comment?) although something like foreach would be more typical when you don't care about the result I think.

Copy link
Member Author

Choose a reason for hiding this comment

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

map on tuple can be optimized by the compiler and thus not using foreach here

This is what I added. Unsure what's the terminology here, "constant folding"?

Copy link
Member

Choose a reason for hiding this comment

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

You could just say "optimized away." Another option would be DCE.

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.

3 participants