@@ -8,12 +8,29 @@ const thread_count_functions = Ref{Vector{Pair{Function, Function}}}(
88 ]
99)
1010
11+ # Non-inlined helpers so that the abstract Function dispatch is contained in
12+ # AbstractOperators and not inlined into the calling module (which would cause
13+ # JET @test_opt findings when target_modules excludes AbstractOperators).
14+ @noinline function _save_thread_counts ()
15+ return [pair. first () for pair in thread_count_functions[]]
16+ end
17+
18+ @noinline function _apply_thread_counts (n:: Int )
19+ for pair in thread_count_functions[]
20+ pair. second (n)
21+ end
22+ end
23+
24+ @noinline function _restore_thread_counts (prev:: Vector )
25+ for (i, pair) in enumerate (thread_count_functions[])
26+ pair. second (prev[i])
27+ end
28+ end
29+
1130function set_thread_counts_expr (thread_count_expr, body_expr)
1231 return quote
13- local prev_thread_counts = [pair. first () for pair in AbstractOperators. thread_count_functions[]]
14- for pair in AbstractOperators. thread_count_functions[]
15- pair. second ($ thread_count_expr)
16- end
32+ local prev_thread_counts = AbstractOperators. _save_thread_counts ()
33+ AbstractOperators. _apply_thread_counts ($ thread_count_expr)
1734 local res
1835 try
1936 if $ thread_count_expr == 1
@@ -26,9 +43,7 @@ function set_thread_counts_expr(thread_count_expr, body_expr)
2643 end
2744 finally
2845 # Restore previous thread counts
29- for (i, pair) in enumerate (AbstractOperators. thread_count_functions[])
30- pair. second (prev_thread_counts[i])
31- end
46+ AbstractOperators. _restore_thread_counts (prev_thread_counts)
3247 end
3348 res
3449 end
0 commit comments