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

replace ind2sub/sub2ind by CartesianIndices/LinearIndices #451

Merged
merged 2 commits into from
Oct 31, 2023

Conversation

jishnub
Copy link
Contributor

@jishnub jishnub commented Oct 3, 2023

This avoids using the internal Base methods

@codecov
Copy link

codecov bot commented Oct 3, 2023

Codecov Report

Merging #451 (0711c40) into main (e86b148) will increase coverage by 0.74%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #451      +/-   ##
==========================================
+ Coverage   84.66%   85.41%   +0.74%     
==========================================
  Files          13       13              
  Lines        8734     8740       +6     
==========================================
+ Hits         7395     7465      +70     
+ Misses       1339     1275      -64     
Files Coverage Δ
src/sparsematrix.jl 95.69% <100.00%> (+<0.01%) ⬆️
src/sparsevector.jl 95.43% <100.00%> (+0.01%) ⬆️

... and 2 files with indirect coverage changes

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

@ViralBShah
Copy link
Member

Is there any perf impact?

@jishnub
Copy link
Contributor Author

jishnub commented Oct 4, 2023

There does appear to be a small performance impact.

julia> S = sparse([1:500;], [x + (isodd(x) ? 1 : -1) for x in 1:500], [1:500;]);

julia> @btime $S[1:100_000];
  1.439 μs (2 allocations: 8.12 KiB) # master
  1.531 μs (2 allocations: 8.12 KiB) # PR

In this case, the difference appears to arise from indexing into the LinearIndices. I'm unsure why this indexing is slower, as it calls _sub2ind internally, and bounds-checking is disabled.

@ViralBShah
Copy link
Member

I wonder what the speed impact would be on larger operations. It is a 7% slowdown in the benchmark, which may perhaps manifest as 1-2% slowdown on larger operations or applications.

@vtjnash
Copy link
Contributor

vtjnash commented Oct 31, 2023

I seem to get the same code for these, so maybe that was just a benchmarking artifact of some runs?

julia> LinearIndices(Matrix{Int}(undef,0,0)) |> typeof
julia> code_native((ans, Int, Int)) do x, y, z; @inbounds x[y, z]; end
julia> code_native((Tuple{Int,Int}, Int, Int)) do x, y, z; @inbounds Base._sub2ind(x, y, z); end

Seems like this should be good to merge to me

@ViralBShah ViralBShah merged commit bd2bda8 into main Oct 31, 2023
8 checks passed
@ViralBShah ViralBShah deleted the jishnub/sub2ind branch October 31, 2023 14:17
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