Skip to content

Commit

Permalink
clean up some global variables in Base (#36555)
Browse files Browse the repository at this point in the history
- add some `const` decls
- move UV callbacks inline instead of using globals
- remove `Base.tot_time_stdlib`
  • Loading branch information
JeffBezanson committed Jul 8, 2020
1 parent b19a357 commit 91bb524
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 67 deletions.
3 changes: 0 additions & 3 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,6 @@ function __init__()
nothing
end


end

const tot_time_stdlib = RefValue(0.0)

end # baremodule Base
5 changes: 3 additions & 2 deletions base/asyncevent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mutable struct AsyncCondition
iolock_begin()
associate_julia_struct(this.handle, this)
err = ccall(:uv_async_init, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
eventloop(), this, uv_jl_asynccb::Ptr{Cvoid})
eventloop(), this, @cfunction(uv_asynccb, Cvoid, (Ptr{Cvoid},)))
if err != 0
#TODO: this codepath is currently not tested
Libc.free(this.handle)
Expand Down Expand Up @@ -83,7 +83,8 @@ mutable struct Timer
finalizer(uvfinalize, this)
ccall(:uv_update_time, Cvoid, (Ptr{Cvoid},), loop)
err = ccall(:uv_timer_start, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, UInt64, UInt64),
this, uv_jl_timercb::Ptr{Cvoid}, timeout, interval)
this, @cfunction(uv_timercb, Cvoid, (Ptr{Cvoid},)),
timeout, interval)
@assert err == 0
iolock_end()
return this
Expand Down
14 changes: 7 additions & 7 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
## and REPL

have_color = nothing
default_color_warn = :yellow
default_color_error = :light_red
default_color_info = :cyan
default_color_debug = :blue
default_color_input = :normal
default_color_answer = :normal
color_normal = text_colors[:normal]
const default_color_warn = :yellow
const default_color_error = :light_red
const default_color_info = :cyan
const default_color_debug = :blue
const default_color_input = :normal
const default_color_answer = :normal
const color_normal = text_colors[:normal]

function repl_color(key, default)
env_str = get(ENV, key, "")
Expand Down
7 changes: 0 additions & 7 deletions base/libuv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,6 @@ function uv_asynccb end
function uv_timercb end

function reinit_stdio()
global uv_jl_alloc_buf = @cfunction(uv_alloc_buf, Cvoid, (Ptr{Cvoid}, Csize_t, Ptr{Cvoid}))
global uv_jl_readcb = @cfunction(uv_readcb, Cvoid, (Ptr{Cvoid}, Cssize_t, Ptr{Cvoid}))
global uv_jl_writecb_task = @cfunction(uv_writecb_task, Cvoid, (Ptr{Cvoid}, Cint))
global uv_jl_return_spawn = @cfunction(uv_return_spawn, Cvoid, (Ptr{Cvoid}, Int64, Int32))
global uv_jl_asynccb = @cfunction(uv_asynccb, Cvoid, (Ptr{Cvoid},))
global uv_jl_timercb = @cfunction(uv_timercb, Cvoid, (Ptr{Cvoid},))

