Skip to content

Commit

Permalink
JRUBY-1491, MethodCache should use weakrefs
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.codehaus.org/jruby/trunk/jruby@4806 961051c9-f516-0410-bf72-c9f7e237a7b7
  • Loading branch information
headius committed Oct 30, 2007
1 parent cb80fab commit 48ddca7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/org/jruby/RubyModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,8 @@ private DynamicMethod searchMethodWithoutCache(String name) {
public DynamicMethod searchMethod(String name) {
MethodCache cache = getRuntime().getMethodCache();
MethodCache.CacheEntry entry = cache.getMethod(this, name);
if (entry.klass == this && name.equals(entry.methodName)) {
return entry.method;
if (entry.klass.get() == this && name.equals(entry.methodName)) {
return entry.method.get();
}

for (RubyModule searchModule = this; searchModule != null; searchModule = searchModule.getSuperClass()) {
Expand Down
21 changes: 12 additions & 9 deletions src/org/jruby/util/MethodCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,30 @@

package org.jruby.util;

import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import org.jruby.RubyClass;
import org.jruby.RubyModule;
import org.jruby.internal.runtime.methods.DynamicMethod;

public class MethodCache {

public static class CacheEntry {
public RubyModule klass = null;
private static final Reference NULL_REFERENCE = new WeakReference(null);
public Reference<RubyModule> klass = NULL_REFERENCE;
public String methodName = null;
public DynamicMethod method = null;
public Reference<DynamicMethod> method = NULL_REFERENCE;

void put(RubyModule klass, String methodName, DynamicMethod method) {
this.klass = klass;
this.klass = new WeakReference(klass);
this.methodName = methodName;
this.method = method;
this.method = new WeakReference(method);
}

void clear() {
this.klass = null;
this.klass = NULL_REFERENCE;
this.methodName = null;
this.method = null;
this.method = NULL_REFERENCE;
}
}

Expand Down Expand Up @@ -76,7 +79,7 @@ public void clearCacheForModule(RubyModule module) {
return;
}
for (int i = 0; i < CACHE_SIZE; i++ ) {
if (cache[i].klass == module) {
if (cache[i].klass.get() == module) {
cache[i].clear();
}
}
Expand All @@ -97,7 +100,7 @@ public void removeMethod(RubyClass c, String id) {

for (int i = CACHE_SIZE; --i >= 0; ) {
CacheEntry entry = cache[i];
if (c == entry.klass && id.equals(entry.methodName)) {
if (c == entry.klass.get() && id.equals(entry.methodName)) {
entry.methodName = null;
}
}
Expand All @@ -123,7 +126,7 @@ public void removeClass(RubyClass klass) {

for (int i = CACHE_SIZE; --i >= 0; ) {
CacheEntry entry = cache[i];
if (entry.klass == klass) {
if (entry.klass.get() == klass) {
entry.methodName = null;
}
}
Expand Down

0 comments on commit 48ddca7

Please sign in to comment.