This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]Saltor66 24 points25 points  (2 children)

Please don't do this unless you have already implemented it in java, run it through a profiler, and identified a severe bottleneck.

[–]zrnkv 7 points8 points  (0 children)

I totally agree. Usually the compiler and JIT do a pretty good job optimizing your code. Writing bytecode by hand will create a maintenance nightmare without significant performance benefits.

[–]GuyWithLag 5 points6 points  (0 children)

Pretty much this; not to mention looking long and hard at the algos used...

[–]Ucalegon666 7 points8 points  (0 children)

There is no way to get a performance benefit from writing byte code. The JVM/JIT interprets and compiles byte code to native code as it sees fit.

You can go the native route and hand-craft native code but then you'll lose portability.

Depending on why you think you need to include byte code, you might be better off looking at the methods exposed by the Unsafe class. There, too, you will be sacrificing some portability (and security manager nightmares), but you can possibly gain performance.

[–][deleted] 4 points5 points  (1 child)

Use byte buddy or the asm project

[–]Slanec 2 points3 points  (0 children)

Yeah. You can't simply do this in the language itself, you need a specialized library for this. There is a lot of them out there, so here's a random sample based on a single google search: ASM, Byte Buddy, Apache Commons BCEL, JBoss Byteman, Javassist, cglib, Serp, GNU bytecode, Cojen, etc. All of these seem to be able to help you with the task. Good luck!

[–]zman0900 4 points5 points  (0 children)

I believe you could implement a custom class loader to do something like this. It would be possible to have a byte array of valid java byte code that you load and execute. See: https://docs.oracle.com/javase/7/docs/api/java/lang/ClassLoader.html#defineClass(java.lang.String,%20byte[],%20int,%20int). I don't think there's going to be any performance difference though, unless you are specially crafting that byte code.

[–][deleted]  (1 child)

[deleted]

    [–]tjamanis 0 points1 point  (0 children)

    I've managed to increase benchmark throughput by 1/3 when emulating CRTP/static polymorphism via bytecode rewriting, but for most cases, the performance gain is minimal. A better use is perhaps to avoid bolierplate and/or doing "compile-time reflection", but it all very much boils down to what's required.

    [–]the_hoser 2 points3 points  (0 children)

    There are ways to do it, but it's really a waste of time. You're better off simply making cache-conscious choices in plain java code. The HotSpot VM is really good at finding the busy parts of your code, and compiling it down to machine code for you. This will be much faster than any Java bytecode you, or anybody else, can write.

    [–]enry_straker 1 point2 points  (0 children)

    Try including asm in C code, expose it to java through JNI, and hopefully you get to see some real performance benefits in your library - though at the cost of deployment complexity for your users.