Skip to content
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

Use multithreaded load / save per entry #51

Merged
merged 6 commits into from
Feb 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "JLSO"
uuid = "9da8a3cd-07a3-59c0-a743-3fdc52c30d11"
license = "MIT"
authors = ["Invenia Technical Computing Corperation"]
version = "2.0.1"
version = "2.1.0"

[deps]
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
Expand Down
8 changes: 8 additions & 0 deletions src/JLSO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ using Memento
using Pkg: Pkg
using Pkg.Types: semver_spec

@static if VERSION < v"1.3.0"
macro spawn(ex)
esc(ex)
end
else
using Base.Threads: @spawn
end

# We need to import these cause of a deprecation on object index via strings.
import Base: getindex, setindex!

Expand Down
6 changes: 4 additions & 2 deletions src/JLSOFile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ struct JLSOFile
project::Dict{String, Any}
manifest::Dict{String, Any}
objects::Dict{Symbol, Vector{UInt8}}
lock::ReentrantLock
end

"""
Expand Down Expand Up @@ -56,10 +57,11 @@ function JLSOFile(
Pkg.TOML.parse(project_toml),
Pkg.TOML.parse(manifest_toml),
Dict{Symbol, Vector{UInt8}}(),
ReentrantLock()
)

for (key, val) in data
jlso[key] = val
@sync for (key, val) in data
@spawn jlso[key] = val
end

return jlso
Expand Down
12 changes: 9 additions & 3 deletions src/file_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ function Base.read(io::IO, ::Type{JLSOFile})
Pkg.TOML.parse(d["metadata"]["project"]),
manifest,
Dict{Symbol, Vector{UInt8}}(Symbol(k) => v for (k, v) in d["objects"]),
ReentrantLock()
)
end

Expand All @@ -88,9 +89,14 @@ function load(io::IO, objects::Symbol...)
objects = isempty(objects) ? names(jlso) : objects
result = Dict{Symbol, Any}()

for o in objects
# Note that calling getindex on the jlso triggers the deserialization of the object
result[o] = jlso[o]
@sync for o in objects
@spawn begin
# Note that calling getindex on the jlso triggers the deserialization of the object
deserialized = jlso[o]
lock(jlso.lock) do
result[o] = deserialized
end
end
end

return result
Expand Down
5 changes: 4 additions & 1 deletion src/serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ function setindex!(jlso::JLSOFile, value, name::Symbol)
compressing_buffer = compress(jlso.compression, buffer)
serialize(jlso.format, compressing_buffer, value)
complete_compression(compressing_buffer)
result = take!(buffer)

jlso.objects[name] = take!(buffer)
lock(jlso.lock) do
jlso.objects[name] = result
end
end
1 change: 1 addition & 0 deletions src/upgrade.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ function upgrade(src, dest, project, manifest)
project,
manifest,
Dict{Symbol, Vector{UInt8}}(Symbol(k) => v for (k, v) in d["objects"]),
ReentrantLock()
)
write(dest, jlso)
end
Expand Down
3 changes: 3 additions & 0 deletions test/file_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ end
project,
manifest,
Dict(:data => hw_5),
ReentrantLock()
)
end

Expand Down Expand Up @@ -94,6 +95,7 @@ end
project,
manifest,
Dict(:data => bytes),
ReentrantLock()
)

# Test failing to deserailize data because of missing modules will
Expand Down Expand Up @@ -130,6 +132,7 @@ end
project,
manifest,
Dict(:data => bytes),
ReentrantLock()
)

# Test failing to deserailize data because of missing modules will
Expand Down