Project

General

Profile

Actions

Feature #7816

closed

Don't invalidate method caches when defining a new method on a class without subclasses

Added by charliesome (Charlie Somerville) over 9 years ago. Updated over 8 years ago.

Status:
Closed
Priority:
Normal
Target version:
[ruby-core:52075]

Description

=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


Files

dont-invalidate-method-cache-when-defining-new-singleton-methods.patch (4.55 KB) dont-invalidate-method-cache-when-defining-new-singleton-methods.patch charliesome (Charlie Somerville), 02/10/2013 02:13 AM
feature-7816-v2.patch (3.41 KB) feature-7816-v2.patch charliesome (Charlie Somerville), 02/10/2013 10:13 PM
feature-7816-v3.patch (3.72 KB) feature-7816-v3.patch charliesome (Charlie Somerville), 02/16/2013 06:31 PM
Actions

Also available in: Atom PDF