Skip to content

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.

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)

4 participants