Skip to content

Commit ad8247c

Browse files
committed
Outline error-string creation to improve indices performance from >3ns -> <1ns.
1 parent 93107b0 commit ad8247c

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

src/ranges.jl

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -301,22 +301,20 @@ end
301301
@inline function _try_static(::StaticInt{M}, ::StaticInt{N}) where {M,N}
302302
@assert false "Unequal Indices: StaticInt{$M}() != StaticInt{$N}()"
303303
end
304+
@noinline unequal_error(x,y) = throw("Unequal Indices: x == $x != $y == y")
305+
@inline function check_equal(x, y)
306+
x == y || unequal_error(x,y)
307+
end
304308
@propagate_inbounds function _try_static(::StaticInt{N}, x) where {N}
305-
@boundscheck begin
306-
@assert N == x "Unequal Indices: StaticInt{$N}() != x == $x"
307-
end
309+
@boundscheck check_equal(StaticInt{N}(), x)
308310
return StaticInt{N}()
309311
end
310312
@propagate_inbounds function _try_static(x, ::StaticInt{N}) where {N}
311-
@boundscheck begin
312-
@assert N == x "Unequal Indices: x == $x != StaticInt{$N}()"
313-
end
313+
@boundscheck check_equal(x, StaticInt{N}())
314314
return StaticInt{N}()
315315
end
316316
@propagate_inbounds function _try_static(x, y)
317-
@boundscheck begin
318-
@assert x == y "Unequal Indices: x == $x != $y == y"
319-
end
317+
@boundscheck check_equal(x, y)
320318
return x
321319
end
322320

@@ -407,15 +405,15 @@ Base.eachindex(r::OptionallyStaticRange) = r
407405
Base.to_shape(x::OptionallyStaticRange) = length(x)
408406
Base.to_shape(x::Slice{T}) where {T<:OptionallyStaticRange} = length(x)
409407

410-
function Base.axes(S::Slice{T}) where {T<:OptionallyStaticRange}
408+
@inline function Base.axes(S::Slice{T}) where {T<:OptionallyStaticRange}
411409
if known_first(T) === 1 && known_step(T) === 1
412410
return (S.indices,)
413411
else
414412
return (Base.IdentityUnitRange(S.indices),)
415413
end
416414
end
417415

418-
function Base.axes1(S::Slice{T}) where {T<:OptionallyStaticRange}
416+
@inline function Base.axes1(S::Slice{T}) where {T<:OptionallyStaticRange}
419417
if known_first(T) === 1 && known_step(T) === 1
420418
return S.indices
421419
else

0 commit comments

Comments
 (0)