Skip to content

Commit ddf5d15

Browse files
committed
improve performance for string indexing
1 parent 8fe1d24 commit ddf5d15

File tree

2 files changed

+11
-14
lines changed

2 files changed

+11
-14
lines changed

base/strings/basic.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ julia> length("jμΛIα")
344344
"""
345345
length(s::AbstractString) = @inbounds return length(s, 1, ncodeunits(s))
346346

347-
function length(s::AbstractString, i::Int, j::Int)
347+
@inline function length(s::AbstractString, i::Int, j::Int)
348348
@boundscheck begin
349349
0 < i ncodeunits(s)+1 || throw(BoundsError(s, i))
350350
0  j < ncodeunits(s)+1 || throw(BoundsError(s, j))
@@ -395,7 +395,7 @@ ERROR: BoundsError: attempt to access "α"
395395
"""
396396
thisind(s::AbstractString, i::Integer) = thisind(s, Int(i))
397397

398-
function thisind(s::AbstractString, i::Int)
398+
@inline function thisind(s::AbstractString, i::Int)
399399
z = ncodeunits(s) + 1
400400
i == z && return i
401401
@boundscheck 0  i z || throw(BoundsError(s, i))
@@ -603,7 +603,7 @@ julia> first("∀ϵ≠0: ϵ²>0", 3)
603603
"∀ϵ≠"
604604
```
605605
"""
606-
first(s::AbstractString, n::Integer) = s[1:min(end, nextind(s, 0, n))]
606+
first(s::AbstractString, n::Integer) = @inbounds s[1:min(end, nextind(s, 0, n))]
607607

608608
"""
609609
last(s::AbstractString, n::Integer)
@@ -621,7 +621,7 @@ julia> last("∀ϵ≠0: ϵ²>0", 3)
621621
"²>0"
622622
```
623623
"""
624-
last(s::AbstractString, n::Integer) = s[max(1, prevind(s, ncodeunits(s)+1, n)):end]
624+
last(s::AbstractString, n::Integer) = @inbounds s[max(1, prevind(s, ncodeunits(s)+1, n)):end]
625625

626626
"""
627627
reverseind(v, i)

base/strings/string.jl

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,10 @@ typemin(::String) = typemin(String)
107107

108108
## thisind, nextind ##
109109

110-
thisind(s::String, i::Int) = _thisind_str(s, i)
110+
Base.@propagate_inbounds thisind(s::String, i::Int) = _thisind_str(s, i)
111111

112112
# s should be String or SubString{String}
113-
function _thisind_str(s, i::Int)
113+
@inline function _thisind_str(s, i::Int)
114114
i == 0 && return 0
115115
n = ncodeunits(s)
116116
i == n + 1 && return i
@@ -128,10 +128,10 @@ function _thisind_str(s, i::Int)
128128
return i
129129
end
130130

131-
nextind(s::String, i::Int) = _nextind_str(s, i)
131+
Base.@propagate_inbounds nextind(s::String, i::Int) = _nextind_str(s, i)
132132

133133
# s should be String or SubString{String}
134-
function _nextind_str(s, i::Int)
134+
@inline function _nextind_str(s, i::Int)
135135
i == 0 && return 1
136136
n = ncodeunits(s)
137137
@boundscheck between(i, 1, n) || throw(BoundsError(s, i))
@@ -237,7 +237,7 @@ end
237237

238238
getindex(s::String, r::UnitRange{<:Integer}) = s[Int(first(r)):Int(last(r))]
239239

240-
function getindex(s::String, r::UnitRange{Int})
240+
@inline function getindex(s::String, r::UnitRange{Int})
241241
isempty(r) && return ""
242242
i, j = first(r), last(r)
243243
@boundscheck begin
@@ -248,14 +248,11 @@ function getindex(s::String, r::UnitRange{Int})
248248
j = nextind(s, j) - 1
249249
n = j - i + 1
250250
ss = _string_n(n)
251-
p = pointer(ss)
252-
for k = 1:n
253-
unsafe_store!(p, codeunit(s, i + k - 1), k)
254-
end
251+
unsafe_copyto!(pointer(ss), pointer(s, i), n)
255252
return ss
256253
end
257254

258-
function length(s::String, i::Int, j::Int)
255+
@inline function length(s::String, i::Int, j::Int)
259256
@boundscheck begin
260257
0 < i ncodeunits(s)+1 || throw(BoundsError(s, i))
261258
0  j < ncodeunits(s)+1 || throw(BoundsError(s, j))

0 commit comments

Comments
 (0)