I think that this is great. It certainly save me from having to remember to
turn OS on when testing, and turn OS off on deployment.

If this patch works, perhaps we can leaving ObjectSpace off by default in
future version of JRuby? It seems like a good way to give a good initial
performance without tuning.

Peter

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf
Of Charles Oliver Nutter
Sent: Tuesday, August 28, 2007 11:34 PM
To: [email protected]
Subject: [jruby-dev] Class iteration without ObjectSpace

Nick Sieger, Michael Koziarski and I were talking about ObjectSpace on IRC
this evening. After some discussion, it became apparent that most remaining
uses of ObjectSpace within Rails were only using the typical
ObjectSpace.each_object(Class) pattern to find descendants of a specific
class. The use of Class#inherited came up as a possible remedy, but
obviously the reason ObjectSpace.each_object(Class) is used in the first
place is to avoid having to track information by hand. Note also that
test/unit uses this to find TestCase subclasses, which is the main reason we
don't have ObjectSpace disabled all the time.

So the next obvious step is the possibility of implementation/VM changes.

Since it's pretty simple to track subclasses within JRuby code, I gave it a
try. The attached patch does the following:

- on instantiation, subclasses tell the parent class to add them to a weak
set.
- Class#subclasses returns a list of immediate subclasses. An optional
boolean argument can enable full descendants. Since this is a non-standard
API, I don't know how we'd want to handle it. It smells a little bit
"embrace and extend".
- ObjectSpace.each_object(Class) is short-circuited to actually do
Object.subclasses(true).each.

This means the following works:

$ jruby -O -e "ObjectSpace.each_object(Class) {|c| puts c if c < Numeric}"
Float
Integer
Bignum
Fixnum

Note that ObjectSpace is technically disabled here, but it still works for
each_object(Class).

Thoughts? This pattern seems to be by far the most common use of
each_object, and with this patch ObjectSpace can be turned off and the
pattern still works.

- Charlie

---------------------------------------------------------------------
To unsubscribe from this list please visit:

    http://xircles.codehaus.org/manage_email

Reply via email to