Skip to content

Commit 18dbb58

Browse files
Add splitkwargs methods to avoid excessive allocations (#126)
* Add splitkwargs methods to avoid excessive allocations On current master, we have the unfortunate situation where users of `splitkwargs` can't avoid some ~8.5K allocations, as shown by: ```julia julia> @time GzipDecompressorStream(buf) 0.002668 seconds (8.47 k allocations: 404.656 KiB) TranscodingStreams.TranscodingStream{GzipDecompressor, Base.BufferStream}(<mode=idle>) ``` With this code in this PR (new method specializations for splitkwargs), as suggested by @nickrobinson251, we now get: ```julia julia> @time GzipDecompressorStream(buf) 0.000021 seconds (10 allocations: 32.562 KiB) TranscodingStreams.TranscodingStream{GzipDecompressor, Base.BufferStream}(<mode=idle>) ``` This was discovered as we're starting to review performanc and allocations in HTTP.jl, which uses `GzipDecompressorStream` to automatically decode responses with the gzip encoding. Co-Authored-By: Nick Robinson <[email protected]> * Compat for 1.6 Co-authored-by: Nick Robinson <[email protected]>
1 parent fa08bae commit 18dbb58

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

src/stream.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ function Base.show(io::IO, stream::TranscodingStream)
134134
end
135135

136136
# Split keyword arguments.
137+
@nospecialize
138+
@static if isdefined(Base, :Pairs)
139+
splitkwargs(kwargs::Base.Pairs, ks::Tuple{Vararg{Symbol}}) = splitkwargs(NamedTuple(kwargs), ks)
140+
end
141+
function splitkwargs(kwargs::NamedTuple, ks::Tuple{Vararg{Symbol}})
142+
non_ks = Base.diff_names(keys(kwargs), ks)
143+
ks = Base.diff_names(keys(kwargs), non_ks)
144+
return NamedTuple{ks}(kwargs), NamedTuple{non_ks}(kwargs)
145+
end
137146
function splitkwargs(kwargs, keys)
138147
hits = []
139148
others = []
@@ -142,6 +151,7 @@ function splitkwargs(kwargs, keys)
142151
end
143152
return hits, others
144153
end
154+
@specialize
145155

146156
# Check that mode is valid.
147157
macro checkmode(validmodes)

0 commit comments

Comments
 (0)