Skip to content

Commit e679494

Browse files
authored
Reduce allocation in blockfirsts (#281)
* Reduce allocation in blockfirsts * optimize only for known types
1 parent 347d5fb commit e679494

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

src/blockaxis.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ _blocklengths2blocklasts(blocks) = cumsum(blocks) # extra level to allow changin
6363
@inline blockedrange(blocks::Union{Tuple,AbstractVector}) = _BlockedUnitRange(_blocklengths2blocklasts(blocks))
6464

6565
@inline blockfirsts(a::BlockedUnitRange) = [a.first; @views(a.lasts[1:end-1]) .+ 1]
66+
# optimize common cases
67+
@inline function blockfirsts(a::BlockedUnitRange{<:Union{Vector, RangeCumsum{<:Any, <:UnitRange}}})
68+
v = Vector{eltype(a)}(undef, length(a.lasts))
69+
v[1] = a.first
70+
v[2:end] .= @views(a.lasts[oneto(end-1)]) .+ 1
71+
return v
72+
end
6673
@inline blocklasts(a::BlockedUnitRange) = a.lasts
6774

6875
_diff(a::AbstractVector) = diff(a)

0 commit comments

Comments
 (0)