-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
improve performance for string(...) #28876
Conversation
d511834
to
ebddae9
Compare
It's funny, merging back the
i.e. only the single Edit: It is not only
but with the methods merged:
Anyway, I'll leave it like this since it is consistently faser. |
If special case of Something like:
should be a bit faster. EDIT: added |
Why can't we bring back the previous optimized version for julia> function string2(a::Union{String,AbstractChar}...)
n = 0
for v in a
if v isa Char
n += Base.codelen(v)
else
n += sizeof(v)
end
end
out = Base._string_n(n)
offs = 1
for v in a
if v isa Char
x = bswap(reinterpret(UInt32, v))
for j in 1:Base.codelen(v)
unsafe_store!(pointer(out, offs), x % UInt8)
offs += 1
x >>= 8
end
else
unsafe_copyto!(pointer(out,offs), pointer(v), sizeof(v))
offs += sizeof(v)
end
end
return out
end
string2 (generic function with 1 method)
julia> @btime string2('a', "fsf", 'a', "fdsf")
42.563 ns (1 allocation: 32 bytes)
"afsfafdsf"
julia> @btime string('a', "fsf", 'a', "fdsf")
259.211 ns (3 allocations: 176 bytes) |
Good point - we can . Though the signature should be As you are probably aware what you propose is not the "old" implementation of |
Yeah, the optimization I was referring to bringing back was the one that used to look like: Lines 350 to 392 in 7711781
I pushed an update. I kept the previos modifications to |
All looks good to me. The only fear I had is whether compiler efficiently handles the situation of 3 element union and |
* improve performance for various string methods (cherry picked from commit 7314249)
* improve performance for various string methods (cherry picked from commit 7314249)
* improve performance for various string methods (cherry picked from commit 7314249)
* improve performance for various string methods (cherry picked from commit 7314249)
Before:
After:
Ref JuliaLang/LinearAlgebra.jl#557