delegate.rb is incredibly slow
------------------------------

                 Key: JRUBY-4658
                 URL: http://jira.codehaus.org/browse/JRUBY-4658
             Project: JRuby
          Issue Type: Bug
            Reporter: Charles Oliver Nutter
            Assignee: Thomas E Enebo
            Priority: Blocker
             Fix For: JRuby 1.5


The Nokogiri guys reported that my "weakling" library was very slow. After 
digging a bit, I traced the problem to WeakRef construction:

{noformat}
~/projects/jruby ➔ jruby -rbenchmark -rweakref -e "5.times { puts 
Benchmark.measure { 1000.times { WeakRef.new(self) } } }"
  1.439000   0.000000   1.439000 (  1.418000)
  1.068000   0.000000   1.068000 (  1.068000)
  1.038000   0.000000   1.038000 (  1.037000)
  1.041000   0.000000   1.041000 (  1.041000)
  1.054000   0.000000   1.054000 (  1.054000)
{noformat}

This is basically taking more than 1ms to construct every object. A profile 
showed that the time was spent doing two things: parsing, and twiddling 
ConcurrentHashMap inside RubyModule. That led me to the delegate.rb library, 
since WeakRef < Delegator.

It turns out that Delegator has some really dreadful code in its initialize, 
which gets called for every construction:

{noformat}
  def initialize(obj)
    preserved = ::Kernel.public_instance_methods(false)
    preserved -= ["to_s","to_a","inspect","==","=~","==="]
    for t in self.class.ancestors
      preserved |= t.public_instance_methods(false)
      preserved |= t.private_instance_methods(false)
      preserved |= t.protected_instance_methods(false)
      break if t == Delegator
    end
    preserved << "singleton_method_added"
    for method in obj.methods
      next if preserved.include? method
      begin
        eval <<-EOS, nil, __FILE__, __LINE__+1
          def self.#{method}(*args, &block)
            begin
              __getobj__.__send__(:#{method}, *args, &block)
            ensure
              $...@.delete_if{|s|IgnoreBacktracePat=~s} if $@
            end
          end
        EOS
      rescue SyntaxError
        raise NameError, "invalid identifier %s" % method, caller(4)
      end
    end
  end
{noformat}

Because of this, the Nokogiri guys are not comfortable replacing id2ref usage 
with WeakRef::IdHash from weakling (and I don't blame them). Since we really 
don't want to turn id2ref support back on, I think we need to find a solution 
for delegate.rb.

FWIW, 1.9 does have a better delegate.rb implementation, but it doesn't appear 
to pass specs. So we are faced with a bit of a conundrum.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://jira.codehaus.org/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

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

    http://xircles.codehaus.org/manage_email


Reply via email to