Skip to content

Commit

Permalink
Avoid reflection when allocating P6opaque.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnthn committed Jun 11, 2013
1 parent b06dc41 commit 080f0bf
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
39 changes: 24 additions & 15 deletions src/vm/jvm/runtime/org/perl6/nqp/sixmodel/reprs/P6Opaque.java
Expand Up @@ -127,10 +127,14 @@ public void compose(ThreadContext tc, STable st, SixModelObject repr_info_hash)

/* Provided we have attributes, generate the JVM backing type. If not,
* P6OpaqueBaseInstance will do. */
if (attrInfoList.size() > 0)
if (attrInfoList.size() > 0) {
generateJVMType(tc, st, attrInfoList);
else
}
else {
((P6OpaqueREPRData)st.REPRData).jvmClass = P6OpaqueBaseInstance.class;
((P6OpaqueREPRData)st.REPRData).instance = new P6OpaqueBaseInstance();
((P6OpaqueREPRData)st.REPRData).instance.st = st;
}
}

/* Adds delegation, needed for mixin support. */
Expand Down Expand Up @@ -511,6 +515,13 @@ private void generateJVMType(ThreadContext tc, STable st, List<AttrInfo> attrInf
// } catch (IOException e) {
// }
((P6OpaqueREPRData)st.REPRData).jvmClass = new ByteClassLoader(classCompiled).findClass(className);
try {
((P6OpaqueREPRData)st.REPRData).instance = (P6OpaqueBaseInstance)((P6OpaqueREPRData)st.REPRData).jvmClass.newInstance();
}
catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
((P6OpaqueREPRData)st.REPRData).instance.st = st;
}

private void generateDelegateMethod(ThreadContext tc, ClassWriter cw, String className, String field, String methodName) {
Expand All @@ -524,15 +535,7 @@ private void generateDelegateMethod(ThreadContext tc, ClassWriter cw, String cla
}

public SixModelObject allocate(ThreadContext tc, STable st) {
try {
P6OpaqueBaseInstance obj = (P6OpaqueBaseInstance)((P6OpaqueREPRData)st.REPRData).jvmClass.newInstance();
obj.st = st;
return obj;
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return ((P6OpaqueREPRData)st.REPRData).instance.instClone();
}

public void change_type(ThreadContext tc, SixModelObject obj, SixModelObject newType) {
Expand Down Expand Up @@ -705,7 +708,14 @@ else if (nameToHintObject instanceof VMHashInstance) {
info.hasAutoVivContainer = REPRData.autoVivContainers[i] != null;
attrInfoList.add(info);
}
generateJVMType(tc, st, attrInfoList);
if (numAttributes > 0) {
generateJVMType(tc, st, attrInfoList);
}
else {
((P6OpaqueREPRData)st.REPRData).jvmClass = P6OpaqueBaseInstance.class;
((P6OpaqueREPRData)st.REPRData).instance = new P6OpaqueBaseInstance();
((P6OpaqueREPRData)st.REPRData).instance.st = st;
}
}

public void serialize_repr_data(ThreadContext tc, STable st, SerializationWriter writer) {
Expand Down Expand Up @@ -763,8 +773,7 @@ public void deserialize_finish(ThreadContext tc, STable st,
SerializationReader reader, SixModelObject stub) {
try {
// Create instance that we'll deserialize into.
P6OpaqueBaseInstance obj = (P6OpaqueBaseInstance)((P6OpaqueREPRData)st.REPRData).jvmClass.newInstance();
obj.st = st;
P6OpaqueBaseInstance obj = (P6OpaqueBaseInstance)((P6OpaqueREPRData)st.REPRData).instance.instClone();

// Install it as the stub's delegate.
((P6OpaqueDelegateInstance)stub).delegate = obj;
Expand All @@ -781,7 +790,7 @@ public void deserialize_finish(ThreadContext tc, STable st,
}
}
}
catch (IllegalAccessException | NoSuchFieldException | InstantiationException e)
catch (IllegalAccessException | NoSuchFieldException e)
{
throw new RuntimeException(e);
}
Expand Down
Expand Up @@ -45,6 +45,14 @@ public SixModelObject clone(ThreadContext tc) {
}
}

public SixModelObject instClone() {
try {
return (SixModelObject)this.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}

public void badNative() {
throw new RuntimeException("Cannot access a reference attribute as a native attribute");
}
Expand Down
Expand Up @@ -12,6 +12,11 @@ public class P6OpaqueREPRData {
*/
public Class<?> jvmClass;

/**
* Instance of jvmClass, cloned per allocation.
*/
public P6OpaqueBaseInstance instance;

/**
* List of class handles that have attributes in this type.
*/
Expand Down

0 comments on commit 080f0bf

Please sign in to comment.