Skip to content

Commit 6ee4813

Browse files
committed
Only use a single ByteClassLoader, attached to the GlobalContext.
In a few cases (Java interop), we still have to create additional ByteClassLoaders, but this should cut down memory use a bit.
1 parent 523a33f commit 6ee4813

File tree

7 files changed

+26
-29
lines changed

7 files changed

+26
-29
lines changed

src/vm/jvm/runtime/org/perl6/nqp/runtime/BootJavaInterop.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,13 @@ protected void finishClass(ClassContext cc) {
222222
//} catch (java.io.IOException e) {
223223
// e.printStackTrace();
224224
//}
225-
cc.constructed = (cc.target == null
226-
? new ByteClassLoader(bits)
227-
: new ByteClassLoader(bits, cc.target.getClassLoader())
228-
).findClass(cc.className.replace('/','.'));
225+
// XXX: The condition here can probably cut down a few more
226+
// allocations if we check if the target's class loader isn't in the
227+
// chain of loaders above gc.byteClassLoader.
228+
ByteClassLoader loader = cc.target == null
229+
? gc.byteClassLoader
230+
: new ByteClassLoader(cc.target.getClassLoader());
231+
cc.constructed = loader.defineClass(cc.className.replace('/','.'), bits);
229232
try {
230233
cc.constructed.getField("constants").set(null, cc.constants.toArray(new Object[0]));
231234
} catch (ReflectiveOperationException roe) {
Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package org.perl6.nqp.runtime;
22

33
public class ByteClassLoader extends ClassLoader {
4-
private byte[] bytes;
5-
6-
public ByteClassLoader(byte[] bytes) {
7-
this.bytes = bytes;
4+
public ByteClassLoader() {
5+
super();
86
}
9-
10-
public ByteClassLoader(byte[] bytes, ClassLoader parent) {
7+
8+
public ByteClassLoader(ClassLoader parent) {
119
super(parent);
12-
this.bytes = bytes;
1310
}
14-
15-
public Class<?> findClass(String name) {
16-
return defineClass(name, this.bytes, 0, this.bytes.length);
11+
12+
public Class<?> defineClass(String name, byte[] bytes) {
13+
return defineClass(name, bytes, 0, bytes.length);
1714
}
1815
}

src/vm/jvm/runtime/org/perl6/nqp/runtime/GlobalContext.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ public class GlobalContext {
168168
*/
169169
public boolean noisyExceptions;
170170

171+
/**
172+
* The global ByteClassLoader instance, used to load classes generated at
173+
* runtime.
174+
*/
175+
public ByteClassLoader byteClassLoader;
176+
171177
/** Redirected output for eval-server. */
172178
public PrintStream out = System.out;
173179
/** Redirected error for eval-server. */
@@ -231,6 +237,8 @@ public GlobalContext()
231237

232238
hllGlobalAll = new HashMap<ContextKey<?,?>, Object>();
233239
hllGlobalAllLock = new Object();
240+
241+
byteClassLoader = new ByteClassLoader();
234242
}
235243

236244
/**

src/vm/jvm/runtime/org/perl6/nqp/runtime/Ops.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5202,8 +5202,8 @@ public static SixModelObject compilejastlinestofile(SixModelObject dump, String
52025202
public static SixModelObject loadcompunit(SixModelObject obj, long compileeHLL, ThreadContext tc) {
52035203
try {
52045204
EvalResult res = (EvalResult)obj;
5205-
ByteClassLoader cl = new ByteClassLoader(res.jc.bytes);
5206-
res.cu = (CompilationUnit)cl.findClass(res.jc.name).newInstance();
5205+
Class<?> cuClass = tc.gc.byteClassLoader.defineClass(res.jc.name, res.jc.bytes);
5206+
res.cu = (CompilationUnit) cuClass.newInstance();
52075207
if (compileeHLL != 0)
52085208
usecompileehllconfig(tc);
52095209
res.cu.initializeCompilationUnit(tc);

src/vm/jvm/runtime/org/perl6/nqp/sixmodel/ByteClassLoader.java

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/vm/jvm/runtime/org/perl6/nqp/sixmodel/reprs/CStruct.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import org.objectweb.asm.Opcodes;
1212
import org.objectweb.asm.Type;
1313

14-
import org.perl6.nqp.sixmodel.ByteClassLoader;
1514
import org.perl6.nqp.sixmodel.REPR;
1615
import org.perl6.nqp.sixmodel.SerializationReader;
1716
import org.perl6.nqp.sixmodel.SixModelObject;
@@ -103,7 +102,6 @@ public void deserialize_finish(ThreadContext tc, STable st, SerializationReader
103102
}
104103

105104
private static long typeId = 0;
106-
private static ByteClassLoader loader = new ByteClassLoader();
107105
private void generateStructClass(ThreadContext tc, STable st, List<AttrInfo> fields) {
108106
CStructREPRData reprData = (CStructREPRData) st.REPRData;
109107
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES);
@@ -164,7 +162,7 @@ private void generateStructClass(ThreadContext tc, STable st, List<AttrInfo> fie
164162

165163
cw.visitEnd();
166164
byte[] compiled = cw.toByteArray();
167-
reprData.structureClass = loader.defineClass(className, compiled);
165+
reprData.structureClass = tc.gc.byteClassLoader.defineClass(className, compiled);
168166
}
169167

170168
private String typeDescriptor(ThreadContext tc, AttrInfo info) {

src/vm/jvm/runtime/org/perl6/nqp/sixmodel/reprs/P6Opaque.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import org.objectweb.asm.Type;
1515
import org.perl6.nqp.runtime.ExceptionHandling;
1616
import org.perl6.nqp.runtime.ThreadContext;
17-
import org.perl6.nqp.sixmodel.ByteClassLoader;
1817
import org.perl6.nqp.sixmodel.REPR;
1918
import org.perl6.nqp.sixmodel.STable;
2019
import org.perl6.nqp.sixmodel.SerializationReader;
@@ -25,7 +24,6 @@
2524

2625
public class P6Opaque extends REPR {
2726
private static long typeId = 0;
28-
private static ByteClassLoader loader = new ByteClassLoader();
2927

3028
private static class AttrInfo {
3129
public STable st;
@@ -625,7 +623,7 @@ private Class<?> generateJVMClass(ThreadContext tc, List<AttrInfo> attrInfoList)
625623
// fos.close();
626624
// } catch (IOException e) {
627625
// }
628-
return loader.defineClass(className, classCompiled);
626+
return tc.gc.byteClassLoader.defineClass(className, classCompiled);
629627
}
630628

631629
private void generateDelegateMethod(ThreadContext tc, ClassWriter cw, String className, String field, String methodName) {

0 commit comments

Comments
 (0)