Skip to content

Commit 354df18

Browse files
committed
A few performance improvements
These fix a couple of places dominated by runtime dispatch.
1 parent 3932df2 commit 354df18

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

src/LoweredCodeUtils.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,7 @@ if ccall(:jl_generating_output, Cint, ()) == 1
4545
@assert precompile(Tuple{typeof(callchain), Vector{NamedTuple{(:linetop, :linebody, :callee, :caller),Tuple{Int64,Int64,Symbol,Union{Bool, Symbol}}}}})
4646

4747
@assert precompile(CodeEdges, (CodeInfo,))
48-
@assert precompile(add_links!, (Pair{JuliaInterpreter.SSAValue,Links}, Any, CodeLinks))
49-
@assert precompile(add_links!, (Pair{JuliaInterpreter.SlotNumber,Links}, Any, CodeLinks))
50-
@assert precompile(add_links!, (Pair{Symbol,Links}, Any, CodeLinks))
48+
@assert precompile(add_links!, (Pair{Union{SSAValue,SlotNumber,NamedVar},Links}, Any, CodeLinks))
5149
@assert precompile(lines_required!, (Vector{Bool}, Set{NamedVar}, CodeInfo, CodeEdges))
5250
end
5351

src/codeedges.jl

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,8 @@ function direct_links!(cl::CodeLinks, src::CodeInfo)
210210
end
211211
end
212212

213+
P = Pair{Union{SSAValue,SlotNumber,NamedVar},Links}
214+
213215
for (i, stmt) in enumerate(src.code)
214216
if isexpr(stmt, :thunk) && isa(stmt.args[1], CodeInfo)
215217
icl = CodeLinks(stmt.args[1])
@@ -231,26 +233,26 @@ function direct_links!(cl::CodeLinks, src::CodeInfo)
231233
if targetstore === nothing
232234
cl.namepreds[name] = targetstore = Links()
233235
end
234-
target = name=>targetstore
236+
target = P(name, targetstore)
235237
add_links!(target, stmt, cl)
236238
end
237239
rhs = stmt
238-
target = SSAValue(i)=>cl.ssapreds[i]
240+
target = P(SSAValue(i), cl.ssapreds[i])
239241
elseif isexpr(stmt, :(=))
240242
# An assignment
241243
stmt = stmt::Expr
242244
lhs, rhs = stmt.args[1], stmt.args[2]
243245
if isslotnum(lhs)
244246
lhs = lhs::AnySlotNumber
245247
id = lhs.id
246-
target = SlotNumber(id)=>cl.slotpreds[id]
248+
target = P(SlotNumber(id), cl.slotpreds[id])
247249
push!(cl.slotassigns[id], i)
248250
elseif isa(lhs, NamedVar)
249251
targetstore = get(cl.namepreds, lhs, nothing)
250252
if targetstore === nothing
251253
cl.namepreds[lhs] = targetstore = Links()
252254
end
253-
target = lhs=>targetstore
255+
target = P(lhs, targetstore)
254256
assign = get(cl.nameassigns, lhs, nothing)
255257
if assign === nothing
256258
cl.nameassigns[lhs] = assign = Int[]
@@ -261,30 +263,34 @@ function direct_links!(cl::CodeLinks, src::CodeInfo)
261263
end
262264
else
263265
rhs = stmt
264-
target = SSAValue(i)=>cl.ssapreds[i]
266+
target = P(SSAValue(i), cl.ssapreds[i])
265267
end
266268
add_links!(target, rhs, cl)
267269
end
268270
return cl
269271
end
270272

271-
function add_links!(target::Pair{<:Any,Links}, @nospecialize(stmt), cl::CodeLinks)
272-
targetid, targetstore = target
273+
function add_links!(target::Pair{Union{SSAValue,SlotNumber,NamedVar},Links}, @nospecialize(stmt), cl::CodeLinks)
274+
_targetid, targetstore = target
275+
targetid = _targetid::Union{SSAValue,SlotNumber,NamedVar}
273276
# Adds bidirectional edges
274277
if isssa(stmt)
275-
push!(targetstore, stmt) # forward edge
278+
stmt = stmt::AnySSAValue
279+
push!(targetstore, SSAValue(stmt.id)) # forward edge
276280
push!(cl.ssasuccs[stmt.id], targetid) # backward edge
277281
elseif isslotnum(stmt)
278-
push!(targetstore, stmt)
282+
stmt = stmt::AnySlotNumber
283+
push!(targetstore, SlotNumber(stmt.id))
279284
push!(cl.slotsuccs[stmt.id], targetid)
280-
elseif isa(stmt, NamedVar)
285+
elseif isa(stmt, Symbol) || isa(stmt, GlobalRef) # NamedVar
281286
push!(targetstore, stmt)
282287
namestore = get(cl.namesuccs, stmt, nothing)
283288
if namestore === nothing
284289
cl.namesuccs[stmt] = namestore = Links()
285290
end
286291
push!(namestore, targetid)
287292
elseif isa(stmt, Expr) && stmt.head !== :copyast
293+
stmt = stmt::Expr
288294
arng = 1:length(stmt.args)
289295
if stmt.head === :call
290296
f = stmt.args[1]
@@ -303,14 +309,14 @@ function add_links!(target::Pair{<:Any,Links}, @nospecialize(stmt), cl::CodeLink
303309
end
304310

305311
function Base.push!(l::Links, id)
306-
if isssa(id)
312+
if isa(id, SSAValue)
307313
k = id.id
308314
k l.ssas && push!(l.ssas, k)
309-
elseif isslotnum(id)
315+
elseif isa(id, SlotNumber)
310316
k = id.id
311317
k l.slots && push!(l.slots, k)
312318
else
313-
# NamedVar
319+
id = id::NamedVar
314320
id l.names && push!(l.names, id)
315321
end
316322
return id
@@ -393,17 +399,6 @@ end
393399
function CodeEdges(src::CodeInfo, cl::CodeLinks)
394400
# The main task here is to elide the slot-dependencies and convert
395401
# everything to just ssas & names.
396-
# Hence we "follow" slot links to their non-slot leaves.
397-
function follow_links!(marked, l::Links, slotlinks, slotassigns, slothandled)
398-
pushall!(marked, l.ssas)
399-
for id in l.slots
400-
slothandled[id] && continue
401-
slothandled[id] = true
402-
pushall!(marked, slotassigns[id])
403-
follow_links!(marked, slotlinks[id], slotlinks, slotassigns, slothandled)
404-
end
405-
return marked
406-
end
407402

408403
# Replace/add named intermediates (slot & named-variable references) with statement numbers
409404
nstmts, nslots = length(src.code), length(src.slotnames)
@@ -484,6 +479,18 @@ function CodeEdges(src::CodeInfo, cl::CodeLinks)
484479
return edges
485480
end
486481

482+
# Follow slot links to their non-slot leaves
483+
function follow_links!(marked, l::Links, slotlinks, slotassigns, slothandled)
484+
pushall!(marked, l.ssas)
485+
for id in l.slots
486+
slothandled[id] && continue
487+
slothandled[id] = true
488+
pushall!(marked, slotassigns[id])
489+
follow_links!(marked, slotlinks[id], slotlinks, slotassigns, slothandled)
490+
end
491+
return marked
492+
end
493+
487494
"""
488495
print_with_code(io, src::CodeInfo, edges::CodeEdges)
489496

0 commit comments

Comments
 (0)