Skip to content

Commit

Permalink
optimize zip_findlast_entry and add benchmarks (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhz2 authored Oct 6, 2024
1 parent 237b528 commit 96de730
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
/.vscode/

/test/Manifest.toml
/benchmark/Manifest.toml
/Manifest.toml
fixture.tar.gz
fixture
Expand Down
6 changes: 6 additions & 0 deletions benchmark/Project.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"
PkgBenchmark = "32113eaa-f34f-5b0d-bd6c-c81e245fc73d"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ZipArchives = "49080126-0e18-4c2a-b176-c102e4b3760c"
11 changes: 11 additions & 0 deletions benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# ZipArchives.jl benchmarks

This directory contains benchmarks for ZipArchives. To run all the
benchmarks, launch `julia --project=benchmark` and enter:

``` julia
using PkgBenchmark
import ZipArchives

benchmarkpkg(ZipArchives)
```
24 changes: 24 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using BenchmarkTools
using Random
using ZipArchives

const SUITE = BenchmarkGroup()
rbench = SUITE["reading"] = BenchmarkGroup()

sink = IOBuffer()
names = String[]
ZipWriter(sink) do w
for i in 1:2000
local name = String(rand(UInt8('a'):UInt8('z'), 100))
push!(names, name)
zip_writefile(w, name, rand(UInt8, 10000))
end
end
data = take!(sink)
r = ZipReader(data)

rbench["ZipReader"] = @benchmarkable ZipReader($(data))
rbench["zip_findlast_entry nothing"] = @benchmarkable zip_findlast_entry($(r), $("abc"))
rbench["zip_findlast_entry first"] = @benchmarkable zip_findlast_entry($(r), $(names[begin]))
rbench["zip_findlast_entry last"] = @benchmarkable zip_findlast_entry($(r), $(names[end]))
rbench["zip_readentry"] = @benchmarkable zip_readentry($(r), $(1000))
5 changes: 3 additions & 2 deletions src/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ Return the index of the last entry with name `s` or `nothing` if not found.
zip_findlast_entry(x::HasEntries, s::AbstractString)::Union{Nothing, Int} = zip_findlast_entry(x, String(s))
function zip_findlast_entry(x::HasEntries, s::String)::Union{Nothing, Int}
data = codeunits(s)
findlast(eachindex(x.entries)) do i
_name_view(x, i) == data
n = length(data)
findlast(x.entries) do e
n == length(e.name_range) && view(x.central_dir_buffer, e.name_range) == data
end
end

Expand Down

0 comments on commit 96de730

Please sign in to comment.