Improve performance of Symbol concatenation#41992
Merged
KristofferC merged 2 commits intoJuliaLang:masterfrom Aug 26, 2021
Merged
Improve performance of Symbol concatenation#41992KristofferC merged 2 commits intoJuliaLang:masterfrom
KristofferC merged 2 commits intoJuliaLang:masterfrom
Conversation
ararslan
reviewed
Aug 24, 2021
Member
|
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)
:testmelower 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)
:testmeSimilar ratio in time improvement, but 2 fewer allocations, 1/5th the space, and hugely more versatile. |
Contributor
Author
|
Oh nice, so shall I change to the low-level hackery? I can add some tests to make sure nothing breaks. |
Member
|
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! |
Contributor
Author
|
I took a look and I think the existing tests seem comprehensive enough? Maybe the only thing useful to add would be empties? |
vtjnash
approved these changes
Aug 26, 2021
LilithHafner
pushed a commit
to LilithHafner/julia
that referenced
this pull request
Feb 22, 2022
LilithHafner
pushed a commit
to LilithHafner/julia
that referenced
this pull request
Mar 8, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This concatenation method is used a lot in Plots.jl and has a material impact on performance: JuliaPlots/Plots.jl#3763