Skip to content

Commit 106ff7e

Browse files
committed
Excise Random from the system image
1 parent 2defa57 commit 106ff7e

File tree

5 files changed

+57
-11
lines changed

5 files changed

+57
-11
lines changed

base/Base.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,29 @@ end
621621
# enable threads support
622622
@eval PCRE PCRE_COMPILE_LOCK = Threads.SpinLock()
623623

624+
# stdlib shims
625+
let Random_PkgID = PkgId(UUID(0x9a3f8284_a2c9_5f02_9a11_845980a1fd5c), "Random")
626+
global rand, randn
627+
628+
RANDOM_MODULE_REF = Ref{Module}()
629+
# functions defined in Random
630+
function rand(args...)
631+
if !isassigned(RANDOM_MODULE_REF)
632+
RANDOM_MODULE_REF[] = require(Random_PkgID)
633+
end
634+
invokelatest(rand, args...)
635+
end
636+
637+
function randn(args...)
638+
if !isassigned(RANDOM_MODULE_REF)
639+
RANDOM_MODULE_REF[] = require(Random_PkgID)
640+
end
641+
invokelatest(randn, args...)
642+
end
624643
end
625644

645+
end # is_primary_base_module
646+
626647
# Ensure this file is also tracked
627648
@assert !isassigned(_included_files, 1)
628649
_included_files[1] = (parentmodule(Base), abspath(@__FILE__))

base/sysimg.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ let
3434
:FileWatching, # used by loading.jl -- implicit assumption that init runs
3535
:Libdl, # Transitive through LinAlg
3636
:Artifacts, # Transitive through LinAlg
37-
:SHA, # transitive through Random
3837
:Sockets, # used by stream.jl
3938

4039
# Transitive through LingAlg
@@ -43,7 +42,6 @@ let
4342

4443
# 1-depth packages
4544
:LinearAlgebra, # Commits type-piracy and GEMM
46-
:Random, # Can't be removed due to rand being exported by Base
4745
]
4846
# PackageCompiler can filter out stdlibs so it can be empty
4947
maxlen = maximum(textwidth.(string.(stdlibs)); init=0)

stdlib/Random/src/RNGs.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -386,12 +386,6 @@ for T in BitInteger_types
386386
@eval rand!(::_GLOBAL_RNG, A::Array{$T}, I::SamplerType{$T}) = rand!(default_rng(), A, I)
387387
end
388388

389-
function __init__()
390-
seed!(GLOBAL_RNG)
391-
ccall(:jl_gc_init_finalizer_rng_state, Cvoid, ())
392-
end
393-
394-
395389
### generation
396390

397391
# MersenneTwister produces natively Float64

stdlib/Random/src/Random.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,39 @@ export rand!, randn!,
3131

3232
## general definitions
3333

34+
# Remove the shim methods
35+
# TODO: If we could support this as part of package loading
36+
# That would be much nicer.
37+
# for m in Base.methods(rand)
38+
# if m.module == Base
39+
# Base.delete_method(m)
40+
# end
41+
# end
42+
43+
function __init__()
44+
seed!(GLOBAL_RNG)
45+
ccall(:jl_gc_init_finalizer_rng_state, Cvoid, ())
46+
47+
# Remove the shim methods
48+
# Is this too coarse? We want to only delete the method
49+
# in Base, not any added by the user later on.
50+
# We are currently not allowed to run this during any precompilation
51+
# so we must guard it until "normal runtime".
52+
# TODO: What to do when we are including Random into a sysimg.
53+
if !Base.generating_output()
54+
for m in Base.methods(rand)
55+
if m.module == Base
56+
Base.delete_method(m)
57+
end
58+
end
59+
for m in Base.methods(randn)
60+
if m.module == Base
61+
Base.delete_method(m)
62+
end
63+
end
64+
end
65+
end
66+
3467
"""
3568
AbstractRNG
3669

stdlib/stdlib.mk

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
STDLIBS_WITHIN_SYSIMG := \
2-
Artifacts FileWatching Libdl SHA libblastrampoline_jll OpenBLAS_jll Random \
2+
Artifacts FileWatching Libdl libblastrampoline_jll OpenBLAS_jll \
33
LinearAlgebra Sockets
44

55
INDEPENDENT_STDLIBS := \
66
ArgTools Base64 CRC32c Dates DelimitedFiles Distributed Downloads Future \
77
InteractiveUtils LazyArtifacts LibGit2 LibCURL Logging Markdown Mmap \
8-
NetworkOptions Profile Printf Pkg REPL Serialization SharedArrays SparseArrays \
9-
Statistics Tar Test TOML Unicode UUIDs \
8+
NetworkOptions Profile Printf Pkg Random REPL Serialization SharedArrays \
9+
SparseArrays Statistics SHA Tar Test TOML Unicode UUIDs \
1010
dSFMT_jll GMP_jll libLLVM_jll LLD_jll LLVMLibUnwind_jll LibUnwind_jll LibUV_jll \
1111
LibCURL_jll LibSSH2_jll LibGit2_jll nghttp2_jll MozillaCACerts_jll MbedTLS_jll \
1212
MPFR_jll OpenLibm_jll PCRE2_jll p7zip_jll Zlib_jll

0 commit comments

Comments
 (0)