From b41d6043ff9298d806de0b5f407262b081edd470 Mon Sep 17 00:00:00 2001 From: Steve Kelly Date: Mon, 10 Jul 2023 10:08:28 +0100 Subject: [PATCH] A few minor perforamnce fixes (#470) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * improve `to_uint64` Reduces some allocations and intermediate work. PR: ``` julia> @benchmark JLD2.to_uint64(rand(UInt8, 5)) BenchmarkTools.Trial: 10000 samples with 983 evaluations. Range (min … max): 58.730 ns … 1.927 μs ┊ GC (min … max): 0.00% … 93.54% Time (median): 65.484 ns ┊ GC (median): 0.00% Time (mean ± σ): 75.603 ns ± 116.148 ns ┊ GC (mean ± σ): 10.53% ± 6.58% ▃▁▆█▆▂ ▂▄▆███████▆▅▄▄▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃ 58.7 ns Histogram: frequency by time 117 ns < Memory estimate: 224 bytes, allocs estimate: 4. ``` Master: ``` julia> @benchmark JLD2.to_uint64(rand(UInt8, 5)) BenchmarkTools.Trial: 10000 samples with 981 evaluations. Range (min … max): 63.088 ns … 1.688 μs ┊ GC (min … max): 0.00% … 94.44% Time (median): 69.438 ns ┊ GC (median): 0.00% Time (mean ± σ): 78.976 ns ± 92.630 ns ┊ GC (mean ± σ): 8.47% ± 6.84% ▂▄▅▇▇█▇▆▅▅▄▃▃▃▂▂▃▂▂▁▁ ▂ ██████████████████████▇▇▆▆▇▆██▇█▇▇▇██▇▆▆▆▄▁▃▃▃▆▅▆▆▅▆▄▄▃▄▆██ █ 63.1 ns Histogram: log(frequency) by time 130 ns < Memory estimate: 256 bytes, allocs estimate: 4. ``` * fix failure to const-prop in error message this was not getting const propped, so just mark as such * improve type stability in show_group --- src/compression.jl | 11 +++++------ src/groups.jl | 10 +++++++--- src/misc.jl | 4 ++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/compression.jl b/src/compression.jl index ff61a6d9..0d0fcc83 100644 --- a/src/compression.jl +++ b/src/compression.jl @@ -65,18 +65,17 @@ const ID_TO_DECOMPRESSOR = Dict( UInt16(32004) => (:CodecLz4, :LZ4FrameCompressor, :LZ4FrameDecompressor, "LZ4"), ) +const compressor_list_string = map(values(ID_TO_DECOMPRESSOR)) do val + "\n\t"*string(val[1])*"."*string(val[2]) +end + issupported_filter(filter_id) = filter_id ∈ keys(ID_TO_DECOMPRESSOR) function verify_compressor(compressor) (compressor isa Bool || haskey(COMPRESSOR_TO_ID, nameof(typeof(compressor)))) && return - crs = map(values(ID_TO_DECOMPRESSOR)) do val - "\n\t"*string(val[1])*"."*string(val[2]) - end - - throw(ArgumentError("""Unsupported Compressor - Supported Compressors are $(crs...)""")) + Supported Compressors are $(compressor_list_string...)""")) end ############################################################################################################# # Dynamic Package Loading Logic copied from FileIO diff --git a/src/groups.jl b/src/groups.jl index 01ab9c10..e5f2e6f2 100644 --- a/src/groups.jl +++ b/src/groups.jl @@ -644,7 +644,7 @@ function show_group(io::IO, g::Group, maxnumlines::Int=10, prefix::String=" ", s iszero(maxnumlines) && return 0 if g.f.n_times_opened == 0 print(io, " (closed)") - return + return -1 end ks = collect(keys(g)) @@ -652,7 +652,7 @@ function show_group(io::IO, g::Group, maxnumlines::Int=10, prefix::String=" ", s if isempty(ks) && prefix == " " print(io, " (no datasets)") - return + return -1 end for i = 1:length(ks) @@ -670,7 +670,11 @@ function show_group(io::IO, g::Group, maxnumlines::Int=10, prefix::String=" ", s if !isempty(newg) if (maxnumlines > 1 || (islast && maxnumlines >= 2)) print(io, '\n') - maxnumlines = show_group(io, newg, islast ? maxnumlines : maxnumlines-1, prefix*(islast ? " " : "│ "), false) + ret = show_group(io, newg, islast ? maxnumlines : maxnumlines-1, prefix*(islast ? " " : "│ "), false) + if ret < 0 + error("closed or empty dataset, this should not happen") + end + maxnumlines = ret maxnumlines += islast ? 0 : 1 else nentries = length(keys(newg)) diff --git a/src/misc.jl b/src/misc.jl index 79f372fd..f606a358 100644 --- a/src/misc.jl +++ b/src/misc.jl @@ -142,9 +142,9 @@ function uintofsize(sz) end function to_uint64(bts::Vector{UInt8}) - bts2 = [bts; zeros(UInt8, 8-length(bts))] + bts2 = append!(zeros(UInt8, 8-length(bts)), reverse(bts)) u = zero(UInt64) - for b in reverse(bts2) + for b in bts2 u = u << 8 u += b end