Automatic Java to C ++ conversion

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

+2
source share
7 answers

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 looping and variable creation. In the Java GC world, creating objects with new ones is almost free, and they are just as easily immersed in oblivion. In C ++, memory allocation (generally speaking) is 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 lead to poor performance (using TR1 shared_ptr as a memory handler instead of GC):

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

An equivalent loop written in C ++ will look like this:

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

A direct translation from one language to another usually ends with the worst of both worlds and it is more difficult to maintain the code.

+17
source

The positive thing about this transformation is that you will need a suitable object-oriented design to move from java to C ++ (paradigm intersection).

However, some people say that C ++ coding does not improve speed compared to Java code.

0
source

Even if it worked, I’m not sure that you will see a significant improvement in speed. JIT Java compiler Hotspot JIT has become pretty good.

0
source

It is unlikely that this type of conversion shell will lead to better results. Usually, when the JVM is running, it will convert most of the code into native machine code. you suggest converting Jave code to C ++ and from there to native machine code, that is, add an additional phase. However, there are some trivial cases where some gain can be achieved due to the fact that:

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

2) It takes a lot of time to run Jit when you run a very short program, you may prefer to spend this time before starting.

3) You cannot get the same level of machine code in Java if you are not working in server mode. (In server mode, you expect to receive top-level machine code, as well as the one that is best suited for your own processor, as detected at runtime, which is usually not enough on most implementations of C / C ++ programs and, in addition, machine code optimized at runtime)

0
source

It is almost impossible to replace automatic Java memory management with manual memory management through a program. Thus, you will most likely end up with a program that has a memory leak or C ++ code that uses the garbage collector. But the garbage collector in Java has much more features that you can rely on (for example, without an arithmetic pointer, etics), so the C ++ garbage collector, to be safe, had reduced performance. Thus, your automatic conversion is likely to reduce performance.

Instead, try porting it manually to C ++ or optimize the Java code.

0
source

Languages ​​use such different styles that a meaningless transformation would be of little use. An intelligent converter will be next to the ability to write due to the use of different styles.

Some areas of concern:

  • Resource allocation is controlled by try {} finally {} 'blocks in Java, while C ++ uses RAII.
  • Java performs C ++ runtime exception checking at compile time.
  • Exceptions are handled differently. Two exceptions:
    • In Java (last cast distributed)
    • The C ++ application is complete.
  • Java has a massive standard library
    C ++ has all the same functionality that you just need to find on the Internet [this is a pain].
  • Java uses pointers for everything.
    • Direct mindless conversion will leave you with a program consisting only of shared_ptr objects.

In any case, with JIT Compilation, Java is comparable to C ++ in speed.

0
source

Speaking about such converters in general, they cannot be expected to contain either supported or high-performance code, since it usually requires the presence of an actual person who understands the language that writes the code. They are very useful for facilitating the transfer of languages. For example, any language with a C converter can be quickly implemented in a wide range of languages. I used f2c in the mid-90s to run the Fortran routine on a Macintosh.

You may or may not have performance improvements if you are rewriting C ++ code. You will probably get a slowdown if you use an automatic converter.

0
source

Source: https://habr.com/ru/post/1300077/


All Articles