From: Charles Oliver Nutter Date: 2007-10-28T15:34:44+09:00 Subject: Re: How fast does your Ruby run? Joel VanderWerf wrote: > The dependence on ObjectSpace is kind of nasty, isn't it? A library > shouldn't use ObjectSpace in this way, because what it finds there may > depend on whether GC has run recently or not. Yes, ObjectSpace is a useful tool for walking the heap when you really want to, but it's not deterministic about whether or when it will find the object(s) you want. So I agree it's a bad feature to use as part of libraries you want to work the same way every time. In the case of test/unit, most TestCase descendants get hard-referenced as classes assigned to constants somewhere. However if you constructed a test case as follows: Class.new(Test::Unit::TestCase) { def test_something; end } ...it may or may not run with the default loader. > Good to hear you found a way to support it anyway, though. > > Does your mechanism to walk child classes prevent them from being > collected, if there are no other references to them? If so, then it > isn't quite bug-compatible (though it is unlikely to matter): No, the references to child classes are all weak references; if they go away, the references will go away. It's a much smaller price to pay than attaching weak references to *all* constructed objects (to my knowledge, it costs almost nothing, and test/unit still works. The typical way I think people would fix test/unit's use of ObjectSpace would be to add a Class#inherited hook that records all subclasses of TestCase. But nobody's gone ahead with that change yet. - Charlie