jruby/jruby — pull request #798 by charliesome (2013-06-11 11:27:42+00:00)
Title: Named based constant cache invalidation

Hi!

I've implemented named based constant cache invalidation in JRuby.

In my implementation, I've added a hashtable mapping constant names to `Invalidator` objects on the `Ruby` class. The lookup of the `Invalidator` object is cached in fields of certain classes - I've tried to make this threadsafe, but I'd like my approach verified because I've probably made a mistake somewhere.

I used this script to benchmark the performance improvement of this patch:

```ruby
require "benchmark"

class A
Foo = 123

class B
class C
class D
class E
def self.get_foo
Foo
end
end
end
end
end
end

Bar = 456

5.times do
puts Benchmark.measure {
1_000_000.times do
Object.send(:remove_const, :Bar)
Object.const_set(:Bar, 456)
A::B::C::D::E.get_foo
end
}
end
```

**ruby-2.0.0-p0**:

```
0.930000 0.000000 0.930000 ( 0.935408)
0.940000 0.000000 0.940000 ( 0.937916)
0.970000 0.000000 0.970000 ( 0.967160)
0.940000 0.000000 0.940000 ( 0.937358)
0.930000 0.000000 0.930000 ( 0.931662)
```

**JRuby master**:

```
1.250000 0.010000 1.260000 ( 0.865000)
0.700000 0.000000 0.700000 ( 0.670000)
0.700000 0.010000 0.710000 ( 0.686000)
0.670000 0.000000 0.670000 ( 0.677000)
0.680000 0.000000 0.680000 ( 0.679000)
```

**JRuby constant-name-cache**:

```
1.250000 0.010000 1.260000 ( 0.794000)
0.620000 0.000000 0.620000 ( 0.589000)
0.580000 0.010000 0.590000 ( 0.572000)
0.570000 0.000000 0.570000 ( 0.568000)
0.570000 0.010000 0.580000 ( 0.570000)
```

:sparkles: