Skip to content

Commit

Permalink
A few minor perforamnce fixes (#470)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
sjkelly authored Jul 10, 2023
1 parent 9c99348 commit b41d604
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 11 deletions.
11 changes: 5 additions & 6 deletions src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/groups.jl
Original file line number Diff line number Diff line change
Expand Up @@ -644,15 +644,15 @@ 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))
skiptypes && filter!(x -> x != "_types", ks)

if isempty(ks) && prefix == " "
print(io, " (no datasets)")
return
return -1
end

for i = 1:length(ks)
Expand All @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b41d604

Please sign in to comment.