From 856903850bf86932f7994da799479eb9c1550041 Mon Sep 17 00:00:00 2001 From: normal Date: Sat, 13 Dec 2014 01:28:18 +0000 Subject: gc.c (define_final0): avoid duplicate blocks This prevents excessive memory growth when a WeakRef is repeatedly created * gc.c (define_final0): avoid duplicate blocks [Bug #10537] * test/test_weakref.rb (test_repeated_object_leak): new test git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@48820 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- gc.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'gc.c') diff --git a/gc.c b/gc.c index feb9c08059..1caa790202 100644 --- a/gc.c +++ b/gc.c @@ -2370,6 +2370,20 @@ define_final0(VALUE obj, VALUE block) if (st_lookup(finalizer_table, obj, &data)) { table = (VALUE)data; + + /* avoid duplicate block, table is usually small */ + { + const VALUE *ptr = RARRAY_CONST_PTR(table); + long len = RARRAY_LEN(table); + long i; + + for (i = 0; i < len; i++, ptr++) { + if (rb_funcall(*ptr, idEq, 1, block)) { + return *ptr; + } + } + } + rb_ary_push(table, block); } else { -- cgit v1.2.3