Automatic Java to C++ conversion [duplicate]
Asked Answered
M

7

2

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/

Melina answered 8/7, 2009 at 7:33 Comment(0)
D
18

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.

Delilahdelimit answered 8/7, 2009 at 7:33 Comment(3)
so your saying that people that dont know java or c++ should not be using converter, because its written by people that dont know java and c++ ? or that you cant fix it in converter, because it must look exactly the same in c++?Enrichment
@01: As far as I understand this post: Writing a good converter is not the conversion itself, but optimization or converted code. I hardly doubt that a converter will perform any optimizations at all. The problem here is not that people who write converter don't know java or c++, but the conversion from garbage collected language to non-garbage collected language => you can't decide without optimization and object usage tracking if the object should be stack or heap allocated. And there are much more such pitfalls, like e.g. java uses 2-byte charactes by default, C++ single byte charactes...Zaragoza
So there the converter must end up with compromises => these are usually not always the best performance kicks! When should be a java method converted to C++ as virtual an when as not? Only when it is declared as final? I think we can come up with thousands of such questions for converter and that will result in an optimizing converter with pretty much logic behind.Zaragoza
O
0

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.

Odawa answered 8/7, 2009 at 7:33 Comment(0)
C
0

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.

Callisto answered 8/7, 2009 at 7:33 Comment(0)
P
0

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.

Prorate answered 8/7, 2009 at 7:33 Comment(3)
You can get C++ language systems with a garbage collector.Karynkaryo
Yes, but as I said a C++-garbage-collector has more things to check. For example pointer-arithmetics make things more difficult. So C++-garbage-collectors are usually not as efficient as the generational garbage-collector of Java and .Net.Prorate
Using shared_ptr<> for everything will be a performance hit, and using a conservative GC will probably be slow, and will allow some memory leaks.Odawa
P
0

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)

Pastis answered 8/7, 2009 at 7:33 Comment(0)
S
0

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.

Sweetheart answered 8/7, 2009 at 7:33 Comment(0)
E
0

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.

Extended answered 8/7, 2009 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.