Search in sources :

Example 1 with ClassSet

use of org.apache.drill.exec.compile.ClassTransformer.ClassSet in project drill by axbaretto.

the class TestClassTransformation method testCompilationNoDebug.

@Test
public void testCompilationNoDebug() throws CompileException, ClassNotFoundException, ClassTransformationException, IOException {
    CodeGenerator<ExampleInner> cg = newCodeGenerator(ExampleInner.class, ExampleTemplateWithInner.class);
    ClassSet classSet = new ClassSet(null, cg.getDefinition().getTemplateClassName(), cg.getMaterializedClassName());
    String sourceCode = cg.generateAndGet();
    sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_OPTION, ClassCompilerSelector.CompilerPolicy.JDK.name());
    sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_DEBUG_OPTION, false);
    @SuppressWarnings("resource") QueryClassLoader loader = new QueryClassLoader(config, sessionOptions);
    final byte[][] codeWithoutDebug = loader.getClassByteCode(classSet.generated, sourceCode);
    loader.close();
    int sizeWithoutDebug = 0;
    for (byte[] bs : codeWithoutDebug) {
        sizeWithoutDebug += bs.length;
    }
    sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_DEBUG_OPTION, true);
    loader = new QueryClassLoader(config, sessionOptions);
    final byte[][] codeWithDebug = loader.getClassByteCode(classSet.generated, sourceCode);
    loader.close();
    int sizeWithDebug = 0;
    for (byte[] bs : codeWithDebug) {
        sizeWithDebug += bs.length;
    }
    Assert.assertTrue("Debug code is smaller than optimized code!!!", sizeWithDebug > sizeWithoutDebug);
    logger.debug("Optimized code is {}% smaller than debug code.", (int) ((sizeWithDebug - sizeWithoutDebug) / (double) sizeWithDebug * 100));
}
Also used : ClassSet(org.apache.drill.exec.compile.ClassTransformer.ClassSet) Test(org.junit.Test)

Example 2 with ClassSet

use of org.apache.drill.exec.compile.ClassTransformer.ClassSet in project drill by apache.

the class TestClassTransformation method testCompilationNoDebug.

@Test
public void testCompilationNoDebug() throws CompileException, ClassNotFoundException, ClassTransformationException, IOException {
    CodeGenerator<ExampleInner> cg = newCodeGenerator(ExampleInner.class, ExampleTemplateWithInner.class);
    ClassSet classSet = new ClassSet(null, cg.getDefinition().getTemplateClassName(), cg.getMaterializedClassName());
    String sourceCode = cg.generateAndGet();
    sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_OPTION, ClassCompilerSelector.CompilerPolicy.JDK.name());
    sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_DEBUG_OPTION, false);
    QueryClassLoader loader = new QueryClassLoader(config, sessionOptions);
    byte[][] codeWithoutDebug = loader.getClassByteCode(classSet.generated, sourceCode);
    loader.close();
    int sizeWithoutDebug = 0;
    for (byte[] bs : codeWithoutDebug) {
        sizeWithoutDebug += bs.length;
    }
    sessionOptions.setLocalOption(ClassCompilerSelector.JAVA_COMPILER_DEBUG_OPTION, true);
    loader = new QueryClassLoader(config, sessionOptions);
    byte[][] codeWithDebug = loader.getClassByteCode(classSet.generated, sourceCode);
    loader.close();
    int sizeWithDebug = 0;
    for (byte[] bs : codeWithDebug) {
        sizeWithDebug += bs.length;
    }
    Assert.assertTrue("Debug code is smaller than optimized code!!!", sizeWithDebug > sizeWithoutDebug);
    logger.debug("Optimized code is {}% smaller than debug code.", (int) ((sizeWithDebug - sizeWithoutDebug) / (double) sizeWithDebug * 100));
}
Also used : ClassSet(org.apache.drill.exec.compile.ClassTransformer.ClassSet) Test(org.junit.Test)

Example 3 with ClassSet

use of org.apache.drill.exec.compile.ClassTransformer.ClassSet in project drill by apache.

the class MergeAdapter method visitEnd.

