Skip to content

Commit 6728717

Browse files
gbaraldiKristofferC
authored andcommitted
Add set to temporary roots to avoid O(N) check (#57961)
(cherry picked from commit d53422c)
1 parent 69fef33 commit 6728717

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

src/aotcompile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ void *jl_emit_native_impl(jl_array_t *codeinfos, LLVMOrcThreadSafeModuleRef llvm
812812
generate_cfunc_thunks(params, compiled_functions);
813813
aot_optimize_roots(params, method_roots, compiled_functions);
814814
params.temporary_roots = nullptr;
815+
params.temporary_roots_set.clear();
815816
JL_GC_POP();
816817

817818
// process the globals array, before jl_merge_module destroys them

src/codegen.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,11 +3212,10 @@ static void jl_temporary_root(jl_codegen_params_t &ctx, jl_value_t *val)
32123212
{
32133213
if (!jl_is_globally_rooted(val)) {
32143214
jl_array_t *roots = ctx.temporary_roots;
3215-
for (size_t i = 0; i < jl_array_dim0(roots); i++) {
3216-
if (jl_array_ptr_ref(roots, i) == val)
3217-
return;
3218-
}
3215+
if (ctx.temporary_roots_set.find(val) != ctx.temporary_roots_set.end())
3216+
return;
32193217
jl_array_ptr_1d_push(roots, val);
3218+
ctx.temporary_roots_set.insert(val);
32203219
}
32213220
}
32223221
static void jl_temporary_root(jl_codectx_t &ctx, jl_value_t *val)

src/jitlayers.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,7 @@ void jl_emit_codeinst_to_jit_impl(
765765
}
766766
jl_optimize_roots(params, jl_get_ci_mi(codeinst), *result_m.getModuleUnlocked()); // contains safepoints
767767
params.temporary_roots = nullptr;
768+
params.temporary_roots_set.clear();
768769
JL_GC_POP();
769770
{ // drop lock before acquiring engine_lock
770771
auto release = std::move(params.tsctx_lock);

src/jitlayers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// This file is a part of Julia. License is MIT: https://julialang.org/license
22

3+
#include "llvm/ADT/SmallSet.h"
34
#include <llvm/ADT/MapVector.h>
45
#include <llvm/ADT/StringSet.h>
56
#include <llvm/Support/AllocatorBase.h>
@@ -240,6 +241,7 @@ struct jl_codegen_params_t {
240241
SmallVector<cfunc_decl_t,0> cfuncs;
241242
std::map<void*, GlobalVariable*> global_targets;
242243
jl_array_t *temporary_roots = nullptr;
244+
SmallSet<jl_value_t *, 0> temporary_roots_set;
243245
std::map<std::tuple<jl_code_instance_t*,bool>, GlobalVariable*> external_fns;
244246
std::map<jl_datatype_t*, DIType*> ditypes;
245247
std::map<jl_datatype_t*, Type*> llvmtypes;

0 commit comments

Comments
 (0)