-
Notifications
You must be signed in to change notification settings - Fork 60
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
Experiment: Potential speed up strategies #399
Draft
svilupp
wants to merge
9
commits into
apache:main
Choose a base branch
from
svilupp:arrow-turbo
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
71a3ba7
speed hacks for Arrow
svilupp e15f9d6
add ZstdDecompressor to transcode!
svilupp 6caea63
added deps
svilupp cc5dad8
updated inlinestrings to catch chainedvectors (moved deeper in)
svilupp 2148cc9
extend inlinestring fromarrow to apply to missing type union
svilupp d4a3ef9
add support for ChainedVector and move inlining to the outer loop (sa…
svilupp 966287a
remove loose debug statement
svilupp 153b82a
fix lock name typo
svilupp b476043
add compat for InlineStrings
svilupp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# The following change needs to be upstreamed to TranscodingStreams.jl: | ||
|
||
# We know the size of each output buffer (saved within Arrow metadata) | ||
# The below functions mutates the provided output buffer. | ||
function _transcode!(codec::Union{LZ4FrameDecompressor,ZstdDecompressor}, data::TS.ByteData,output::TS.Buffer) | ||
input = TS.Buffer(data) | ||
error = TS.Error() | ||
code = TS.startproc(codec, :write, error) | ||
if code === :error | ||
@goto error | ||
end | ||
# n = TS.minoutsize(codec, buffermem(input)) | ||
@label process | ||
# makemargin!(output, n) | ||
Δin, Δout, code = TS.process(codec, TS.buffermem(input), TS.marginmem(output), error) | ||
@debug( | ||
"called process()", | ||
code = code, | ||
input_size = buffersize(input), | ||
output_size = marginsize(output), | ||
input_delta = Δin, | ||
output_delta = Δout, | ||
) | ||
TS.consumed!(input, Δin) | ||
TS.supplied!(output, Δout) | ||
if code === :error | ||
@goto error | ||
elseif code === :end | ||
if TS.buffersize(input) > 0 | ||
if TS.startproc(codec, :write, error) === :error | ||
@goto error | ||
end | ||
# n = minoutsize(codec, buffermem(input)) | ||
@goto process | ||
end | ||
resize!(output.data, output.marginpos - 1) | ||
return output.data | ||
else | ||
# n = max(Δout, minoutsize(codec, buffermem(input))) | ||
@goto process | ||
end | ||
@label error | ||
if !(TS.haserror)(error) | ||
TS.set_default_error!(error) | ||
end | ||
throw(error[]) | ||
end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# This code should be moved into InlineStrings extensions | ||
|
||
### Type extensions | ||
# Use InlineStrings to get data from pointers (for getindex and similar) | ||
ArrowTypes.fromarrow(::Type{T}, ptr::Ptr{UInt8}, len::Int) where {T<:InlineString} =ArrowTypes.fromarrow(T, T(ptr, len)) | ||
ArrowTypes.fromarrow(::Type{Union{T,Missing}}, ptr::Ptr{UInt8}, len::Int) where {T<:InlineString} =ArrowTypes.fromarrow(T, T(ptr, len)) | ||
|
||
### Utilities for inlining strings | ||
# determines the maximum string length necessary for the offsets | ||
# calculates difference between offsets as a proxy for string size | ||
function _maximum_diff(v::AbstractVector{<:Integer}) | ||
mx = first(v) | ||
prev = mx | ||
@inbounds if length(v) > 1 | ||
mx = max(mx, v[2] - prev) | ||
prev = v[2] | ||
for i in firstindex(v)+2:lastindex(v) | ||
diff = v[i] - prev | ||
mx < diff && (mx = diff) | ||
prev = v[i] | ||
end | ||
end | ||
mx | ||
end | ||
# extract offsets from Arrow.List | ||
_offsetsints(arrowlist::Arrow.List) = arrowlist.offsets.offsets | ||
|
||
# convert strings to InlineStrings, does not check validity (hence unsafe!) - simply swaps the type | ||
function _unsafe_convert(::Type{Arrow.List{S, O, A}}, vect::Arrow.List{T,O,A}) where {S<:Union{InlineString, Union{Missing, InlineString}},T<:Union{AbstractString, Union{Missing, AbstractString}},O,A} | ||
Arrow.List{S,O,A}(vect.arrow, vect.validity, vect.offsets, vect.data, vect.ℓ, vect.metadata) | ||
end | ||
|
||
# passthrough for non-strings | ||
pick_string_type(::Type{T}, offsets) where {T} = T | ||
pick_string_type(::Type{Union{Missing,T}}, offsets) where {T<:AbstractString} = Union{Missing,pick_string_type(T, offsets)} | ||
function pick_string_type(::Type{T}, offsets::AbstractVector{<:Integer}) where {T<:AbstractString} | ||
max_size = _maximum_diff(offsets) | ||
# if the maximum string length is less than 255, we can use InlineStrings | ||
return max_size < 255 ? InlineStringType(max_size) : T | ||
end | ||
# find one joint string type for all chained arrays - vector of vectors | ||
function pick_string_type(::Type{T}, vectoffsets::AbstractVector{<:AbstractVector{<:Integer}}) where {T<:AbstractString} | ||
max_size = _maximum_diff.(vectoffsets)|>maximum | ||
# if the maximum string length is less than 255, we can use InlineStrings | ||
return max_size < 255 ? InlineStringType(max_size) : T | ||
end | ||
|
||
# extend inlinestrings to pass through Arrow.Lists | ||
_inlinestrings(vect::AbstractVector) = vect | ||
|
||
## methods for SentinelArrays.ChainedVector (if we have many RecordBatches / partitions) | ||
# if it's already an InlineString, we can pass it through | ||
_inlinestrings(vect::SentinelArrays.ChainedVector{<:Union{T,Union{T,Missing}}}) where {T<:InlineString} = vect | ||
# if we detect a String type, try to inline it -- we need to find one unified type across all chained arrays | ||
function _inlinestrings(vectofvect::SentinelArrays.ChainedVector{T, Arrow.List{T,O,A}}) where {T<:Union{AbstractString,Union{Missing,AbstractString}},O,A} | ||
# find the smallest common denominator string type for all chained arrays | ||
S = pick_string_type(T, _offsetsints.(vectofvect.arrays)) | ||
if S == T | ||
# if the type is the same, we can pass it through | ||
return vectofvect | ||
else | ||
# otherwise, we need to reconstruct the ChainedVector with the new string type | ||
# TODO: look into in-place conversion | ||
return SentinelArrays.ChainedVector(_unsafe_convert.(Arrow.List{S,O,A},vectofvect.arrays)) | ||
end | ||
end | ||
|
||
# TODO: check that we handle ChainedVector that contains something else than Arrow.List with Strings | ||
|
||
# if we detect that the strings are small enough, we can inline them | ||
function _inlinestrings(vect::Arrow.List{T,O,A}) where {T<:Union{AbstractString,Union{AbstractString,Missing}},O,A} | ||
S = pick_string_type(T, _offsetsints(vect)) | ||
if S == T | ||
return vect | ||
else | ||
# reconstruct the Arrow.List with the new string type | ||
return _unsafe_convert(Arrow.List{S,O,A}, vect) | ||
end | ||
end | ||
|
||
## methods for Arrow.List (if we have only 1 RecordBatch, ie, unpartitioned) | ||
# if it's already an InlineString, we can pass it through | ||
_inlinestrings(vect::Arrow.List{T,O,A}) where {T<:InlineString,O,A} = vect | ||
# if we detect that the strings are small enough, we can inline them | ||
function _inlinestrings(vect::Arrow.List{T,O,A}) where {T<:Union{AbstractString,Union{AbstractString,Missing}},O,A} | ||
S = pick_string_type(T, vect.offsets.offsets) | ||
# reconstruct the Arrow.List with the new string type | ||
Arrow.List{S,O,A}(vect.arrow, vect.validity, vect.offsets, vect.data, vect.ℓ, vect.metadata) | ||
end |
This file contains 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
This file contains 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
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.
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.
Missing compat entry for InlineStrings?
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.
Added. I set it to 1.4, as I don't understand its evolution up to this point, but I have a suspicion that "1" would work as well.
To be clear, I'm not sure if this PR could ever be merged.
I've hijacked (copy&pasted) transcode method from TranscodingStreams to create the mutating version, which is probably not the best practice :-/