@Override
public void visitEnd() {
    // add all the fields of the class we're going to merge.
    // Special handling for nested classes. Drill uses non-static nested
    // "inner" classes in some templates. Prior versions of Drill would
    // create the generated nested classes as static, then this line
    // would copy the "this$0" field to convert the static nested class
    // into a non-static inner class. However, that approach is not
    // compatible with plain-old Java compilation. Now, Drill generates
    // the nested classes as non-static inner classes. As a result, we
    // do not want to copy the hidden fields; we'll end up with two if
    // we do.
    classToMerge.fields.stream().filter(field -> !field.name.startsWith("this$")).forEach(field -> field.accept(this));
    // add all the methods that we to include.
    for (MethodNode mn : classToMerge.methods) {
        if (mn.name.equals("<init>")) {
            continue;
        }
        String[] exceptions = new String[mn.exceptions.size()];
        mn.exceptions.toArray(exceptions);
        MethodVisitor mv = cv.visitMethod(mn.access | Modifier.FINAL, mn.name, mn.desc, mn.signature, exceptions);
        if (verifyBytecode) {
            mv = new CheckMethodVisitorFsm(api, mv);
        }
        mn.instructions.resetLabels();
        // mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new
        // SimpleRemapper("org.apache.drill.exec.compile.ExampleTemplate", "Bunky")));
        ClassSet top = set;
        while (top.parent != null) {
            top = top.parent;
        }
        mn.accept(new MethodRemapper(mv, new SimpleRemapper(top.precompiled.slash, top.generated.slash)));
    }
    super.visitEnd();
}
Also used : ClassWriter(org.objectweb.asm.ClassWriter) MethodVisitor(org.objectweb.asm.MethodVisitor) Remapper(org.objectweb.asm.commons.Remapper) Logger(org.slf4j.Logger) Collection(java.util.Collection) FieldVisitor(org.objectweb.asm.FieldVisitor) ClassSet(org.apache.drill.exec.compile.ClassTransformer.ClassSet) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) SignatureHolder(org.apache.drill.exec.compile.sig.SignatureHolder) HashSet(java.util.HashSet) MethodNode(org.objectweb.asm.tree.MethodNode) ValueHolderReplacementVisitor(org.apache.drill.exec.compile.bytecode.ValueHolderReplacementVisitor) ClassReader(org.objectweb.asm.ClassReader) Modifier(java.lang.reflect.Modifier) MethodRemapper(org.objectweb.asm.commons.MethodRemapper) SimpleRemapper(org.objectweb.asm.commons.SimpleRemapper) ClassNode(org.objectweb.asm.tree.ClassNode) ClassVisitor(org.objectweb.asm.ClassVisitor) ClassRemapper(org.objectweb.asm.commons.ClassRemapper) SimpleRemapper(org.objectweb.asm.commons.SimpleRemapper) MethodNode(org.objectweb.asm.tree.MethodNode) ClassSet(org.apache.drill.exec.compile.ClassTransformer.ClassSet) MethodRemapper(org.objectweb.asm.commons.MethodRemapper) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 4 with ClassSet

use of org.apache.drill.exec.compile.ClassTransformer.ClassSet in project drill by axbaretto.

the class MergeAdapter method visitEnd.

@Override
public void visitEnd() {
    // add all the fields of the class we're going to merge.
    for (Iterator<?> it = classToMerge.fields.iterator(); it.hasNext(); ) {
        // Special handling for nested classes. Drill uses non-static nested
        // "inner" classes in some templates. Prior versions of Drill would
        // create the generated nested classes as static, then this line
        // would copy the "this$0" field to convert the static nested class
        // into a non-static inner class. However, that approach is not
        // compatible with plain-old Java compilation. Now, Drill generates
        // the nested classes as non-static inner classes. As a result, we
        // do not want to copy the hidden fields; we'll end up with two if
        // we do.
        FieldNode field = (FieldNode) it.next();
        if (!field.name.startsWith("this$")) {
            field.accept(this);
        }
    }
    // add all the methods that we to include.
    for (Iterator<?> it = classToMerge.methods.iterator(); it.hasNext(); ) {
        MethodNode mn = (MethodNode) it.next();
        if (mn.name.equals("<init>")) {
            continue;
        }
        String[] exceptions = new String[mn.exceptions.size()];
        mn.exceptions.toArray(exceptions);
        MethodVisitor mv = cv.visitMethod(mn.access | Modifier.FINAL, mn.name, mn.desc, mn.signature, exceptions);
        if (verifyBytecode) {
            mv = new CheckMethodVisitorFsm(api, mv);
        }
        mn.instructions.resetLabels();
        // mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new
        // SimpleRemapper("org.apache.drill.exec.compile.ExampleTemplate", "Bunky")));
        ClassSet top = set;
        while (top.parent != null) {
            top = top.parent;
        }
        mn.accept(new RemappingMethodAdapter(mn.access, mn.desc, mv, new SimpleRemapper(top.precompiled.slash, top.generated.slash)));
    }
    super.visitEnd();
}
Also used : SimpleRemapper(org.objectweb.asm.commons.SimpleRemapper) FieldNode(org.objectweb.asm.tree.FieldNode) MethodNode(org.objectweb.asm.tree.MethodNode) ClassSet(org.apache.drill.exec.compile.ClassTransformer.ClassSet) RemappingMethodAdapter(org.objectweb.asm.commons.RemappingMethodAdapter) MethodVisitor(org.objectweb.asm.MethodVisitor)

Aggregations

ClassSet (org.apache.drill.exec.compile.ClassTransformer.ClassSet)4 Test (org.junit.Test)2 MethodVisitor (org.objectweb.asm.MethodVisitor)2 SimpleRemapper (org.objectweb.asm.commons.SimpleRemapper)2 MethodNode (org.objectweb.asm.tree.MethodNode)2 Modifier (java.lang.reflect.Modifier)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ValueHolderReplacementVisitor (org.apache.drill.exec.compile.bytecode.ValueHolderReplacementVisitor)1 SignatureHolder (org.apache.drill.exec.compile.sig.SignatureHolder)1 ClassReader (org.objectweb.asm.ClassReader)1 ClassVisitor (org.objectweb.asm.ClassVisitor)1 ClassWriter (org.objectweb.asm.ClassWriter)1 FieldVisitor (org.objectweb.asm.FieldVisitor)1 ClassRemapper (org.objectweb.asm.commons.ClassRemapper)1 MethodRemapper (org.objectweb.asm.commons.MethodRemapper)1 Remapper (org.objectweb.asm.commons.Remapper)1 RemappingMethodAdapter (org.objectweb.asm.commons.RemappingMethodAdapter)1 ClassNode (org.objectweb.asm.tree.ClassNode)1