Skip to content

Commit f047af7

Browse files
committed
Fast-track @threads when nthreads() == 1
This avoids overhead when threading is disabled, Example benchmark: ``` using BenchmarkTools, Base.Threads function func(val) sum = 0*(1 .^ val) for idx in 1:100 sum += idx.^val end return sum end function func_threaded(val) sum = 0*(1 .^ val) @threads for idx in 1:100 sum += idx.^val end return sum end function func_threaded_ref(val) sum = Ref(0*(1 .^ val)) @threads for idx in 1:100 sum[] += idx.^val end return sum[] end @show @benchmark func(2.0) @show @benchmark func_threaded(2.0) @show @benchmark func_threaded_ref(2.0) ``` Before change: ``` ``` After change: ``` ``` The rest of the overhead is attributed to the `Box` introduced by #15276
1 parent 1e06e68 commit f047af7

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

base/threadingconstructs.jl

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ function _threadsfor(iter,lbody)
2525
lidx = iter.args[1] # index
2626
range = iter.args[2]
2727
quote
28+
local serialfor_fun
2829
local threadsfor_fun
30+
2931
let range = $(esc(range))
32+
# Fast-track serial execution for case of nthreads == 1
33+
function serialfor_fun()
34+
for $(esc(lidx)) in range
35+
$(esc(lbody))
36+
end
37+
end
38+
3039
function threadsfor_fun(onethread=false)
3140
r = range # Load into local variable
3241
lenr = length(r)
@@ -70,10 +79,14 @@ function _threadsfor(iter,lbody)
7079
# We are in a nested threaded loop
7180
Base.invokelatest(threadsfor_fun, true)
7281
else
73-
in_threaded_loop[] = true
74-
# the ccall is not expected to throw
75-
ccall(:jl_threading_run, Cvoid, (Any,), threadsfor_fun)
76-
in_threaded_loop[] = false
82+
if nthreads() == 1
83+
Base.invokelatest(serialfor_fun)
84+
else
85+
in_threaded_loop[] = true
86+
# the ccall is not expected to throw
87+
ccall(:jl_threading_run, Cvoid, (Any,), threadsfor_fun)
88+
in_threaded_loop[] = false
89+
end
7790
end
7891
nothing
7992
end

0 commit comments

Comments
 (0)