From: Eric Wong <normalperson@...>
Date: 2011-06-10T18:37:21+09:00
Subject: [ruby-core:36915] Re: [Ruby 1.9 - Feature #3905] rb_clear_cache_by_class() called often during GC for non-blocking I/O

Charles Oliver Nutter <headius@headius.com> wrote:
> On Thu, Jun 9, 2011 at 3:17 AM, Eric Wong <normalperson@yhbt.net> wrote:
> > Nevermind, I misread the first time and got ordering of your question
> > mixed up in my mind.
> >
> > Once a class is tagged RCLASS_EPHEMERAL, it's impossible for it to
> > write to the cache. ��There's no need to flush the cache for ephemeral
> > classes because...
> ...
> > ...the the /only/ safe way to use RCLASS_EPHEMERAL is before any methods
> > are called (and cached) for the singleton class.
> 
> >> begin
> >> �� io.read_nonblock
> >> rescue WaitReadable => e
> >> �� class << self
> >> �� �� # add something cute
> >
> > Any methods defined here will never be cached, because RCLASS_EPHEMERAL
> > was set before we re-entered Ruby-land.
> 
> Ok, not being familiar with the MRI code, and not seeing more than a
> few lines of context in the patch, I didn't get this.
> 
> So summarizing in non-code:
> 
> * Ephemeral class creation does not flush global cache
> * ...because ephemeral class methods will never be cached

Yes, to both.  Creation of new classes (ephemeral or not) never touches
the method cache, only destruction clears the cache.  The global method
cache includes the (exact) class of each method along with the method
ID.

> But I'm confused; if code has already cached a method from an
> ephemeral class's superclass, and someone adds to the ephemeral class,
> does the new method get picked up? Hopefully adding methods to an
> ephemeral class still clears cache, because otherwise invocation won't
> see such changes. Am I following?

Yes a method gets picked up in the ephmeral class if the (non-ephemeral)
superclass is modified.

The MRI method cache relies on the class of the calling object matching
/exactly/ with the class of the cached method for a hit.  Modifying a
superclass of any class

I think adding any method anywhere will clear the cache in MRI.  But
it's not needed for adding methods to ephemeral classes since they'd
never have any methods in the cache in the first place.

> I need to look at ko1's patch since that seems to please you.

Yes, it's the best patch (along with r28813) I've seen for this issue.
Easy to understand, too.

The only thing I can imagine being better is to make uncached method
lookup fast enough to where the cache becomes obsolete.  I think that
would be difficult, though.

-- 
Eric Wong