4 Replies Latest reply: May 13, 2014 1:50 PM by jschellSomeoneStoleMyAlias RSS

    Emulating #ifdef in Java by final members

    user6766606

      I am doing calculation in Java that takes billions of iterations. Inside this inner iteration, there is lot of switches like:

       

      if (bool_setting_x) {

           do_something;

      }

       

      or:

       

      if (int_setting_y == CONST) {

           do_something_else;

      }

       

      All these settings (boolean and int) are final members of this class initialized in the constructor. And there are many switches (like 15). The thing is, they always have SOME value during whole hour-long calculation, so if JVM was very smart, it can consider all those members as #ifdef statements we know in C, and remove most of the inner loop, making everything MUCH faster.

       

      Perhaps we could help JVM by providing hint like this:

       

      class Foo {

       

           @IfdefHint

           int boolFoo;

       

           Foo(int inp) {

                boolFoo = inp;

           }

      }

       

      So, in cases like these, compiler would compile another instance of this function for each value of int it encounters (up to certain count of function-instances in memory, of course). Some sort of GC could also perhaps remove unused instances of function.

        • 1. Re: Emulating #ifdef in Java by final members
          user6766606

          name of int variable in the example above supposed to be iFoo instead of boolFoo, sorry for that.

          • 2. Re: Emulating #ifdef in Java by final members
            jschellSomeoneStoleMyAlias

            The Oracle compiler mostly relies on runtime optimizations.  It is quite possible that runtime optimizations are doing what you want although it probably depends on the specifics of the code.

             

            And just to note you can use a C/C++ compiler macros via command line switches to produce macro behavior in java if you want.  It just a matter of building a macro compilation step into your build process.

            • 3. Re: Emulating #ifdef in Java by final members
              user6766606

              Maybe some developer of JVM could enlight us if and how this is done currently. Point is, using annotation hint developer would mandate (to some extent) that he wants this "if" to go away, because it is inside inner loop that runs for hours. Also, this could be used for "zero-overhead" logging. For example: if you have logging level set to "Severe", calling log with "Info" level would be completely removed from code, something like this:

               

              class Logger {

              @IfdefHint 

              final int currentLoggingLevel;

               

              Logger(int lvl) {

                   currentLoggingLevel = lvl;

              }

               

              final public void log(int level, String message) { // This function exists in multiple copies, therefore for log() calls with lower level then currently set, it can be completely ignored in resulting code.

                   if (level < currentLoggingLevel) {

                        return;

                   }

                   ...

                   ...

              }

              • 4. Re: Emulating #ifdef in Java by final members
                jschellSomeoneStoleMyAlias

                I can only say that years ago (much slower system) it was suggested that we should remove logging to make the application faster.  Removing the logging had absolutely no impact.  Profiling an application is the only way to actually determine where bottlenecks are.

                 

                And after several decades of server side programming I would never want the production system set to 'severe' for logging anyways.  As the developer called on to solve production failures I need more information than just a single error message.

                 

                But as I said you can inject macro processing in your build if you want.  I know because I did exactly that years ago.