Introducing
Your new presentation assistant.
Refine, enhance, and tailor your content, source relevant images, and edit visuals quicker than ever before.
Trending searches
Re: Ruby GC question (MRI, JRuby, etc)
Posted by Yukihiro Matsumoto (Guest) on 2010-08-21 02:15
Hi,
In message "Re: Ruby GC question (MRI, JRuby, etc)"
on Sat, 21 Aug 2010 08:15:15 +0900, Jörg W Mittag
<JoergWMittag+Ruby@GoogleMail.Com> writes:
|In particular, the GC algorithms in MRI and YARV are specifically
|designed with the assumption that they will never actually run in
|99.999% of all cases. They are designed for scripting, where a script
|doesn't even allocate enough memory to trigger a collection, runs for
|a couple of seconds and then exits, after which the OS simply reclaims
|the memory: no GC needed.
99.999% is a bit over-exaggerated, but it is true that garbage
collection algorithm of YARV and MRI focus for throughput on
non-memory extensive short-running programs, and GC of REE is not
suitable for those programs.
matz.
class Array
include Enumerable
# Passes each index of the Array to the given block
# and returns self. We re-evaluate @total each time
# through the loop in case the array has changed.
def each_index
return to_enum(:each_index) unless block_given?
i = @start
total = i + @total
while i < total
yield i
i += 1
end
self
end
end
static VALUE
rb_ary_each_index(VALUE ary)
{
long i;
RETURN_ENUMERATOR(ary, 0, 0);
for (i=0; i<RARRAY_LEN(ary); i++) {
rb_yield(LONG2NUM(i));
}
return ary;
}
void
Init_Array(void)
{
rb_cArray = rb_define_class("Array", rb_cObject);
rb_include_module(rb_cArray, rb_mEnumerable);
rb_define_method(rb_cArray, "each_index", rb_ary_each_index, 0);
}
class Fixnum < Integer
def +(o)
Ruby.primitive :fixnum_add
redo_coerced :+, o
end
end
class Fixnum : public Integer {
public:
// Ruby.primitive! :fixnum_add
Integer* add(STATE, Fixnum* other) {
native_int r = to_native() + other->to_native();
if(r > FIXNUM_MAX || r < FIXNUM_MIN) {
return Bignum::from(state, r);
} else {
return Fixnum::from(r);
}
}
}
module GC
# The Maglev in-memory garbage collector runs
# automatically whenever GC is required.
# It cannot be disabled.
def self.start
# has no effect
end
def self.enable
false # gc is never disabled
end
def self.disable
# has no effect
false
end
def garbage_collect
# has no effect
end
def self.stress# added for 1.8.7
false
end
def self.stress=(bool)# added for 1.8.7
# has no effect
false
end
end
RubyConf 2009上的Rubinius session
(Evan Phoenix)
module GC
def self.start
run(false)
end
def self.run(force)
Ruby.primitive :vm_gc_start
raise PrimitiveFailure, "GC.run primitive failed"
end
# Totally fake.
def self.stress
@stress_level ||= false
end
# Totally fake.
def self.stress=(flag)
@stress_level = !!flag
end
# Totally fake.
@enabled = true
def self.enable
# We don't support disable, so sure! enabled!
ret = !@enabled
@enabled = true
return ret
end
# Totally fake.
def self.disable
# Treat this like a request that we don't honor.
ret = !@enabled
@enabled = false
return ret
end
def garbage_collect
GC.start
end
end
module ObjectSpace
def self.find_object(query, callable)
Ruby.primitive :vm_find_object
raise PrimitiveFailure, "vm_each_object failed"
end
end
static VALUE
fix_plus(VALUE x, VALUE y)
{
if (FIXNUM_P(y)) {
long a, b, c;
VALUE r;
a = FIX2LONG(x);
b = FIX2LONG(y);
c = a + b;
r = LONG2NUM(c);
return r;
}
switch (TYPE(y)) {
case T_BIGNUM:
return rb_big_plus(y, x);
case T_FLOAT:
return DBL2NUM((double)FIX2LONG(x) + RFLOAT_VALUE(y));
default:
return rb_num_coerce_bin(x, y, '+');
}
}
void
Init_Numeric(void)
{
rb_cNumeric = rb_define_class("Numeric", rb_cObject);
rb_cInteger = rb_define_class("Integer", rb_cNumeric);
rb_cFixnum = rb_define_class("Fixnum", rb_cInteger);
rb_define_method(rb_cFixnum, "+", fix_plus, 1);
}
typo: it's :* here