Skip to content

Commit f741df9

Browse files
authored
optimize overflow_check (#150)
1 parent 535efad commit f741df9

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/OffsetArrays.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,17 @@ OffsetVector{T,AA<:AbstractArray} = OffsetArray{T,1,AA}
2424
OffsetMatrix{T,AA<:AbstractArray} = OffsetArray{T,2,AA}
2525

2626
function overflow_check(r, offset::T) where T
27+
# This gives some performance boost https://github.com/JuliaLang/julia/issues/33273
28+
throw_upper_overflow_error() = throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or less than $(typemax(T) - last(r))"))
29+
throw_lower_overflow_error() = throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or greater than $(typemin(T) - first(r))"))
30+
2731
if offset > 0 && last(r) > typemax(T) - offset
28-
throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or less than $(typemax(T) - last(r))"))
32+
throw_upper_overflow_error()
2933
elseif offset < 0 && first(r) < typemin(T) - offset
30-
throw(ArgumentError("Boundary overflow detected: offset $offset should be equal or greater than $(typemin(T) - first(r))"))
34+
throw_lower_overflow_error()
3135
end
3236
end
37+
3338
## OffsetArray constructors
3439

3540
offset(axparent::AbstractUnitRange, ax::AbstractUnitRange) = first(ax) - first(axparent)

0 commit comments

Comments
 (0)