From 17da66de237520cd3b6c307cc051c53a738598d9 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Tue, 18 Apr 2023 16:25:00 -0400 Subject: Remove use of RARRAY_PTR RARRAY_PTR is bad for GC performance as it exposes the internal buffer of the array meaning that the array becomes write barrier unprotected. --- ext/ffi_c/AbstractMemory.c | 2 +- ext/ffi_c/Struct.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/ffi_c/AbstractMemory.c b/ext/ffi_c/AbstractMemory.c index 3cf16a7..10ddc28 100644 --- a/ext/ffi_c/AbstractMemory.c +++ b/ext/ffi_c/AbstractMemory.c @@ -150,7 +150,7 @@ memory_put_array_of_##name(VALUE self, VALUE offset, VALUE ary) \ if (likely(count > 0)) checkWrite(memory); \ checkBounds(memory, off, count * sizeof(type)); \ for (i = 0; i < count; i++) { \ - type tmp = (type) VAL(toNative(RARRAY_PTR(ary)[i]), swap); \ + type tmp = (type) VAL(toNative(RARRAY_AREF(ary, i)), swap); \ memcpy(memory->address + off + (i * sizeof(type)), &tmp, sizeof(tmp)); \ } \ return self; \ diff --git a/ext/ffi_c/Struct.c b/ext/ffi_c/Struct.c index b9b6b2c..9eb10ec 100644 --- a/ext/ffi_c/Struct.c +++ b/ext/ffi_c/Struct.c @@ -132,7 +132,7 @@ struct_initialize(int argc, VALUE* argv, VALUE self) /* Call up into ruby code to adjust the layout */ if (nargs > 1) { - VALUE rbLayout = rb_funcall2(CLASS_OF(self), id_layout, (int) RARRAY_LEN(rest), RARRAY_PTR(rest)); + VALUE rbLayout = rb_apply(CLASS_OF(self), id_layout, rest); RB_OBJ_WRITE(self, &s->rbLayout, rbLayout); } else { RB_OBJ_WRITE(self, &s->rbLayout, struct_class_layout(klass)); -- cgit v1.2.1