Skip to content

Commit e6fa3ec

Browse files
Profile: multithread getdict! (redo of #43805) (#43816)
1 parent cf8338a commit e6fa3ec

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

stdlib/Profile/src/Profile.jl

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,19 +349,34 @@ function getdict(data::Vector{UInt})
349349
end
350350

351351
function getdict!(dict::LineInfoDict, data::Vector{UInt})
352-
for ip in data
353-
# Lookup is expensive, so do it only once per ip.
354-
haskey(dict, UInt64(ip)) && continue
355-
st = lookup(convert(Ptr{Cvoid}, ip))
356-
# To correct line numbers for moving code, put it in the form expected by
357-
# Base.update_stackframes_callback[]
358-
stn = map(x->(x, 1), st)
359-
try Base.invokelatest(Base.update_stackframes_callback[], stn) catch end
360-
dict[UInt64(ip)] = map(first, stn)
352+
# we don't want metadata here as we're just looking up ips
353+
unique_ips = unique(has_meta(data) ? strip_meta(data) : data)
354+
n_unique_ips = length(unique_ips)
355+
n_unique_ips == 0 && return dict
356+
iplookups = similar(unique_ips, Vector{StackFrame})
357+
@sync for indexes_part in Iterators.partition(eachindex(unique_ips), div(n_unique_ips, Threads.nthreads(), RoundUp))
358+
Threads.@spawn begin
359+
for i in indexes_part
360+
iplookups[i] = _lookup_corrected(unique_ips[i])
361+
end
362+
end
363+
end
364+
for i in eachindex(unique_ips)
365+
dict[unique_ips[i]] = iplookups[i]
361366
end
362367
return dict
363368
end
364369

370+
function _lookup_corrected(ip::UInt)
371+
st = lookup(convert(Ptr{Cvoid}, ip))
372+
# To correct line numbers for moving code, put it in the form expected by
373+
# Base.update_stackframes_callback[]
374+
stn = map(x->(x, 1), st)
375+
# Note: Base.update_stackframes_callback[] should be data-race free
376+
try Base.invokelatest(Base.update_stackframes_callback[], stn) catch end
377+
return map(first, stn)
378+
end
379+
365380
"""
366381
flatten(btdata::Vector, lidict::LineInfoDict) -> (newdata::Vector{UInt64}, newdict::LineInfoFlatDict)
367382

0 commit comments

Comments
 (0)