global stdin = init_stdio(ccall(:jl_stdin_stream, Ptr{Cvoid}, ()))
global stdout = init_stdio(ccall(:jl_stdout_stream, Ptr{Cvoid}, ()))
global stderr = init_stdio(ccall(:jl_stderr_stream, Ptr{Cvoid}, ()))
Expand Down
2 changes: 1 addition & 1 deletion base/process.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const SpawnIOs = Vector{Any} # convenience name for readability
cmd.flags,
cmd.env === nothing ? C_NULL : cmd.env,
isempty(cmd.dir) ? C_NULL : cmd.dir,
uv_jl_return_spawn::Ptr{Cvoid})
@cfunction(uv_return_spawn, Cvoid, (Ptr{Cvoid}, Int64, Int32)))
if err != 0
ccall(:jl_forceclose_uv, Cvoid, (Ptr{Cvoid},), handle) # will call free on handle eventually
throw(_UVError("could not spawn " * repr(cmd), err))
Expand Down
5 changes: 3 additions & 2 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,8 @@ function start_reading(stream::LibuvStream)
# for a TTY on Windows, so ensure the status is set first
stream.status = StatusActive
ret = ccall(:uv_read_start, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
stream, uv_jl_alloc_buf::Ptr{Cvoid}, uv_jl_readcb::Ptr{Cvoid})
stream, @cfunction(uv_alloc_buf, Cvoid, (Ptr{Cvoid}, Csize_t, Ptr{Cvoid})),
@cfunction(uv_readcb, Cvoid, (Ptr{Cvoid}, Cssize_t, Ptr{Cvoid})))
elseif stream.status == StatusPaused
stream.status = StatusActive
ret = Int32(0)
Expand Down Expand Up @@ -1024,7 +1025,7 @@ function uv_write_async(s::LibuvStream, p::Ptr{UInt8}, n::UInt)
Int32,
(Ptr{Cvoid}, Ptr{Cvoid}, UInt, Ptr{Cvoid}, Ptr{Cvoid}),
s, p, nwrite, uvw,
uv_jl_writecb_task::Ptr{Cvoid})
@cfunction(uv_writecb_task, Cvoid, (Ptr{Cvoid}, Cint)))
if err < 0
Libc.free(uvw)
uv_error("write", err)
Expand Down
41 changes: 20 additions & 21 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Core.include(Main, "Base.jl")

using .Base

# Set up Main module
import Base.MainInclude: eval, include

# Ensure this file is also tracked
pushfirst!(Base._included_files, (@__MODULE__, joinpath(@__DIR__, "Base.jl")))
pushfirst!(Base._included_files, (@__MODULE__, joinpath(@__DIR__, "sysimg.jl")))
Expand Down Expand Up @@ -51,6 +54,7 @@ let

maxlen = reduce(max, textwidth.(string.(stdlibs)); init=0)

tot_time_stdlib = 0.0
# use a temp module to avoid leaving the type of this closure in Main
m = Module()
GC.@preserve m begin
Expand All @@ -59,7 +63,7 @@ let
print_time(Base, (Base.end_base_include - Base.start_base_include) * 10^(-9))

Base._track_dependencies[] = true
Base.tot_time_stdlib[] = @elapsed for stdlib in stdlibs
tot_time_stdlib = @elapsed for stdlib in stdlibs
tt = @elapsed Base.require(Base, stdlib)
print_time(stdlib, tt)
end
Expand All @@ -70,36 +74,31 @@ let
empty!(Base._require_dependencies)
Base._track_dependencies[] = false

print_time("Stdlibs total", Base.tot_time_stdlib[])
print_time("Stdlibs total", tot_time_stdlib)
end
end
end

# Clear global state
empty!(Core.ARGS)
empty!(Base.ARGS)
empty!(LOAD_PATH)
@eval Base creating_sysimg = false
Base.init_load_path() # want to be able to find external packages in userimg.jl

# Set up Main module
import Base.MainInclude: eval, include
# Clear global state
empty!(Core.ARGS)
empty!(Base.ARGS)
empty!(LOAD_PATH)
@eval Base creating_sysimg = false
Base.init_load_path() # want to be able to find external packages in userimg.jl

Base.@eval Base let
ccall(:jl_clear_implicit_imports, Cvoid, (Any,), Main)
tot_time_userimg = @elapsed (isfile("userimg.jl") && include(Main, "userimg.jl"))
tot_time_userimg = @elapsed (isfile("userimg.jl") && Base.include(Main, "userimg.jl"))

tot_time_base = (end_base_include - start_base_include) * 10.0^(-9)
tot_time = tot_time_base + tot_time_stdlib[] + tot_time_userimg
tot_time_base = (Base.end_base_include - Base.start_base_include) * 10.0^(-9)
tot_time = tot_time_base + tot_time_stdlib + tot_time_userimg

println("Sysimage built. Summary:")
print("Total ─────── "); time_print(tot_time * 10^9); print(" \n");
print("Base: ─────── "); time_print(tot_time_base * 10^9); print(" "); show(IOContext(stdout, :compact=>true), (tot_time_base / tot_time) * 100); println("%")
print("Stdlibs: ──── "); time_print(tot_time_stdlib[] * 10^9); print(" "); show(IOContext(stdout, :compact=>true), (tot_time_stdlib[] / tot_time) * 100); println("%")
print("Total ─────── "); Base.time_print(tot_time * 10^9); print(" \n");
print("Base: ─────── "); Base.time_print(tot_time_base * 10^9); print(" "); show(IOContext(stdout, :compact=>true), (tot_time_base / tot_time) * 100); println("%")
print("Stdlibs: ──── "); Base.time_print(tot_time_stdlib * 10^9); print(" "); show(IOContext(stdout, :compact=>true), (tot_time_stdlib / tot_time) * 100); println("%")
if isfile("userimg.jl")
print("Userimg: ──── "); time_print(tot_time_userimg * 10^9); print(" "); show(IOContext(stdout, :compact=>true), (tot_time_userimg / tot_time) * 100); println("%")
print("Userimg: ──── "); Base.time_print(tot_time_userimg * 10^9); print(" "); show(IOContext(stdout, :compact=>true), (tot_time_userimg / tot_time) * 100); println("%")
end

empty!(LOAD_PATH)
empty!(DEPOT_PATH)
end
end
13 changes: 7 additions & 6 deletions base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,13 @@ const disable_text_style = Dict{Symbol,String}(

# Create a docstring with an automatically generated list
# of colors.
available_text_colors = collect(Iterators.filter(x -> !isa(x, Integer), keys(text_colors)))
const possible_formatting_symbols = [:normal, :bold, :default]
available_text_colors = cat(
sort!(intersect(available_text_colors, possible_formatting_symbols), rev=true),
sort!(setdiff( available_text_colors, possible_formatting_symbols));
dims=1)
let color_syms = collect(Iterators.filter(x -> !isa(x, Integer), keys(text_colors))),
formatting_syms = [:normal, :bold, :default]
global const available_text_colors = cat(
sort!(intersect(color_syms, formatting_syms), rev=true),
sort!(setdiff( color_syms, formatting_syms));
dims=1)
end

const available_text_colors_docstring =
string(join([string("`:", key,"`")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Sockets/src/PipeServer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function connect!(sock::PipeEndpoint, path::AbstractString)
req = Libc.malloc(Base._sizeof_uv_connect)
uv_req_set_data(req, C_NULL)
ccall(:uv_pipe_connect, Cvoid, (Ptr{Cvoid}, Ptr{Cvoid}, Cstring, Ptr{Cvoid}), req, sock.handle, path,
uv_jl_connectcb::Ptr{Cvoid})
@cfunction(uv_connectcb, Cvoid, (Ptr{Cvoid}, Cint)))
sock.status = StatusConnecting
iolock_end()
return sock
Expand Down
25 changes: 11 additions & 14 deletions stdlib/Sockets/src/Sockets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ function recv(sock::UDPSocket)
return data
end

function uv_recvcb end

"""
recvfrom(socket::UDPSocket) -> (host_port, data)
Expand All @@ -347,7 +349,9 @@ function recvfrom(sock::UDPSocket)
end
if ccall(:uv_is_active, Cint, (Ptr{Cvoid},), sock.handle) == 0
err = ccall(:uv_udp_recv_start, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, Ptr{Cvoid}),
sock, Base.uv_jl_alloc_buf::Ptr{Cvoid}, uv_jl_recvcb::Ptr{Cvoid})
sock,
@cfunction(Base.uv_alloc_buf, Cvoid, (Ptr{Cvoid}, Csize_t, Ptr{Cvoid})),
@cfunction(uv_recvcb, Cvoid, (Ptr{Cvoid}, Cssize_t, Ptr{Cvoid}, Ptr{Cvoid}, Cuint)))
uv_error("recv_start", err)
end
sock.status = StatusActive
Expand Down Expand Up @@ -417,7 +421,9 @@ function _send_async(sock::UDPSocket, ipaddr::Union{IPv4, IPv6}, port::UInt16, b
uv_req_set_data(req, C_NULL) # in case we get interrupted before arriving at the wait call
host_in = Ref(hton(ipaddr.host))
err = ccall(:jl_udp_send, Cint, (Ptr{Cvoid}, Ptr{Cvoid}, UInt16, Ptr{Cvoid}, Ptr{UInt8}, Csize_t, Ptr{Cvoid}, Cint),
req, sock, hton(port), host_in, buf, sizeof(buf), Base.uv_jl_writecb_task::Ptr{Cvoid}, ipaddr isa IPv6)
req, sock, hton(port), host_in, buf, sizeof(buf),
@cfunction(Base.uv_writecb_task, Cvoid, (Ptr{Cvoid}, Cint)),
ipaddr isa IPv6)
if err < 0
Libc.free(req)
uv_error("send", err)
Expand Down Expand Up @@ -500,7 +506,8 @@ function connect!(sock::TCPSocket, host::Union{IPv4, IPv6}, port::Integer)
end
host_in = Ref(hton(host.host))
uv_error("connect", ccall(:jl_tcp_connect, Int32, (Ptr{Cvoid}, Ptr{Cvoid}, UInt16, Ptr{Cvoid}, Cint),
sock, host_in, hton(UInt16(port)), uv_jl_connectcb::Ptr{Cvoid}, host isa IPv6))
sock, host_in, hton(UInt16(port)), @cfunction(uv_connectcb, Cvoid, (Ptr{Cvoid}, Cint)),
host isa IPv6))
sock.status = StatusConnecting
iolock_end()
nothing
Expand Down Expand Up @@ -643,7 +650,7 @@ function trylisten(sock::LibuvServer; backlog::Integer=BACKLOG_DEFAULT)
iolock_begin()
check_open(sock)
err = ccall(:uv_listen, Cint, (Ptr{Cvoid}, Cint, Ptr{Cvoid}),
sock, backlog, uv_jl_connectioncb::Ptr{Cvoid})
sock, backlog, @cfunction(uv_connectioncb, Cvoid, (Ptr{Cvoid}, Cint)))
sock.status = StatusActive
iolock_end()
return err
Expand Down Expand Up @@ -842,14 +849,4 @@ end

include("PipeServer.jl")

# libuv callback handles

function __init__()
global uv_jl_getaddrinfocb = @cfunction(uv_getaddrinfocb, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Cvoid}))
global uv_jl_getnameinfocb = @cfunction(uv_getnameinfocb, Cvoid, (Ptr{Cvoid}, Cint, Cstring, Cstring))
global uv_jl_recvcb = @cfunction(uv_recvcb, Cvoid, (Ptr{Cvoid}, Cssize_t, Ptr{Cvoid}, Ptr{Cvoid}, Cuint))
global uv_jl_connectioncb = @cfunction(uv_connectioncb, Cvoid, (Ptr{Cvoid}, Cint))
global uv_jl_connectcb = @cfunction(uv_connectcb, Cvoid, (Ptr{Cvoid}, Cint))
end

end
5 changes: 3 additions & 2 deletions stdlib/Sockets/src/addrinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ function getalladdrinfo(host::String)
uv_req_set_data(req, C_NULL) # in case we get interrupted before arriving at the wait call
iolock_begin()
status = ccall(:jl_getaddrinfo, Int32, (Ptr{Cvoid}, Ptr{Cvoid}, Cstring, Ptr{Cvoid}, Ptr{Cvoid}),
eventloop(), req, host, #=service=#C_NULL, uv_jl_getaddrinfocb::Ptr{Cvoid})
eventloop(), req, host, #=service=#C_NULL,
@cfunction(uv_getaddrinfocb, Cvoid, (Ptr{Cvoid}, Cint, Ptr{Cvoid})))
if status < 0
Libc.free(req)
if status == UV_EINVAL
Expand Down Expand Up @@ -178,7 +179,7 @@ function getnameinfo(address::Union{IPv4, IPv6})
uv_req_set_data(req, C_NULL) # in case we get interrupted before arriving at the wait call
port = hton(UInt16(0))
flags = 0
uvcb = uv_jl_getnameinfocb::Ptr{Cvoid}
uvcb = @cfunction(uv_getnameinfocb, Cvoid, (Ptr{Cvoid}, Cint, Cstring, Cstring))
status = UV_EINVAL
host_in = Ref(hton(address.host))
iolock_begin()
Expand Down
1 change: 0 additions & 1 deletion test/testhelpers/FakePTYs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ module FakePTYs
if Sys.iswindows()
pushfirst!(LOAD_PATH, Sys.STDLIB)
using Sockets
Sockets.__init__()
popfirst!(LOAD_PATH)
end

Expand Down

0 comments on commit 91bb524

Please sign in to comment.