From: "charliesome (Charlie Somerville)" Date: 2013-02-10T10:33:45+09:00 Subject: [ruby-core:52095] [ruby-trunk - Feature #7816] Don't invalidate method caches when defining a new method on a class without subclasses Issue #7816 has been updated by charliesome (Charlie Somerville). marcandre (Marc-Andre Lafortune) wrote: > I'd split the new API from the patch; personally I'm not convinced of the usefulness of Class#has_subclass? or of RubyVM.state_version I'm not particularly attached to Class#has_subclass? - I don't care if that stays or goes. I figured that if we have the information, we may as well let the user access it. I think RubyVM.state_version could be very useful for performance profiling. I work on a Rails app that invalidates the method cache ~200 times per request (mainly due to OpenStruct). I think this patch will lower that number somewhat, but I want to keep this API in so I (and others) can monitor cache invalidations and try to eliminate the remaining invalidations. ---------------------------------------- Feature #7816: Don't invalidate method caches when defining a new method on a class without subclasses https://bugs.ruby-lang.org/issues/7816#change-36111 Author: charliesome (Charlie Somerville) Status: Open Priority: Normal Assignee: Category: core Target version: next minor =begin Attached is a patch that avoids incrementing the VM state version when defining a method and these conditions are true: * The method is not a redefinition of an existing method in the same class or any superclass * The class has no subclasses * The class is not a module This means that defining singleton methods on objects no longer invalidates every method cache. This will significantly improve performance of code that defines singleton methods at runtime (eg. when using OpenStruct) In my testing, a fresh Rails app boots about 15% faster (~1.7 sec down to ~1.4 sec). This controller action can do ~440 requests per second with my patch, compared to ~320 requests per second without my patch. class HomeController < ApplicationController def index OpenStruct.new a: 1, b: 2 render text: "home" end end Of course these numbers will vary between apps, but I think this is a good start in improving the performance of a very common use case in Ruby. =end -- http://bugs.ruby-lang.org/