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 offset overflow check #144

Merged
merged 2 commits into from
Sep 14, 2020
Merged

add offset overflow check #144

merged 2 commits into from
Sep 14, 2020

Conversation

johnnychen94
Copy link
Member

Although this is related to overflow behavior in IdOffsetRange, the better place to raise an error is at the construction time of OffsetArray.

julia> a = rand(10);

julia> ao = OffsetArray(a, typemax(Int)-1)
ERROR: ArgumentError: Boundary overflow detected: offset 9223372036854775806 should be smaller than 9223372036854775797

Performance overhead should be acceptable:

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

# Before: 14.129 ns (0 allocations: 0 bytes)
@btime OffsetArray($a, -2:2, -2:2, -2:2, -2:2);

# After: 15.952 ns (0 allocations: 0 bytes)
@btime OffsetArray($a, -2:2, -2:2, -2:2, -2:2);

closes #136

@codecov
Copy link

codecov bot commented Sep 13, 2020

Codecov Report

Merging #144 into master will increase coverage by 1.25%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #144      +/-   ##
==========================================
+ Coverage   91.39%   92.64%   +1.25%     
==========================================
  Files           2        2              
  Lines         244      272      +28     
==========================================
+ Hits          223      252      +29     
+ Misses         21       20       -1     
Impacted Files Coverage Δ
src/OffsetArrays.jl 92.98% <100.00%> (+1.48%) ⬆️

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 55af8ad...967873f. Read the comment docs.

end
OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA}
OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA}

function overflow_check(r, offset::T) where T
throw_offseterror() = throw(ArgumentError("Boundary overflow detected: offset $offset should be smaller than $(typemax(T) - last(r))"))

Choose a reason for hiding this comment

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

Why define a function for this?

Copy link
Member

Choose a reason for hiding this comment

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

It appears to improve the performance, not entirely sure why

Copy link
Member Author

Choose a reason for hiding this comment

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

I opened a new issue in JuliaLang/julia#37558

In this PR I just switched back to the simple version and don't tweak it; IMO 5-10ns difference isn't critical to our use case.

@johnnychen94 johnnychen94 merged commit ce6298c into master Sep 14, 2020
@johnnychen94 johnnychen94 deleted the jc/boundscheck branch September 14, 2020 07:33
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.

Bound Check when offsetting near typemin/max(Int)
3 participants