Skip to content

Commit

Permalink
shorten compression code
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasIsensee committed Sep 21, 2024
1 parent 15c9478 commit ea27a1e
Showing 1 changed file with 11 additions and 70 deletions.
81 changes: 11 additions & 70 deletions src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,9 @@ function write_chunked_storage_message( io::IO,
data_address)
end

function decompress!(inptr::Ptr, data_length, element_size, n, decompressor::TranscodingStreams.Codec)
function decompress!(io::MemoryBackedIO, data_length, element_size, n, decompressor::TranscodingStreams.Codec)
ensureroom(io, data_length)
inptr = io.curptr
TranscodingStreams.initialize(decompressor)
data = transcode(decompressor, unsafe_wrap(Array, Ptr{UInt8}(inptr), data_length))::Array{UInt8, 1}
TranscodingStreams.finalize(decompressor)
Expand All @@ -223,41 +225,12 @@ function decompress!(data::Vector{UInt8}, data_length, element_size, num_element
end
return data_new
end
function decompress!(io::IOStream, data_length, element_size, n, decompressor)
read!(TranscodingStreams.TranscodingStream(decompressor, io), Vector{UInt8}(undef, element_size*n))
end

function read_compressed_array!(v::Array{T}, f::JLDFile{IO},
rr::ReadRepresentation{T,RR},
data_length::Integer,
filters
) where {T,RR, IO<:Union{MmapIO, ReadOnlyBuffer}}

invoke_again, decompressors = get_decompressor(filters)
if invoke_again
return Base.invokelatest(read_compressed_array!, v, f, rr, data_length, filters)::typeof(v)
end
io = f.io
inptr = io.curptr
element_size = odr_sizeof(RR)
n = length(v)
data = decompress!(inptr, data_length, element_size, n, decompressors[end])
if length(decompressors) > 1
for decompressor in decompressors[end-1:-1:1]
data = decompress!(data, length(data), element_size, n, decompressor)
end
end
@simd for i = 1:length(v)
dataptr = Ptr{Cvoid}(pointer(data, odr_sizeof(RR)*(i-1)+1))
if !jlconvert_canbeuninitialized(rr) || jlconvert_isinitialized(rr, dataptr)
@inbounds v[i] = jlconvert(rr, f, dataptr, NULL_REFERENCE)
end
end
io.curptr = inptr + data_length
v
function decompress!(io::IO, data_length, element_size, n, decompressor)
read!(TranscodingStreams.TranscodingStream(decompressor, io), Vector{UInt8}(undef, element_size*n))

Check warning on line 230 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L229-L230

Added lines #L229 - L230 were not covered by tests
end

function read_compressed_array!(v::Array{T}, f::JLDFile{IOStream},
function read_compressed_array!(v::Array{T}, f::JLDFile,
rr::ReadRepresentation{T,RR},
data_length::Integer,
filters,
Expand All @@ -277,45 +250,13 @@ function read_compressed_array!(v::Array{T}, f::JLDFile{IOStream},
data = decompress!(data, length(data), element_size, n, decompressor)
end
end
@simd for i = 1:n
dataptr = Ptr{Cvoid}(pointer(data, odr_sizeof(RR)*(i-1)+1))
cp0 = Ptr{Cvoid}(pointer(data))
@simd for i = eachindex(v)
dataptr = cp0 + element_size*(i-1)
if !jlconvert_canbeuninitialized(rr) || jlconvert_isinitialized(rr, dataptr)
@inbounds v[i] = jlconvert(rr, f, dataptr, NULL_REFERENCE)
v[i] = jlconvert(rr, f, dataptr, NULL_REFERENCE)
end
end
seek(io, data_offset + data_length)
v
end


function read_compressed_array!(v::Array{T}, f::JLDFile{IO},
rr::ReadRepresentation{T,RR},
data_length::Integer,
filters,
) where {T,RR, IO<:RWBuffer}
invoke_again, decompressors = get_decompressor(filters)
if invoke_again
return Base.invokelatest(read_compressed_array!, v, f, rr, data_length, filters)::typeof(v)
end

io = f.io
data_offset = position(io)
n = length(v)
element_size = odr_sizeof(RR)
buf = read!(io, Vector{UInt8}(undef, data_length))

data = decompress!(pointer(buf), data_length, element_size, n, decompressors[end])
if length(decompressors) > 1
for decompressor in decompressors[end-1:-1:1]
data = decompress!(data, length(data), element_size, n, decompressor)
end
end
@simd for i = 1:n
dataptr = Ptr{Cvoid}(pointer(data, odr_sizeof(RR)*(i-1)+1))
if !jlconvert_canbeuninitialized(rr) || jlconvert_isinitialized(rr, dataptr)
@inbounds v[i] = jlconvert(rr, f, dataptr, NULL_REFERENCE)
end
end
seek(io, data_offset + data_length)
v
end
end

0 comments on commit ea27a1e

Please sign in to comment.