Skip to content

Commit 1dc1459

Browse files
committed
optimize pinning in ast.c
1 parent 5e33341 commit 1dc1459

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

src/ast.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ typedef struct _jl_ast_context_t {
136136
value_t ssavalue_sym;
137137
value_t slot_sym;
138138
jl_module_t *module; // context module for `current-julia-module-counter`
139-
struct _jl_ast_context_t *next; // invasive list pointer for getting free contexts
139+
arraylist_t pinned_objects;
140140
} jl_ast_context_t;
141141

142142
static jl_ast_context_t jl_ast_main_ctx;
@@ -279,27 +279,29 @@ static void jl_init_ast_ctx(jl_ast_context_t *ctx) JL_NOTSAFEPOINT
279279
ctx->slot_sym = symbol(fl_ctx, "slot");
280280
ctx->module = NULL;
281281
set(symbol(fl_ctx, "*scopewarn-opt*"), fixnum(jl_options.warn_scope));
282+
arraylist_new(&ctx->pinned_objects, 0);
282283
}
283284

284285
// There should be no GC allocation while holding this lock
285286
static uv_mutex_t flisp_lock;
286-
static jl_ast_context_t *jl_ast_ctx_freed = NULL;
287+
int flisp_initialized = 0;
288+
arraylist_t jl_ast_ctx_freed;
289+
arraylist_t jl_ast_ctx_used;
287290

288291
static jl_ast_context_t *jl_ast_ctx_enter(jl_module_t *m) JL_GLOBALLY_ROOTED JL_NOTSAFEPOINT
289292
{
290293
JL_SIGATOMIC_BEGIN();
291294
uv_mutex_lock(&flisp_lock);
292-
jl_ast_context_t *ctx = jl_ast_ctx_freed;
293-
if (ctx != NULL) {
294-
jl_ast_ctx_freed = ctx->next;
295-
ctx->next = NULL;
296-
}
295+
jl_ast_context_t *ctx = (jl_ast_context_t*)arraylist_pop(&jl_ast_ctx_freed);
297296
uv_mutex_unlock(&flisp_lock);
298297
if (ctx == NULL) {
299298
// Construct a new one if we can't find any
300299
ctx = (jl_ast_context_t*)calloc(1, sizeof(jl_ast_context_t));
301300
jl_init_ast_ctx(ctx);
302301
}
302+
uv_mutex_lock(&flisp_lock);
303+
arraylist_push(&jl_ast_ctx_used, ctx);
304+
uv_mutex_unlock(&flisp_lock);
303305
ctx->module = m;
304306
return ctx;
305307
}
@@ -308,16 +310,20 @@ static void jl_ast_ctx_leave(jl_ast_context_t *ctx)
308310
{
309311
uv_mutex_lock(&flisp_lock);
310312
ctx->module = NULL;
311-
ctx->next = jl_ast_ctx_freed;
312-
jl_ast_ctx_freed = ctx;
313+
ctx->pinned_objects.len = 0; // clear pinned objects
314+
arraylist_pop(&jl_ast_ctx_used);
315+
arraylist_push(&jl_ast_ctx_freed, ctx);
313316
uv_mutex_unlock(&flisp_lock);
314317
JL_SIGATOMIC_END();
315318
}
316319

317320
void jl_init_flisp(void)
318321
{
319-
if (jl_ast_ctx_freed)
322+
if (flisp_initialized)
320323
return;
324+
flisp_initialized = 1;
325+
arraylist_new(&jl_ast_ctx_freed, 0);
326+
arraylist_new(&jl_ast_ctx_used, 0);
321327
uv_mutex_init(&flisp_lock);
322328
jl_init_ast_ctx(&jl_ast_main_ctx);
323329
// To match the one in jl_ast_ctx_leave

src/gc-mmtk.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ JL_DLLEXPORT void jl_gc_collect(jl_gc_collection_t collection) {
254254
// print_fragmentation();
255255
}
256256

257-
void gc_pin_objects_from_inference_engine(arraylist_t *objects_pinned_by_call)
257+
void gc_pin_objects_from_compiler_frontend(arraylist_t *objects_pinned_by_call)
258258
{
259259
for (size_t i = 0; i < gc_pinned_objects.len; i++) {
260260
void *obj = gc_pinned_objects.items[i];
@@ -265,7 +265,7 @@ void gc_pin_objects_from_inference_engine(arraylist_t *objects_pinned_by_call)
265265
}
266266
}
267267

268-
void gc_unpin_objects_from_inference_engine(arraylist_t *objects_pinned_by_call)
268+
void gc_unpin_objects_from_compiler_frontend(arraylist_t *objects_pinned_by_call)
269269
{
270270
for (size_t i = 0; i < objects_pinned_by_call->len; i++) {
271271
void *obj = objects_pinned_by_call->items[i];
@@ -337,9 +337,9 @@ JL_DLLEXPORT void jl_gc_prepare_to_collect(void)
337337
#ifndef __clang_gcanalyzer__
338338
arraylist_t objects_pinned_by_call;
339339
arraylist_new(&objects_pinned_by_call, 0);
340-
gc_pin_objects_from_inference_engine(&objects_pinned_by_call);
340+
gc_pin_objects_from_compiler_frontend(&objects_pinned_by_call);
341341
mmtk_block_thread_for_gc();
342-
gc_unpin_objects_from_inference_engine(&objects_pinned_by_call);
342+
gc_unpin_objects_from_compiler_frontend(&objects_pinned_by_call);
343343
arraylist_free(&objects_pinned_by_call);
344344
#endif
345345
JL_UNLOCK_NOGC(&finalizers_lock);

src/julia.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,7 @@ JL_DLLEXPORT void jl_register_newmeth_tracer(void (*callback)(jl_method_t *trace
24392439

24402440
// AST access
24412441
JL_DLLEXPORT jl_value_t *jl_copy_ast(jl_value_t *expr JL_MAYBE_UNROOTED);
2442+
extern arraylist_t jl_ast_ctx_used;
24422443

24432444
// IR representation
24442445
JL_DLLEXPORT jl_value_t *jl_compress_ir(jl_method_t *m, jl_code_info_t *code);

0 commit comments

Comments
 (0)