-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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 of Symbol concatenation #41992
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
Seems like we should be able to do even better by going deeper. I seem to have a slower system than you: Before: julia> @btime Symbol(:test, :me)
152.419 ns (3 allocations: 160 bytes)
:testme lower level hackeryjulia> @eval Base begin
@inline function __unsafe_string!(out, s::Symbol, offs::Integer)
n = sizeof(s)
GC.@preserve s out unsafe_copyto!(pointer(out, offs), unsafe_convert(Ptr{UInt8},s), n)
return n
end
function string(a::Union{Char, String, SubString{String}, Symbol}...)
n = 0
for v in a
if v isa Char
n += ncodeunits(v)
else
n += sizeof(v)
end
end
out = _string_n(n)
offs = 1
for v in a
offs += __unsafe_string!(out, v, offs)
end
return out
end
end
string (generic function with 21 methods) After: julia> @btime Symbol(:test, :me)
119.850 ns (1 allocation: 32 bytes)
:testme Similar ratio in time improvement, but 2 fewer allocations, 1/5th the space, and hugely more versatile. |
Oh nice, so shall I change to the low-level hackery? I can add some tests to make sure nothing breaks. |
I just pushed it — I already had it locally. It'd be good to ensure we have testing that includes heterogeneous combinations of a Symbol or two with chars and strings; if you could add that or verify it it'd be great! |
I took a look and I think the existing tests seem comprehensive enough?
Maybe the only thing useful to add would be empties?
|
This concatenation method is used a lot in Plots.jl and has a material impact on performance: JuliaPlots/Plots.jl#3763