Skip to content

Commit fcca55c

Browse files
committed
Excise Random from the system image
1 parent fee7551 commit fcca55c

File tree

9 files changed

+78
-12
lines changed

9 files changed

+78
-12
lines changed

base/Base.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ include("util.jl")
470470
include("client.jl")
471471
include("asyncmap.jl")
472472

473+
# excised stdlib stubs
474+
include("stubs.jl")
475+
473476
# deprecated functions
474477
include("deprecated.jl")
475478
#
@@ -621,7 +624,7 @@ end
621624
# enable threads support
622625
@eval PCRE PCRE_COMPILE_LOCK = Threads.SpinLock()
623626

624-
end
627+
end # is_primary_base_module
625628

626629
# Ensure this file is also tracked
627630
@assert !isassigned(_included_files, 1)

base/stubs.jl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
module Stubs
2+
3+
module Random
4+
let Random_PkgID = Base.PkgId(Base.UUID(0x9a3f8284_a2c9_5f02_9a11_845980a1fd5c), "Random")
5+
RANDOM_MODULE_REF = Ref{Module}()
6+
7+
global delay_initialize
8+
function delay_initialize()
9+
if !isassigned(RANDOM_MODULE_REF)
10+
RANDOM_MODULE_REF[] = require(Random_PkgID)
11+
end
12+
return ccall(:jl_module_world, Csize_t, (Any,), RANDOM_MODULE_REF[])
13+
end
14+
end
15+
16+
function Base.rand(args...)
17+
Base.invoke_in_world(delay_initialize(), rand, args...)
18+
end
19+
20+
function Base.randn(args...)
21+
Base.invoke_in_world(delay_initialize(), rand, args...)
22+
end
23+
end
24+
25+
function delete_stubs(mod)
26+
for name in names(mod)
27+
if name == :delay_initialize
28+
continue
29+
end
30+
obj = getglobal(Base.Stubs.Randomod, name)
31+
if obj isa Function
32+
ms = Base.methods(obj, mod)
33+
for m in ms
34+
Base.delete_method(m)
35+
end
36+
end
37+
end
38+
end
39+
40+
end

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)

src/jl_exported_funcs.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@
353353
XX(jl_new_method_table) \
354354
XX(jl_new_method_uninit) \
355355
XX(jl_new_module) \
356+
XX(jl_module_world) \
356357
XX(jl_new_primitivetype) \
357358
XX(jl_new_struct) \
358359
XX(jl_new_structt) \

src/julia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,7 @@ extern JL_DLLIMPORT jl_module_t *jl_base_module JL_GLOBALLY_ROOTED;
17371737
extern JL_DLLIMPORT jl_module_t *jl_top_module JL_GLOBALLY_ROOTED;
17381738
extern JL_DLLIMPORT jl_module_t *jl_libdl_module JL_GLOBALLY_ROOTED;
17391739
JL_DLLEXPORT jl_module_t *jl_new_module(jl_sym_t *name, jl_module_t *parent);
1740+
JL_DLLEXPORT size_t jl_module_world(jl_module_t *m);
17401741
JL_DLLEXPORT void jl_set_module_nospecialize(jl_module_t *self, int on);
17411742
JL_DLLEXPORT void jl_set_module_optlevel(jl_module_t *self, int lvl);
17421743
JL_DLLEXPORT int jl_get_module_optlevel(jl_module_t *m);

src/module.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ uint32_t jl_module_next_counter(jl_module_t *m)
6464
return jl_atomic_fetch_add(&m->counter, 1);
6565
}
6666

67+
JL_DLLEXPORT size_t jl_module_world(jl_module_t *m)
68+
{
69+
return m->primary_world;
70+
}
71+
6772
JL_DLLEXPORT jl_value_t *jl_f_new_module(jl_sym_t *name, uint8_t std_imports, uint8_t default_names)
6873
{
6974
// TODO: should we prohibit this during incremental compilation?

stdlib/Random/src/RNGs.jl

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,6 @@ function seed!(seed=nothing)
402402
seed!(default_rng(), seed)
403403
end
404404

405-
function __init__()
406-
seed!()
407-
ccall(:jl_gc_init_finalizer_rng_state, Cvoid, ())
408-
end
409-
410-
411405
### generation
412406

413407
# MersenneTwister produces natively Float64

stdlib/Random/src/Random.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,30 @@ 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!()
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+
Base.Stubs.delete_stubs(Base.Stubs.Random)
55+
end
56+
end
57+
3458
"""
3559
AbstractRNG
3660

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)