Skip to content

Instantly share code, notes, and snippets.

@headius
Created November 17, 2011 07:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save headius/1372628 to your computer and use it in GitHub Desktop.
Save headius/1372628 to your computer and use it in GitHub Desktop.
diff --git a/src/org/jruby/RubyModule.java b/src/org/jruby/RubyModule.java
index c5f6643..33cc9d4 100644
--- a/src/org/jruby/RubyModule.java
+++ b/src/org/jruby/RubyModule.java
@@ -1582,7 +1582,7 @@ public class RubyModule extends RubyObject {
if (getBaseName() == null) {
return RubyString.newEmptyString(runtime);
} else {
- return runtime.newString(getName());
+ return getRubyName(runtime).dup();
}
}
@@ -1592,9 +1592,20 @@ public class RubyModule extends RubyObject {
if (getBaseName() == null) {
return runtime.getNil();
} else {
- return runtime.newString(getName());
+ return getRubyName(runtime).dup();
}
}
+
+ private IRubyObject getRubyName(Ruby runtime) {
+ ConstantCache cachedName = this.cachedName;
+ Object data = runtime.getConstantInvalidator().getData();
+ if (cachedName.key == data) {
+ return cachedName.value;
+ }
+ IRubyObject name = runtime.newString(getName());
+ this.cachedName = new ConstantCache(data, name);
+ return name;
+ }
protected IRubyObject cloneMethods(RubyModule clone) {
RubyModule realType = this.getNonIncludedClass();
@@ -3459,6 +3470,20 @@ public class RubyModule extends RubyObject {
*/
protected String baseName;
+ private static class ConstantCache {
+ public final Object key;
+ public final IRubyObject value;
+ public static final ConstantCache NULL = new ConstantCache(null, null);
+ public ConstantCache(Object key, IRubyObject value) {
+ this.key = key;
+ this.value = value;
+ }
+ }
+ /**
+ * A name cache for class names, to avoid the cost of recalculating them
+ */
+ private ConstantCache cachedName = ConstantCache.NULL;
+
/**
* The cached anonymous class name, since it never changes and has a nonzero
* cost to calculate.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment