Skip to content

Commit 810da4c

Browse files
authored
reduce amount of compilation in the repl due to showing new types (#38028)
1 parent 46834a2 commit 810da4c

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

stdlib/REPL/src/REPL.jl

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ const JULIA_PROMPT = "julia> "
7979

8080
mutable struct REPLBackend
8181
"channel for AST"
82-
repl_channel::Channel
82+
repl_channel::Channel{Any}
8383
"channel for results: (value, iserror)"
84-
response_channel::Channel
84+
response_channel::Channel{Any}
8585
"flag indicating the state of this backend"
8686
in_eval::Bool
8787
"transformation functions to apply before evaluating expressions"
@@ -130,7 +130,7 @@ function eval_user_input(@nospecialize(ast), backend::REPLBackend)
130130
try
131131
Base.sigatomic_end()
132132
if lasterr !== nothing
133-
put!(backend.response_channel, (lasterr,true))
133+
put!(backend.response_channel, Pair{Any, Bool}(lasterr, true))
134134
else
135135
backend.in_eval = true
136136
for xf in backend.ast_transforms
@@ -140,7 +140,7 @@ function eval_user_input(@nospecialize(ast), backend::REPLBackend)
140140
backend.in_eval = false
141141
# note: use jl_set_global to make sure value isn't passed through `expand`
142142
ccall(:jl_set_global, Cvoid, (Any, Any, Any), Main, :ans, value)
143-
put!(backend.response_channel, (value,false))
143+
put!(backend.response_channel, Pair{Any, Bool}(value, false))
144144
end
145145
break
146146
catch err
@@ -156,14 +156,14 @@ function eval_user_input(@nospecialize(ast), backend::REPLBackend)
156156
end
157157

158158
"""
159-
start_repl_backend(repl_channel::Channel,response_channel::Channel)
159+
start_repl_backend(repl_channel::Channel, response_channel::Channel)
160160
161161
Starts loop for REPL backend
162162
Returns a REPLBackend with backend_task assigned
163163
164164
Deprecated since sync / async behavior cannot be selected
165165
"""
166-
function start_repl_backend(repl_channel::Channel, response_channel::Channel)
166+
function start_repl_backend(repl_channel::Channel{Any}, response_channel::Channel{Any})
167167
# Maintain legacy behavior of asynchronous backend
168168
backend = REPLBackend(repl_channel, response_channel, false)
169169
# Assignment will be made twice, but will be immediately available
@@ -209,29 +209,30 @@ end
209209
==(a::REPLDisplay, b::REPLDisplay) = a.repl === b.repl
210210

211211
function display(d::REPLDisplay, mime::MIME"text/plain", x)
212+
x = Ref{Any}(x)
212213
with_repl_linfo(d.repl) do io
213214
io = IOContext(io, :limit => true, :module => Main::Module)
214215
get(io, :color, false) && write(io, answer_color(d.repl))
215216
if isdefined(d.repl, :options) && isdefined(d.repl.options, :iocontext)
216217
# this can override the :limit property set initially
217218
io = foldl(IOContext, d.repl.options.iocontext, init=io)
218219
end
219-
show(io, mime, x)
220+
show(io, mime, x[])
220221
println(io)
221222
end
222223
return nothing
223224
end
224225
display(d::REPLDisplay, x) = display(d, MIME("text/plain"), x)
225226

226-
function print_response(repl::AbstractREPL, @nospecialize(response), show_value::Bool, have_color::Bool)
227+
function print_response(repl::AbstractREPL, response, show_value::Bool, have_color::Bool)
227228
repl.waserror = response[2]
228229
with_repl_linfo(repl) do io
229230
io = IOContext(io, :module => Main::Module)
230231
print_response(io, response, show_value, have_color, specialdisplay(repl))
231232
end
232233
return nothing
233234
end
234-
function print_response(errio::IO, @nospecialize(response), show_value::Bool, have_color::Bool, specialdisplay::Union{AbstractDisplay,Nothing}=nothing)
235+
function print_response(errio::IO, response, show_value::Bool, have_color::Bool, specialdisplay::Union{AbstractDisplay,Nothing}=nothing)
235236
Base.sigatomic_begin()
236237
val, iserr = response
237238
while true
@@ -279,8 +280,8 @@ end
279280

280281
# A reference to a backend that is not mutable
281282
struct REPLBackendRef
282-
repl_channel::Channel
283-
response_channel::Channel
283+
repl_channel::Channel{Any}
284+
response_channel::Channel{Any}
284285
end
285286
REPLBackendRef(backend::REPLBackend) = REPLBackendRef(backend.repl_channel, backend.response_channel)
286287
function destroy(ref::REPLBackendRef, state::Task)
@@ -791,7 +792,7 @@ function respond(f, repl, main; pass_empty::Bool = false, suppress_on_semicolon:
791792
ast = Base.invokelatest(f, line)
792793
response = eval_with_backend(ast, backend(repl))
793794
catch
794-
response = (catch_stack(), true)
795+
response = Pair{Any, Bool}(catch_stack(), true)
795796
end
796797
hide_output = suppress_on_semicolon && ends_with_semicolon(line)
797798
print_response(repl, response, !hide_output, hascolor(repl))
@@ -942,7 +943,7 @@ function setup_interface(
942943
hist_from_file(hp, hist_path)
943944
catch
944945
# use REPL.hascolor to avoid using the local variable with the same name
945-
print_response(repl, (catch_stack(),true), true, REPL.hascolor(repl))
946+
print_response(repl, Pair{Any, Bool}(catch_stack(), true), true, REPL.hascolor(repl))
946947
println(outstream(repl))
947948
@info "Disabling history file for this session"
948949
repl.history_file = false

stdlib/REPL/test/repl.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,12 +1219,12 @@ end
12191219
@async REPL.start_repl_backend(backend)
12201220
put!(backend.repl_channel, (:(1+1), false))
12211221
reply = take!(backend.response_channel)
1222-
@test reply == (2, false)
1222+
@test reply == Pair{Any, Bool}(2, false)
12231223
twice(ex) = Expr(:tuple, ex, ex)
12241224
push!(backend.ast_transforms, twice)
12251225
put!(backend.repl_channel, (:(1+1), false))
12261226
reply = take!(backend.response_channel)
1227-
@test reply == ((2, 2), false)
1227+
@test reply == Pair{Any, Bool}((2, 2), false)
12281228
put!(backend.repl_channel, (nothing, -1))
12291229
Base.wait(backend.backend_task)
12301230
end
@@ -1236,12 +1236,12 @@ frontend_task = @async begin
12361236
@testset "AST Transformations Async" begin
12371237
put!(backend.repl_channel, (:(1+1), false))
12381238
reply = take!(backend.response_channel)
1239-
@test reply == (2, false)
1239+
@test reply == Pair{Any, Bool}(2, false)
12401240
twice(ex) = Expr(:tuple, ex, ex)
12411241
push!(backend.ast_transforms, twice)
12421242
put!(backend.repl_channel, (:(1+1), false))
12431243
reply = take!(backend.response_channel)
1244-
@test reply == ((2, 2), false)
1244+
@test reply == Pair{Any, Bool}((2, 2), false)
12451245
end
12461246
catch e
12471247
Base.rethrow(e)

0 commit comments

Comments
 (0)