ruby-changes:27759
From: nari <ko1@a...>
Date: Mon, 18 Mar 2013 21:45:05 +0900 (JST)
Subject: [ruby-changes:27759] nari:r39811 (trunk): * gc.c: Avoid unnecessary heap growth. patched by tmm1(Aman Gupta).
nari 2013-03-18 21:44:55 +0900 (Mon, 18 Mar 2013) New Revision: 39811 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=39811 Log: * gc.c: Avoid unnecessary heap growth. patched by tmm1(Aman Gupta). [Bug #8093] [ruby-core:53393] Modified files: trunk/ChangeLog trunk/gc.c trunk/test/ruby/test_gc.rb Index: ChangeLog =================================================================== --- ChangeLog (revision 39810) +++ ChangeLog (revision 39811) @@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1 +Mon Mar 18 21:42:48 2013 Narihiro Nakamura <authornari@g...> + + * gc.c: Avoid unnecessary heap growth. patched by tmm1(Aman Gupta). + [Bug #8093] [ruby-core:53393] + Mon Mar 18 17:58:36 2013 Narihiro Nakamura <authornari@g...> * gc.c: Fix unlimited memory growth with large values of Index: gc.c =================================================================== --- gc.c (revision 39810) +++ gc.c (revision 39811) @@ -228,6 +228,7 @@ typedef struct rb_objspace { https://github.com/ruby/ruby/blob/trunk/gc.c#L228 struct heaps_free_bitmap *free_bitmap; RVALUE *range[2]; struct heaps_header *freed; + size_t marked_num; size_t free_num; size_t free_min; size_t final_num; @@ -2001,7 +2002,7 @@ after_gc_sweep(rb_objspace_t *objspace) https://github.com/ruby/ruby/blob/trunk/gc.c#L2002 inc = ATOMIC_SIZE_EXCHANGE(malloc_increase, 0); if (inc > malloc_limit) { malloc_limit += - (size_t)((inc - malloc_limit) * (double)objspace_live_num(objspace) / (heaps_used * HEAP_OBJ_LIMIT)); + (size_t)((inc - malloc_limit) * (double)objspace->heap.marked_num / (heaps_used * HEAP_OBJ_LIMIT)); if (malloc_limit < initial_malloc_limit) malloc_limit = initial_malloc_limit; } @@ -2074,7 +2075,7 @@ gc_prepare_free_objects(rb_objspace_t *o https://github.com/ruby/ruby/blob/trunk/gc.c#L2075 gc_marks(objspace); before_gc_sweep(objspace); - if (objspace->heap.free_min > (heaps_used * HEAP_OBJ_LIMIT - objspace_live_num(objspace))) { + if (objspace->heap.free_min > (heaps_used * HEAP_OBJ_LIMIT - objspace->heap.marked_num)) { set_heaps_increment(objspace); } @@ -2562,6 +2563,7 @@ gc_mark_ptr(rb_objspace_t *objspace, VAL https://github.com/ruby/ruby/blob/trunk/gc.c#L2563 register uintptr_t *bits = GET_HEAP_BITMAP(ptr); if (MARKED_IN_BITMAP(bits, ptr)) return 0; MARK_IN_BITMAP(bits, ptr); + objspace->heap.marked_num++; return 1; } @@ -2922,6 +2924,7 @@ gc_marks(rb_objspace_t *objspace) https://github.com/ruby/ruby/blob/trunk/gc.c#L2924 objspace->mark_func_data = 0; gc_prof_mark_timer_start(objspace); + objspace->heap.marked_num = 0; objspace->count++; SET_STACK_END; Index: test/ruby/test_gc.rb =================================================================== --- test/ruby/test_gc.rb (revision 39810) +++ test/ruby/test_gc.rb (revision 39811) @@ -146,4 +146,18 @@ class TestGc < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/ruby/test_gc.rb#L146 ObjectSpace.define_finalizer(Thread.main) { p 'finalize' } EOS end + + def test_expand_heap + assert_separately %w[--disable-gem], __FILE__, __LINE__, <<-'eom' + base_length = GC.stat[:heap_length] + (base_length * 500).times{ 'a' } + GC.start + assert_equal base_length, GC.stat[:heap_length], "invalid heap expanding" + + a = [] + (base_length * 500).times{ a << 'a' } + GC.start + assert base_length < GC.stat[:heap_length] + eom + end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/