
JavaCPP - marvel_boy
https://github.com/bytedeco/javacpp
======
amelius
Somewhat off-topic. I recently got interested in Java and its ecosystem
because of Clojure. However, what bugs me is the startup time for Java. Since
this is mature technology, I cannot believe it takes so long for an executable
to start. One guess is that this is because of the garbage collector inside
the VM, but that doesn't make much sense because NodeJS also uses a garbage
collector and it starts instantly (and it even has to parse & compile the code
upon startup, which the Java VM doesn't even have to do). So... I'm puzzled.
Any comments? This is not meant as a trolling question.

~~~
chrisseaton
It doesn't make any sense to blame the garbage collector. During startup
you're loading classes, allocating structures, and verifying byte code. There
aren't things that involve garbage collection (allocating with a tracing GC is
generally by bump-pointer so is very efficient).

I work on a large Java project (JRuby) and for us the largest component of
time spent is on loading classes. Also, startup code is likely to run in the
interpreter rather than being JIT compiled, which is also slower.

The solution we're looking at is ahead of time compilation. This avoids class
loading and interpretation, and it does seem to solve the problem (we're back
to ms startup time).

In V8 (Node.js) the majority of the core language and library features are
effectively already AOT compiled as they're implemented in C++. In the OpenJDK
(and most JVMs) a lot of this is implemented in Java, so that has to be loaded
and interpreted rather than executed natively.

~~~
amelius
By AOT, do you mean something like this:
[http://www.excelsiorjet.com/](http://www.excelsiorjet.com/)

If you look at the "Benefits" tab, you will see that startup will still take
on the order of 1+ seconds, which is quite long IMO.

Also, would this work with Clojure? And are there open-source AOT compilers
which work with Clojure?

~~~
the8472
Afaik IBM's JVM supports class data sharing, which effectively provides AOT
behavior once they have been JITed.

For oracle's VM they're working on a commercial feature
[https://www.youtube.com/watch?v=Xybzyv8qbOc](https://www.youtube.com/watch?v=Xybzyv8qbOc)

There also are things like nailgun which simply prestart a JVM and then
delegate to the loaded JVM when you invoke some command. It may be a bit of a
workaround, but if your use-case is to frequently start extremely shortlived
programs that might be worth it.

~~~
Alphasite_
Doesn't Dalvik use a machinist similar to railgun to achieve its performance?

~~~
bitmapbrother
Dalvik is no more. ART uses AOT.

------
agibsonccc
JavaCPP user here. Amazing framework.

The ideas behind it are really cool (auto generate JNI headers with
annotations).

Ensure you checkout the presets: [https://github.com/bytedeco/javacpp-
presets](https://github.com/bytedeco/javacpp-presets)

Another awesome thing built with it:

[https://github.com/WPIRoboticsProjects/GRIP](https://github.com/WPIRoboticsProjects/GRIP)

~~~
jevinskie
I took a look at the LLVM preset and it looked like you had to pass object
handles to static functions. I'd prefer to have an actual, say, LLVM::Module
equivalent object in Java that you can call member functions on. Are all the
presets like this?

~~~
bytedeco
No, that's because in the specific case of LLVM only the C API is currently
mapped. We'd have to work a little bit harder for the C++ API, but there is
interest in doing so, if only to use it as a better parser for JavaCPP itself:
Improve Parser: Use the Clang API
[https://github.com/bytedeco/javacpp/issues/51](https://github.com/bytedeco/javacpp/issues/51)

------
corysama
Any recommendations for going the other way? I'm interested in writing Android
applications entirely in C++, but obviously a lot of interaction with Java is
required. It would be great if there was a way to use JNI well enough to never
be tempted to write little bits of Java here and there.

~~~
bytedeco
I recommend Jace for that:
[https://bitbucket.org/cowwoc/jace/wiki/Home](https://bitbucket.org/cowwoc/jace/wiki/Home)

------
andrewguenther
For those of you looking at something like this, remember that JNI (no matter
how you wrap it) is still _incredibly_ slow when trying to pass larger data
structures (>1KB).

~~~
bytedeco
On modern JVM we can use direct NIO buffers and let Java access native memory
directly at full speed. There is (almost) no overhead at all anymore, so if we
do it that way, it's pretty fast actually.

------
jesuslop
Bookmarked, looks useful and leaner than swig and any other thing I know of.

------
ww520
Looks very useful. It would help to bridge to many C++ packages. I wonder what
the deployment option is. Can it bundle multiple platform libraries (e.g.
Windows and Linux X86) in one package?

~~~
agibsonccc
Yes. You can have a custom shell script that sets up the deployment. It's not
ideal but it works in practice:
[http://search.maven.org/#search%7Cga%7C1%7Cjavacpp](http://search.maven.org/#search%7Cga%7C1%7Cjavacpp)

