Skip to content

Commit d53422c

Browse files
authored
Add set to temporary roots to avoid O(N) check (#57961)
1 parent 4635377 commit d53422c

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
@@ -824,6 +824,7 @@ void *jl_emit_native_impl(jl_array_t *codeinfos, LLVMOrcThreadSafeModuleRef llvm
824824
generate_cfunc_thunks(params, compiled_functions);
825825
aot_optimize_roots(params, method_roots, compiled_functions);
826826
params.temporary_roots = nullptr;
827+
params.temporary_roots_set.clear();
827828
JL_GC_POP();
828829

829830
// 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
@@ -3170,11 +3170,10 @@ static void jl_temporary_root(jl_codegen_params_t &ctx, jl_value_t *val)
31703170
{
31713171
if (!jl_is_globally_rooted(val)) {
31723172
jl_array_t *roots = ctx.temporary_roots;
3173-
for (size_t i = 0; i < jl_array_dim0(roots); i++) {
3174-
if (jl_array_ptr_ref(roots, i) == val)
3175-
return;
3176-
}
3173+
if (ctx.temporary_roots_set.find(val) != ctx.temporary_roots_set.end())
3174+
return;
31773175
jl_array_ptr_1d_push(roots, val);
3176+
ctx.temporary_roots_set.insert(val);
31783177
}
31793178
}
31803179
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
@@ -788,6 +788,7 @@ void jl_emit_codeinst_to_jit_impl(
788788
}
789789
jl_optimize_roots(params, jl_get_ci_mi(codeinst), *result_m.getModuleUnlocked()); // contains safepoints
790790
params.temporary_roots = nullptr;
791+
params.temporary_roots_set.clear();
791792
JL_GC_POP();
792793
{ // drop lock before acquiring engine_lock
793794
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>
@@ -245,6 +246,7 @@ struct jl_codegen_params_t {
245246
SmallVector<cfunc_decl_t,0> cfuncs;
246247
std::map<void*, GlobalVariable*> global_targets;
247248
jl_array_t *temporary_roots = nullptr;
249+
SmallSet<jl_value_t *, 0> temporary_roots_set;
248250
std::map<std::tuple<jl_code_instance_t*,bool>, GlobalVariable*> external_fns;
249251
std::map<jl_datatype_t*, DIType*> ditypes;
250252
std::map<jl_datatype_t*, Type*> llvmtypes;

0 commit comments

Comments
 (0)