Skip to content

Commit

Permalink
Avoid allocations in ascii_lc_isequal
Browse files Browse the repository at this point in the history
Fixes #724. Maybe in a perfect world, the compiler would be able to "see
through" the calls to `Iterators.Stateful` and `zip` iterating through
them to avoid allocating `Stateful` at all, but this also just feels
like a case of a very low-level function trying to use high-level
constructs. The proposed code here is an order of magnitude faster (~10x
in the few cases I tested), avoids all allocations, and IMO, is simpler
(I'm guessing most users don't immediately understand what
`Iterators.Stateful` is for).
  • Loading branch information
quinnj committed Jun 18, 2021
1 parent ffee0a0 commit 9692998
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/ascii.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ ascii_lc_isequal(a::UInt8, b::UInt8) = ascii_lc(a) == ascii_lc(b)
Case insensitive ASCII string comparison.
"""
function ascii_lc_isequal(a, b)
a = Iterators.Stateful(codeunits(a))
b = Iterators.Stateful(codeunits(b))
for (i, j) in zip(a, b)
if !ascii_lc_isequal(i, j)
return false
end
acu = codeunits(a)
bcu = codeunits(b)
len = length(acu)
len != length(bcu) && return false
for i = 1:len
@inbounds !ascii_lc_isequal(acu[i], bcu[i]) && return false
end
return isempty(a) && isempty(b)
return true
end

0 comments on commit 9692998

Please sign in to comment.