Skip to content

Commit

Permalink
Excise Random from the system image
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Oct 9, 2023
1 parent f90fd72 commit 446a6f7
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 6 deletions.
5 changes: 4 additions & 1 deletion base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,9 @@ include("util.jl")
include("client.jl")
include("asyncmap.jl")

# excised stdlib stubs
include("stubs.jl")

# deprecated functions
include("deprecated.jl")
#
Expand Down Expand Up @@ -621,7 +624,7 @@ end
# enable threads support
@eval PCRE PCRE_COMPILE_LOCK = Threads.SpinLock()

end
end # is_primary_base_module

# Ensure this file is also tracked
@assert !isassigned(_included_files, 1)
Expand Down
44 changes: 44 additions & 0 deletions base/stubs.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module Stubs

module Random
let Random_PkgID = Base.PkgId(Base.UUID(0x9a3f8284_a2c9_5f02_9a11_845980a1fd5c), "Random")
RANDOM_MODULE_REF = Ref{Module}()

global delay_initialize
function delay_initialize()
if !isassigned(RANDOM_MODULE_REF)
RANDOM_MODULE_REF[] = Base.require(Random_PkgID)
end
return ccall(:jl_module_world, Csize_t, (Any,), RANDOM_MODULE_REF[])
end
end

import Base: rand, randn
function rand(args...)
Base.invoke_in_world(delay_initialize(), rand, args...)
end

function randn(args...)
Base.invoke_in_world(delay_initialize(), randn, args...)
end
end

Base.Docs.getdoc(::typeof(Base.rand)) = (Random.delay_initialize(); nothing)
Base.Docs.getdoc(::typeof(Base.randn)) = (Random.delay_initialize(); nothing)

function delete_stubs(mod)
for name in names(mod, imported=true)
if name == :delay_initialize
continue
end
obj = getglobal(mod, name)
if obj isa Function
ms = Base.methods(obj, mod)
for m in ms
Base.delete_method(m)
end
end
end
end

end
2 changes: 0 additions & 2 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ let
:FileWatching, # used by loading.jl -- implicit assumption that init runs
:Libdl, # Transitive through LinAlg
:Artifacts, # Transitive through LinAlg
:SHA, # transitive through Random
:Sockets, # used by stream.jl

# Transitive through LingAlg
Expand All @@ -43,7 +42,6 @@ let

# 1-depth packages
:LinearAlgebra, # Commits type-piracy and GEMM
:Random, # Can't be removed due to rand being exported by Base
]
# PackageCompiler can filter out stdlibs so it can be empty
maxlen = maximum(textwidth.(string.(stdlibs)); init=0)
Expand Down
1 change: 1 addition & 0 deletions src/jl_exported_funcs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@
XX(jl_new_method_table) \
XX(jl_new_method_uninit) \
XX(jl_new_module) \
XX(jl_module_world) \
XX(jl_new_primitivetype) \
XX(jl_new_struct) \
XX(jl_new_structt) \
Expand Down
1 change: 1 addition & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -1737,6 +1737,7 @@ extern JL_DLLIMPORT jl_module_t *jl_base_module JL_GLOBALLY_ROOTED;
extern JL_DLLIMPORT jl_module_t *jl_top_module JL_GLOBALLY_ROOTED;
extern JL_DLLIMPORT jl_module_t *jl_libdl_module JL_GLOBALLY_ROOTED;
JL_DLLEXPORT jl_module_t *jl_new_module(jl_sym_t *name, jl_module_t *parent);
JL_DLLEXPORT size_t jl_module_world(jl_module_t *m);
JL_DLLEXPORT void jl_set_module_nospecialize(jl_module_t *self, int on);
JL_DLLEXPORT void jl_set_module_optlevel(jl_module_t *self, int lvl);
JL_DLLEXPORT int jl_get_module_optlevel(jl_module_t *m);
Expand Down
5 changes: 5 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ uint32_t jl_module_next_counter(jl_module_t *m)
return jl_atomic_fetch_add(&m->counter, 1);
}

JL_DLLEXPORT size_t jl_module_world(jl_module_t *m)
{
return m->primary_world;
}

JL_DLLEXPORT jl_value_t *jl_f_new_module(jl_sym_t *name, uint8_t std_imports, uint8_t default_names)
{
// TODO: should we prohibit this during incremental compilation?
Expand Down
1 change: 1 addition & 0 deletions stdlib/Random/Project.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name = "Random"
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
version = "1.11.0"

[deps]
SHA = "ea8e919c-243c-51af-8825-aaa63cd721ce"
Expand Down
9 changes: 9 additions & 0 deletions stdlib/Random/src/Random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ export rand!, randn!,

## general definitions

module Stubs
function __init__()
# Remove the shim methods
if !Base.generating_output()
Base.Stubs.delete_stubs(Base.Stubs.Random)
end
end
end

"""
AbstractRNG
Expand Down
6 changes: 3 additions & 3 deletions stdlib/stdlib.mk
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
STDLIBS_WITHIN_SYSIMG := \
Artifacts FileWatching Libdl SHA libblastrampoline_jll OpenBLAS_jll Random \
Artifacts FileWatching Libdl libblastrampoline_jll OpenBLAS_jll \
LinearAlgebra Sockets

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

0 comments on commit 446a6f7

Please sign in to comment.