Wednesday, 7 March 2012

JDK8 Warnings Cleanup Day on Dec 1st, 2011

JDK8 Warnings Cleanup Day on Dec 1st, 2011

In the previous blog I had posted the details about how to configure your virtual box for building Open JDK.

London Java Community had planned to participate on the global JDK8 Warnings Cleanup Day on Dec 1st, 2011. Michael Barker of the LJC had planned a session at Skills matter eXchange, 10 of the members had signed up for the event.

Michael had earlier advised the instructions, i tried to set the machine before I had gone for the event, :( unfortunately I was not able to setup my machine. Luckily, the next day Michael and Martijn  were able to provide us with full setup VM.

    I had paired with one another person Mani (he came without a laptop). We all were at skills matter by 6 30 PM, the machine set up took the most of the time.

    At about 7 30, we had the system up with the setting and had finished the build. We were advised by Michael that we will be dealing with java.util.* warnings, and once we start working on a fix to update the name of the class on the board.

   We had chosen to work on the fix for java.util.zip.ZipEntry.java, but the change was similar to the one in Apache Harmony implementation.

diff -r 43a630f11af6 src/share/classes/java/util/zip/ZipEntry.java
--- a/src/share/classes/java/util/zip/ZipEntry.java Wed Nov 30 13:11:16 2011 -0800
+++ b/src/share/classes/java/util/zip/ZipEntry.java Fri Dec 02 19:12:54 2011 +0000
@@ -24,7 +24,7 @@
  */
 
 package java.util.zip;
-
+import java.util.Calendar;
 import java.util.Date;
 
 /**
@@ -281,27 +281,33 @@
      * Converts DOS time to Java time (number of milliseconds since epoch).
      */
     private static long dosToJavaTime(long dtime) {
-        Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
-                          (int)(((dtime >> 21) & 0x0f) - 1),
-                          (int)((dtime >> 16) & 0x1f),
-                          (int)((dtime >> 11) & 0x1f),
-                          (int)((dtime >> 5) & 0x3f),
-                          (int)((dtime << 1) & 0x3e));
-        return d.getTime();
+     Calendar cal = Calendar.getInstance();
+     cal.set(Calendar.YEAR, (int) ((dtime >> 25) & 0x7f) + 1980);
+     cal.set(Calendar.MONTH, (int) ((dtime >> 21) & 0x0f) - 1);
+     cal.set(Calendar.DATE, (int) (dtime >> 16) & 0x1f);
+     cal.set(Calendar.HOUR_OF_DAY, (int) (dtime >> 11) & 0x1f);
+     cal.set(Calendar.MINUTE, (int) (dtime >> 5) & 0x3f);
+     cal.set(Calendar.SECOND, (int) (dtime << 1) & 0x3e);
+     return cal.getTimeInMillis();
     }
 
     /*
      * Converts Java time to DOS time.
      */
     private static long javaToDosTime(long time) {
-        Date d = new Date(time);
-        int year = d.getYear() + 1900;
+        Calendar cal = Calendar.getInstance();
+        cal.setTimeInMillis(time);
+        int year = cal.get(Calendar.YEAR);
         if (year < 1980) {
             return (1 << 21) | (1 << 16);
         }
-        return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
-               d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() << 5 |
-               d.getSeconds() >> 1;
+        int month = cal.get(Calendar.MONTH) + 1;
+        return (year - 1980) << 25
+                | month << 21
+                | cal.get(Calendar.DAY_OF_MONTH) << 16
+                | cal.get(Calendar.HOUR_OF_DAY) << 11
+                | cal.get(Calendar.MINUTE) << 5
+                | cal.get(Calendar.SECOND) >> 1;
     }
 
     /**
 
The Fix we provided started the question of performance and what sort of things we can fix in warnings clean up, a very good lesson learn t.
 
This was my first build in OpenJDK (facepalm)...
 
There were lots of things I had learned, 

You can run the following program to get the real performance 

public class JavaDosTime {

     public static void main(String[] args) throws IOException {

         long tt0 = System.currentTimeMillis();
         int ii = 0;
         for (ii = 0; ii < 1000000; ii++) {
             long t = 10000 + ii;
             t= javaToDosTime(t);
         }
         long tt1 = System.currentTimeMillis();
         System.out.println("j2dt  :" + (tt1 - tt0));


         tt0 = System.currentTimeMillis();
         ii = 0;
         for (ii = 0; ii < 1000000; ii++) {
             long t = 10000 + ii;
             t= javaToDosTime2(t);
         }
         tt1 = System.currentTimeMillis();
         System.out.println("j2dt2 :" + (tt1 - tt0));


         tt0 = System.currentTimeMillis();
         ii = 0;
         for (ii = 0; ii < 1000000; ii++) {
             long t = 10000 + ii;
             t= dosToJavaTime(t);
         }
         tt1 = System.currentTimeMillis();
         System.out.println("d2jt  :" + (tt1 - tt0));


         tt0 = System.currentTimeMillis();
         ii = 0;
         for (ii = 0; ii < 1000000; ii++) {
             long t = 10000 + ii;
             t= dosToJavaTime2(t);
         }
         tt1 = System.currentTimeMillis();
         System.out.println("d2jt2 :" + (tt1 - tt0));

     }

     private static long javaToDosTime(long time){
         Date d = new Date(time);
         int year = d.getYear() + 1900;
         if (year < 1980) {
             return (1 << 21) | (1 << 16);
         }
         return (year - 1980) << 25 | (d.getMonth() + 1) << 21 |
                d.getDate() << 16 | d.getHours() << 11 | d.getMinutes() 
<< 5 |
                d.getSeconds() >> 1;
     }

     private static long javaToDosTime2(long time){
         Calendar cal = Calendar.getInstance();
         cal.setTimeInMillis(time);
         int year = cal.get(Calendar.YEAR);
         if (year < 1980) {
             return (1 << 21) | (1 << 16);
         }
         int month = cal.get(Calendar.MONTH) + 1;
         return (year - 1980) << 25
                 | month << 21
                 | cal.get(Calendar.DAY_OF_MONTH) << 16
                 | cal.get(Calendar.HOUR_OF_DAY) << 11
                 | cal.get(Calendar.MINUTE) << 5
                 | cal.get(Calendar.SECOND) >> 1;
      }

      private static long dosToJavaTime(long dtime) {
         Date d = new Date((int)(((dtime >> 25) & 0x7f) + 80),
                           (int)(((dtime >> 21) & 0x0f) - 1),
                           (int)((dtime >> 16) & 0x1f),
                           (int)((dtime >> 11) & 0x1f),
                           (int)((dtime >> 5) & 0x3f),
                           (int)((dtime << 1) & 0x3e));
         return d.getTime();
      }

      private static long dosToJavaTime2(long dtime) {
      Calendar cal = Calendar.getInstance();
      cal.set(Calendar.YEAR, (int) ((dtime >> 25) & 0x7f) + 1980);
      cal.set(Calendar.MONTH, (int) ((dtime >> 21) & 0x0f) - 1);
      cal.set(Calendar.DATE, (int) (dtime >> 16) & 0x1f);
      cal.set(Calendar.HOUR_OF_DAY, (int) (dtime >> 11) & 0x1f);
      cal.set(Calendar.MINUTE, (int) (dtime >> 5) & 0x3f);
      cal.set(Calendar.SECOND, (int) (dtime << 1) & 0x3e);
          return cal.getTimeInMillis();
      }

} 
 
The Results were
 
t$ java -server JavaDosTime
j2dt  :386
j2dt2 :1282
d2jt  :424
d2jt2 :1296
 
$ java -client JavaDosTime
j2dt  :621
j2dt2 :1901
d2jt  :662
d2jt2 :2016 


This made me learn so many things on building OpenJDK and fixing some warning (which eventually made me fix more issues the next time).

Drawbacks

We had very little time for fixing just about 45 minutes we were able to look at the code for fixing.

Positives

We had learn t so many things out of this 1 hour of working time.





Oracle had posted me a t-shirt, I was expecting Oracle T-Shirt, but I was so happy the -Shirt HAD JAVA... Hurrah !!! A T-Shirt with JAVA and that also from JAVA. I was so proud. 
 
 I have to thank LJC, and skills matter for arranging such a day.