Skip to content

Commit 0e1eceb

Browse files
loading: install at packages/$name/$slug instead of no names (#26057)
Save .ji files at libs/v#.#/$name/$slug.ji so all the caches for a given package name are in the same directory of that name. These can be a mix of .ji files for different packages with the same name but I think that's perfectly acceptable. Also base the slug code on CRC32 instead of modular arithmetic.
1 parent f965fd4 commit 0e1eceb

File tree

6 files changed

+35
-45
lines changed

6 files changed

+35
-45
lines changed

base/loading.jl

+25-36
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,9 @@ struct SHA1
9191
end
9292
end
9393
SHA1(s::Union{String,SubString{String}}) = SHA1(hex2bytes(s))
94+
string(hash::SHA1) = bytes2hex(hash.bytes)
9495

95-
convert(::Type{String}, hash::SHA1) = bytes2hex(convert(Vector{UInt8}, hash))
96-
convert(::Type{Vector{UInt8}}, hash::SHA1) = hash.bytes
97-
98-
string(hash::SHA1) = String(hash)
99-
show(io::IO, hash::SHA1) = print(io, "SHA1(", convert(String, hash), ")")
96+
show(io::IO, hash::SHA1) = print(io, "SHA1(", string(hash), ")")
10097
isless(a::SHA1, b::SHA1) = lexless(a.bytes, b.bytes)
10198
hash(a::SHA1, h::UInt) = hash((SHA1, a.bytes), h)
10299
==(a::SHA1, b::SHA1) = a.bytes == b.bytes
@@ -123,36 +120,28 @@ dummy_uuid(project_file::String) = isfile_casesensitive(project_file) ?
123120

124121
## package path slugs: turning UUID + SHA1 into a pair of 4-byte "slugs" ##
125122

126-
const SlugInt = UInt32 # max p = 4
127-
const chars = String(['A':'Z'; 'a':'z'; '0':'9'])
128-
const nchars = SlugInt(length(chars))
129-
const max_p = floor(Int, log(nchars, typemax(SlugInt) >>> 8))
123+
const slug_chars = String(['A':'Z'; 'a':'z'; '0':'9'])
130124

131-
function slug(x::SlugInt, p::Int)
132-
1 p max_p || # otherwise previous steps are wrong
133-
error("invalid slug size: $p (need 1 ≤ p ≤ $max_p)")
134-
return sprint() do io
125+
function slug(x::UInt32, p::Int)
126+
sprint(sizehint=p) do io
127+
n = length(slug_chars)
135128
for i = 1:p
136-
x, d = divrem(x, nchars)
137-
write(io, chars[1+d])
129+
x, d = divrem(x, n)
130+
write(io, slug_chars[1+d])
138131
end
139132
end
140133
end
141-
slug(x::Integer, p::Int) = slug(SlugInt(x), p)
142134

143-
function slug(bytes::Vector{UInt8}, p::Int)
144-
n = nchars^p
145-
x = zero(SlugInt)
146-
for (i, b) in enumerate(bytes)
147-
x = (x + b*powermod(2, 8(i-1), n)) % n
148-
end
149-
slug(x, p)
135+
function package_slug(uuid::UUID, p::Int=4)
136+
crc = _crc32c(uuid)
137+
return slug(crc, p)
150138
end
151139

152-
slug(uuid::UUID, p::Int=4) = slug(uuid.value % nchars^p, p)
153-
slug(sha1::SHA1, p::Int=4) = slug(sha1.bytes, p)
154-
155-
version_slug(uuid::UUID, sha1::SHA1) = joinpath(slug(uuid), slug(sha1))
140+
function version_slug(uuid::UUID, sha1::SHA1, p::Int=4)
141+
crc = _crc32c(uuid)
142+
crc = _crc32c(sha1.bytes, crc)
143+
return slug(crc, p)
144+
end
156145

157146
## load path expansion: turn LOAD_PATH entries into concrete paths ##
158147

@@ -585,7 +574,7 @@ function explicit_manifest_uuid_path(manifest_file::String, pkg::PkgId)::Union{N
585574
return entry_path(path, name)
586575
end
587576
hash == nothing && return nothing
588-
slug = version_slug(uuid, hash)
577+
slug = joinpath(name, version_slug(uuid, hash))
589578
for depot in DEPOT_PATH
590579
path = abspath(depot, "packages", slug)
591580
ispath(path) && return path
@@ -638,10 +627,13 @@ function find_source_file(path::AbstractString)
638627
return isfile(base_path) ? base_path : nothing
639628
end
640629

630+
cache_file_entry(pkg::PkgId) =
631+
pkg.uuid === nothing ? "$(pkg.name).ji" :
632+
joinpath(pkg.name, "$(package_slug(pkg.uuid)).ji")
633+
641634
function find_all_in_cache_path(pkg::PkgId)
642635
paths = String[]
643-
suffix = "$(pkg.name).ji"
644-
pkg.uuid !== nothing && (suffix = joinpath(slug(pkg.uuid), suffix))
636+
suffix = cache_file_entry(pkg)
645637
for prefix in LOAD_CACHE_PATH
646638
path = joinpath(prefix, suffix)
647639
isfile_casesensitive(path) && push!(paths, path)
@@ -1179,12 +1171,9 @@ function compilecache(pkg::PkgId)
11791171
path = locate_package(pkg)
11801172
path === nothing && throw(ArgumentError("$name not found in path"))
11811173
# decide where to put the resulting cache file
1182-
cachepath = abspath(LOAD_CACHE_PATH[1])
1183-
pkg.uuid !== nothing && (cachepath = joinpath(cachepath, slug(pkg.uuid)))
1184-
if !isdir(cachepath)
1185-
mkpath(cachepath)
1186-
end
1187-
cachefile::String = joinpath(cachepath, "$(pkg.name).ji")
1174+
cachefile = abspath(LOAD_CACHE_PATH[1], cache_file_entry(pkg))
1175+
cachepath = dirname(cachefile)
1176+
isdir(cachepath) || mkpath(cachepath)
11881177
# build up the list of modules that we want the precompile process to preserve
11891178
concrete_deps = copy(_concrete_dependencies)
11901179
for (key, mod) in loaded_modules

base/sysimg.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,6 @@ using .StackTraces
426426
include("initdefs.jl")
427427
include("client.jl")
428428

429-
# misc useful functions & macros
430-
include("util.jl")
431-
432429
# statistics
433430
include("statistics.jl")
434431

@@ -442,6 +439,9 @@ include("threadcall.jl")
442439
include("uuid.jl")
443440
include("loading.jl")
444441

442+
# misc useful functions & macros
443+
include("util.jl")
444+
445445
# set up depot & load paths to be able to find stdlib packages
446446
let BINDIR = ccall(:jl_get_julia_bindir, Any, ())
447447
init_depot_path(BINDIR)

base/util.jl

+2-1
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,8 @@ function _crc32c(io::IO, nb::Integer, crc::UInt32=0x00000000)
561561
end
562562
_crc32c(io::IO, crc::UInt32=0x00000000) = _crc32c(io, typemax(Int64), crc)
563563
_crc32c(io::IOStream, crc::UInt32=0x00000000) = _crc32c(io, filesize(io)-position(io), crc)
564-
564+
_crc32c(uuid::UUID, crc::UInt32=0x00000000) =
565+
ccall(:jl_crc32c, UInt32, (UInt32, Ref{UInt128}, Csize_t), crc, uuid.value, 16)
565566

566567
"""
567568
@kwdef typedef

test/loading.jl

+5-5
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,11 @@ push!(empty!(DEPOT_PATH), "depot")
9494
@testset "project & manifest identify_package & locate_package" begin
9595
local path
9696
for (names, uuid, path) in [
97-
("Foo", "767738be-2f1f-45a9-b806-0234f3164144", "project/deps/Foo1/src/Foo.jl" ),
98-
("Bar.Foo", "6f418443-bd2e-4783-b551-cdbac608adf2", "project/deps/Foo2.jl/src/Foo.jl" ),
99-
("Bar", "2a550a13-6bab-4a91-a4ee-dff34d6b99d0", "project/deps/Bar/src/Bar.jl" ),
100-
("Foo.Baz", "6801f525-dc68-44e8-a4e8-cabd286279e7", "depot/packages/9HkB/TCSb/src/Baz.jl"),
101-
("Foo.Qux", "b5ec9b9c-e354-47fd-b367-a348bdc8f909", "project/deps/Qux.jl" ),
97+
("Foo", "767738be-2f1f-45a9-b806-0234f3164144", "project/deps/Foo1/src/Foo.jl" ),
98+
("Bar.Foo", "6f418443-bd2e-4783-b551-cdbac608adf2", "project/deps/Foo2.jl/src/Foo.jl" ),
99+
("Bar", "2a550a13-6bab-4a91-a4ee-dff34d6b99d0", "project/deps/Bar/src/Bar.jl" ),
100+
("Foo.Baz", "6801f525-dc68-44e8-a4e8-cabd286279e7", "depot/packages/Baz/81oL/src/Baz.jl"),
101+
("Foo.Qux", "b5ec9b9c-e354-47fd-b367-a348bdc8f909", "project/deps/Qux.jl" ),
102102
]
103103
n = map(String, split(names, '.'))
104104
pkg = identify_package(n...)

0 commit comments

Comments
 (0)