Uploaded image for project: 'MINA'
  1. MINA
  2. DIRMINA-529

use isPrimitive() instead of comparing the string to a list of primitives

Details

    • Improvement
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.0.0-M1
    • 2.0.0-M2
    • Core
    • None

    Description

      isPrimitive is a native function and should be used to determine whether a class is primitive instead of comparing to a list of known primitive class names

      Attachments

        1. mina-patch.txt
          2 kB
          Stefan Ackermann
        2. Primitive.java
          3 kB
          Trustin Lee
        3. Primitive.java
          3 kB
          Johannes Ulfkjær Jensen
        4. Primitive.java
          2 kB
          Edouard De Oliveira

        Activity

          Hi,

          I am not sure I am allowed to paste code into this comment field so I will try to describe another approach in words. Looking at the patch, it seems you can get the actual Class object for free, so why not just identify-compare that to the Class objects for the primitive types? You avoid the overhead of a native call, and being pure java, I think (pure speculation) that the JIT would have an easier time compiling it. This is obviously just an extreme mini optimization (compared to the patch) but I thought I would chime in regardless

          spand Johannes Ulfkjær Jensen added a comment - Hi, I am not sure I am allowed to paste code into this comment field so I will try to describe another approach in words. Looking at the patch, it seems you can get the actual Class object for free, so why not just identify-compare that to the Class objects for the primitive types? You avoid the overhead of a native call, and being pure java, I think (pure speculation) that the JIT would have an easier time compiling it. This is obviously just an extreme mini optimization (compared to the patch) but I thought I would chime in regardless
          trustin Trustin Lee added a comment -

          Thanks for the patch. It has been applied to the trunk.

          trustin Trustin Lee added a comment - Thanks for the patch. It has been applied to the trunk.
          trustin Trustin Lee added a comment -

          I'm not sure that isPrimitive() is slower than if - else if - ... block. Any benchmark report is appreciated.

          trustin Trustin Lee added a comment - I'm not sure that isPrimitive() is slower than if - else if - ... block. Any benchmark report is appreciated.

          Here's a simple benchmark between the two methods. Ran under jdk1.6.0_3, output is :

          Elapsed method 1 = 531
          Elapsed method 2 = 1297

          ted_kods Edouard De Oliveira added a comment - Here's a simple benchmark between the two methods. Ran under jdk1.6.0_3, output is : Elapsed method 1 = 531 Elapsed method 2 = 1297
          spand Johannes Ulfkjær Jensen added a comment - - edited

          I find it quite surprising that the existing method is actually better! and just to serve my own ego I extended the benchmark with my method. The results are:

          Elapsed method 1 = 178816721
          Elapsed method 2 = 571406491
          Elapsed method 3 = 136158715
          java.vm.version=1.6.0_02-b05
          java.vm.name=Java HotSpot(TM) 64-Bit Server VM
          os.name=Windows Vista
          true

          And given that it is a mini benchmark I tried to move them around a little and got this also:

          Elapsed method 3 = 76254315
          Elapsed method 1 = 190396475
          Elapsed method 2 = 604831410
          java.vm.version=1.6.0_02-b05
          java.vm.name=Java HotSpot(TM) 64-Bit Server VM
          os.name=Windows Vista
          true

          Not sure why this is the case. Any ideas?

          Will try to attach the extended and slightly altered benchmark.

          spand Johannes Ulfkjær Jensen added a comment - - edited I find it quite surprising that the existing method is actually better! and just to serve my own ego I extended the benchmark with my method. The results are: Elapsed method 1 = 178816721 Elapsed method 2 = 571406491 Elapsed method 3 = 136158715 java.vm.version=1.6.0_02-b05 java.vm.name=Java HotSpot(TM) 64-Bit Server VM os.name=Windows Vista true And given that it is a mini benchmark I tried to move them around a little and got this also: Elapsed method 3 = 76254315 Elapsed method 1 = 190396475 Elapsed method 2 = 604831410 java.vm.version=1.6.0_02-b05 java.vm.name=Java HotSpot(TM) 64-Bit Server VM os.name=Windows Vista true Not sure why this is the case. Any ideas? Will try to attach the extended and slightly altered benchmark.
          trustin Trustin Lee added a comment -

          I modified the Primitive.java you provided me, and got the following result:

          Elapsed method 1 = 35200
          Elapsed method 2 = 6
          Elapsed method 3 = 4168
          Elapsed method 1 = 35895
          Elapsed method 2 = 1
          Elapsed method 3 = 4128
          Elapsed method 1 = 29329
          Elapsed method 2 = 0
          Elapsed method 3 = 3730
          ...

          I also changed the order of each tests (i.e. 3->1->2), but didn't find any difference. So far, isPrimitive() seems to ourperform all alternatives.

          I'm on Linux with SUN JDK 1.6.0_05 Server VM.

          trustin Trustin Lee added a comment - I modified the Primitive.java you provided me, and got the following result: Elapsed method 1 = 35200 Elapsed method 2 = 6 Elapsed method 3 = 4168 Elapsed method 1 = 35895 Elapsed method 2 = 1 Elapsed method 3 = 4128 Elapsed method 1 = 29329 Elapsed method 2 = 0 Elapsed method 3 = 3730 ... I also changed the order of each tests (i.e. 3->1->2), but didn't find any difference. So far, isPrimitive() seems to ourperform all alternatives. I'm on Linux with SUN JDK 1.6.0_05 Server VM.

          People

            trustin Trustin Lee
            stivo Stefan Ackermann
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: