Skip to content

Commit 1c364b4

Browse files
authored
Stop pirating Base._searchindex and Base._rsearchindex (#9)
Before, these methods were extended with new signatures using big unions. These big unions included Base types, which meant that certain methodinstances using concrete types of Base was being invalidated unnecessarily. This PR stops the type piracy, cutting invalidations from 2000 -> 362.
1 parent abb97ca commit 1c364b4

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

src/search.jl

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,30 @@ end
7777
# The following functions require julia#37283 in Julia 1.6, which
7878
# allow us to search byte arrays (applied to codeunits(s)).
7979
@static if VERSION v"1.6.0-DEV.1341"
80-
function Base._searchindex(s::Union{StringViewAndSub,StringAndSub}, t::Union{StringViewAndSub,StringAndSub}, i::Integer)
80+
# Split into two identical methods to avoid pirating Base's methods, leading to ~370 invalidations.
81+
function Base._searchindex(s::StringViewAndSub, t::Union{StringViewAndSub,StringAndSub}, i::Integer)
82+
searchindex_internal(s, t, i)
83+
end
84+
85+
function Base._searchindex(s::Union{StringViewAndSub,StringAndSub}, t::StringViewAndSub, i::Integer)
86+
searchindex_internal(s, t, i)
87+
end
88+
89+
function searchindex_internal(s::Union{StringViewAndSub,StringAndSub}, t::Union{StringViewAndSub,StringAndSub}, i::Integer)
8190
# Check for fast case of a single byte
8291
lastindex(t) == 1 && return something(findnext(isequal(t[1]), s, i), 0)
8392
Base._searchindex(codeunits(s), codeunits(t), i)
8493
end
85-
function Base._rsearchindex(s::Union{StringViewAndSub,StringAndSub}, t::Union{StringViewAndSub,StringAndSub}, i::Integer)
94+
95+
function Base._rsearchindex(s::StringViewAndSub, t::Union{StringViewAndSub,StringAndSub}, i::Integer)
96+
rsearchindex_internal(s, t, i)
97+
end
98+
99+
function Base._rsearchindex(s::Union{StringViewAndSub,StringAndSub}, t::StringViewAndSub, i::Integer)
100+
rsearchindex_internal(s, t, i)
101+
end
102+
103+
function rsearchindex_internal(s::Union{StringViewAndSub,StringAndSub}, t::Union{StringViewAndSub,StringAndSub}, i::Integer)
86104
# Check for fast case of a single byte
87105
if lastindex(t) == 1
88106
return something(findprev(isequal(t[1]), s, i), 0)

0 commit comments

Comments
 (0)