Skip to content

Commit 3390785

Browse files
committed
Avoid an unnecessary std::function allocation in codegen
We already need to manually create a C compatible function pointer and heap allocated data, might as well do so directly without another indirection through std::function.
1 parent cd2ebf9 commit 3390785

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

src/aotcompile.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1827,10 +1827,16 @@ static void construct_vars(Module &M, Partition &partition, StringRef suffix) {
18271827
gidxs_var->setDSOLocal(true);
18281828
}
18291829

1830-
extern "C" void lambda_trampoline(void* arg) {
1831-
std::function<void()>* func = static_cast<std::function<void()>*>(arg);
1832-
(*func)();
1833-
delete func;
1830+
template<typename CB>
1831+
static inline void schedule_uv_thread(uv_thread_t *worker, CB &&cb)
1832+
{
1833+
auto func = new CB(std::move(cb));
1834+
// Use libuv thread to avoid issues with stack sizes
1835+
uv_thread_create(worker, [] (void *arg) {
1836+
auto func = static_cast<CB*>(arg);
1837+
(*func)();
1838+
delete func;
1839+
}, func);
18341840
}
18351841

18361842
// Entrypoint to optionally-multithreaded image compilation. This handles global coordination of the threading,
@@ -1930,7 +1936,7 @@ static SmallVector<AOTOutputs, 16> add_output(Module &M, TargetMachine &TM, Stri
19301936
JL_TIMING(NATIVE_AOT, NATIVE_Opt);
19311937
std::vector<uv_thread_t> workers(threads);
19321938
for (unsigned i = 0; i < threads; i++) {
1933-
std::function<void()> func = [&, i]() {
1939+
schedule_uv_thread(&workers[i], [&, i]() {
19341940
LLVMContext ctx;
19351941
ctx.setDiscardValueNames(true);
19361942
// Lazily deserialize the entire module
@@ -1961,9 +1967,7 @@ static SmallVector<AOTOutputs, 16> add_output(Module &M, TargetMachine &TM, Stri
19611967
timers[i].construct.stopTimer();
19621968

19631969
outputs[i] = add_output_impl(*M, TM, timers[i], unopt_out, opt_out, obj_out, asm_out);
1964-
};
1965-
auto arg = new std::function<void()>(func);
1966-
uv_thread_create(&workers[i], lambda_trampoline, arg); // Use libuv thread to avoid issues with stack sizes
1970+
});
19671971
}
19681972

19691973
// Wait for all of the worker threads to finish

0 commit comments

Comments
 (0)