From 2ec2437d23f2b3b5f1136041268a57cc1ebd8f34 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sat, 19 Oct 2024 11:10:10 +0900 Subject: [PATCH] Small improvements for `mrb_gc_unregister()` `ARY_PTR()` and `ARY_LEN()` avoid using them in a loop if the array is not changed, since they involve branching. --- src/gc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/gc.c b/src/gc.c index a9549e730..78b7e00ea 100644 --- a/src/gc.c +++ b/src/gc.c @@ -455,11 +455,10 @@ mrb_gc_unregister(mrb_state *mrb, mrb_value obj) } a = mrb_ary_ptr(table); mrb_ary_modify(mrb, a); - for (mrb_int i = 0; i < ARY_LEN(a); i++) { - if (mrb_ptr(ARY_PTR(a)[i]) == mrb_ptr(obj)) { - mrb_int len = ARY_LEN(a)-1; - mrb_value *ptr = ARY_PTR(a); - + mrb_int len = ARY_LEN(a)-1; + mrb_value *ptr = ARY_PTR(a); + for (mrb_int i = 0; i <= len; i++) { + if (mrb_ptr(ptr[i]) == mrb_ptr(obj)) { ARY_SET_LEN(a, len); memmove(&ptr[i], &ptr[i + 1], (len - i) * sizeof(mrb_value)); break;