Question

This question already has an answer here:

Has anyone tried automatic Java to C++ conversion for speed improvements? Is it a maintenance nightmare in the long run? Just read that is used to generate the HTML5 parsing engine in Gecko http://ejohn.org/blog/html-5-parsing/

Was it helpful?

Solution

In general, automatic conversions from one language to another will not be an improvement. Different languages have different idioms that affect performance.

The simplest example is with loops and variable creation. In a Java GC world, creating objects with new is almost free, and they dive into oblivion just as easily. In C++ memory allocation is (generally speaking) expensive:

// Sample java code
for ( int i = 0; i < 10000000; ++i )
{
   String str = new String( "hi" ); // new is free, GC is almost free for young objects
}

Direct conversion to C++ will result in bad performance (use of TR1 shared_ptr as memory handler instead of GC):

for ( int i = 0; i < 10000000; ++i )
{
   std::shared_ptr< std::string > str( new std::string( "hi" ) );
}

The equivalent loop written in C++ would be:

for ( int i = 0; i < 10000000; ++i )
{
   std::string str( "hi" );
}

Direct translation from a language to another usually ends with the worst of both worlds and harder to maintain code.

OTHER TIPS

The positive point to that conversion is that you will need a proper object oriented design in order to switch from java to C++ (paradigm intersection).

However some people say that coding C++ does not bring speed improvement compared to java code.

Even if that worked, I am not so sure that you will see much speed improvement. Java's Hotspot JIT compiler has become pretty good.

It's nearly impossible to replace the automatic memory-management of Java with a manual memory-management via a program. So you most likely will end up with a program, that has memory leaks or with C++-code that uses a garbage-collector. But a garbage-collector in Java have much more things to rely on (for instance no pointer-arithm,etics), so a garbage-collector in C++ to be safe it has a decreased performance. So your automatic conversion will most likely decrease performance.

Instead try to port it by hand to C++ or optimize the Java-code.

There is hardly a chance that this type of conversion shell lead to better performances. Usually when the JVM is working, it converts most of the code to native machine code. what you are suggesting is converting the Jave code to C++ and from there to native machine code, that is to add an extra phase. However there are some trivial cases in which some gain may be achived due to the fact that:

1) It takes some time to load the JVM from scratch.

2) It takes some time to do the Jit, when running a very short program, a lot of times, you may prefer to waste this time before running.

3) You may not got the same level of machine code on Java, if you are not running on Server mode. (On server Mode you are expected to get top notch machine code, and also one that is best suited for your own CPU as detected on run-time, that is usually lacking on most C/C++ implantations of programs, and moreover a machine code optimized at run-time)

The languages have such different styles in usage that a mindless conversion would be of little use. An intelligent converter would be next to imposable to write because of the different styles used.

Some Problem areas:

  • Resource allocation is controlled by 'try{} finally{}' blocks in Java while C++ uses RAII.
  • Java does exception checking at compile time C++ run-time.
  • Exceptions are handled differently. Two exceptions:
    • In Java (last throw is propagated)
    • In C++ application terminated.
  • Java has a massive standard library
    C++ has all the same functionality you just need to go find it on the web [which is a pain].
  • Java uses pointers for everything.
    • A straight unthinking conversion will leave you with a program consisting of just shared_ptr objects.

Anyway with JIT Compilation Java is comparable to C++ in speed.

Speaking of such converters in general, they can't be expected to produce either maintainable or high-performance code, since those generally require having an actual human who understands the language writing the code. What they are very useful for is in making it easy to port languages. Any language with a converter to C, for example, can be swiftly implemented on a wide range of languages. I used f2c in the mid-90s to run a Fortran routine on a Macintosh.

You may or may not have performance improvements if you rewrite the code in C++. You'll probably get a slowdown if you use an automatic converter.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top