From a182b2c5e1dbdbe8960912f8bac076d997a74e65 Mon Sep 17 00:00:00 2001 From: Peter Zhu Date: Mon, 20 Nov 2023 11:28:36 -0500 Subject: [PATCH] Implement Enumerator objects on VWA This commit implements Enumerator objects on VWA. This speeds allocations and decreases memory usage. ``` require "benchmark" ary = [] puts(Benchmark.measure do 10_000_000.times do u = ary.to_enum end end) puts `ps -o rss= -p #{$$}` ``` Before: ``` 1.500774 0.002717 1.503491 ( 1.506791) 18512 ``` After: ``` 0.892580 0.002539 0.895119 ( 0.897642) 16480 ``` --- enumerator.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/enumerator.c b/enumerator.c index 0e010c14c2..98432df93d 100644 --- a/enumerator.c +++ b/enumerator.c @@ -256,23 +256,15 @@ struct enum_product { VALUE rb_cArithSeq; -#define enumerator_free RUBY_TYPED_DEFAULT_FREE - -static size_t -enumerator_memsize(const void *p) -{ - return sizeof(struct enumerator); -} - static const rb_data_type_t enumerator_data_type = { "enumerator", { REFS_LIST_PTR(enumerator_refs), - enumerator_free, - enumerator_memsize, + RUBY_TYPED_DEFAULT_FREE, + NULL, // Nothing allocated externally, so don't need a memsize function NULL, }, - 0, NULL, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_DECL_MARKING + 0, NULL, RUBY_TYPED_FREE_IMMEDIATELY | RUBY_TYPED_DECL_MARKING | RUBY_TYPED_EMBEDDABLE }; static struct enumerator * @@ -1909,7 +1901,7 @@ lazy_add_method(VALUE obj, int argc, VALUE *argv, VALUE args, VALUE memo, rb_ary_push(new_procs, entry_obj); new_obj = enumerator_init_copy(enumerator_allocate(rb_cLazy), obj); - new_e = DATA_PTR(new_obj); + new_e = RTYPEDDATA_GET_DATA(new_obj); new_e->obj = new_generator; new_e->procs = new_procs;