Author: damjan
Date: Sat Mar 25 14:14:44 2017
New Revision: 1788667

URL: http://svn.apache.org/viewvc?rev=1788667&view=rev
Log:
Add some Java performance optimizations with boxing of basic types:
instead of using "new <Type>(<value>)", use <Type>.valueOf(<value>),
or better yet, rely on autoboxing.

Since the box objects are immutable, Java can and does cache them for
values between -128 and 127, which includes all possible boolean and byte
values and many common values for all other basic types. Thus, unlike the
constructor, calling valueOf() or autoboxing such values does not allocate
memory at all, and only returns the pre-existing instance, which is not only
faster, but uses zero extra memory too.

This is the first of many patches. This one fixes this problem in bridges,
javaunohelper and jurt, which are parts of the Java<->Uno bridge, which is
critical to performance.

Patch by: me


Modified:
    
openoffice/trunk/main/bridges/source/jni_uno/java/com/sun/star/bridges/jni_uno/JNI_proxy.java
    
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySet.java
    
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySetMixin.java
    openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java
    
openoffice/trunk/main/jurt/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory.java
    openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/Unmarshal.java
    openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/urp.java
    openoffice/trunk/main/jurt/com/sun/star/uno/AnyConverter.java

Modified: 
openoffice/trunk/main/bridges/source/jni_uno/java/com/sun/star/bridges/jni_uno/JNI_proxy.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/bridges/source/jni_uno/java/com/sun/star/bridges/jni_uno/JNI_proxy.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- 
openoffice/trunk/main/bridges/source/jni_uno/java/com/sun/star/bridges/jni_uno/JNI_proxy.java
 (original)
+++ 
openoffice/trunk/main/bridges/source/jni_uno/java/com/sun/star/bridges/jni_uno/JNI_proxy.java
 Sat Mar 25 14:14:44 2017
@@ -150,7 +150,7 @@ public final class JNI_proxy implements
             if (method_name.equals( "hashCode" ))
             {
                 // int hashCode()
-                return new Integer( m_oid.hashCode() );
+                return Integer.valueOf( m_oid.hashCode() );
             }
             else if (method_name.equals( "equals" ))
             {
@@ -208,7 +208,7 @@ public final class JNI_proxy implements
     }
 
     private Boolean isSame(Object obj) {
-        return new Boolean(obj != null
-                           && m_oid.equals(UnoRuntime.generateOid(obj)));
+        return obj != null
+                           && m_oid.equals(UnoRuntime.generateOid(obj));
     }
 }

Modified: 
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySet.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySet.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- 
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySet.java
 (original)
+++ 
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySet.java
 Sat Mar 25 14:14:44 2017
@@ -221,7 +221,7 @@ XMultiPropertySet
      */
     protected Property getPropertyByHandle(int nHandle)
     {
-        return (Property) _handleToPropertyMap.get(new Integer(nHandle));
+        return (Property) _handleToPropertyMap.get(nHandle);
     }
     
     /** Returns an array of all Property objects or an array of length null if 
there
@@ -251,7 +251,7 @@ XMultiPropertySet
     {
         _nameToPropertyMap.put(prop.Name, prop);
         if (prop.Handle != -1)
-            _handleToPropertyMap.put(new Integer(prop.Handle), prop);
+            _handleToPropertyMap.put(prop.Handle, prop);
     }
     
     /** Assigns an identifyer object to a Property object so that the 
identifyer 
@@ -577,7 +577,7 @@ XMultiPropertySet
      *  value of the property is to be stored in a member variable of type int 
with name intProp. Then setPropertyValue is 
      *  called:
      *  <pre>
-     *  set.setPropertyValue( "PropA", new Byte( (byte)111));
+     *  set.setPropertyValue( "PropA", (byte)111);
      *  </pre>
      *  At some point setPropertyValue will call convertPropertyValue and pass 
in the Byte object. Since we allow
      *  that Byte values can be used with the property and know that the value 
is to be stored in intProp (type int) 
@@ -732,21 +732,21 @@ XMultiPropertySet
             retVal= obj;
         }
         else if(cl.equals(boolean.class))
-            retVal= new Boolean(AnyConverter.toBoolean(obj));
+            retVal= AnyConverter.toBoolean(obj);
         else if (cl.equals(char.class))
-            retVal= new Character(AnyConverter.toChar(obj));
+            retVal= AnyConverter.toChar(obj);
         else if (cl.equals(byte.class))
-            retVal= new Byte(AnyConverter.toByte(obj));
+            retVal= AnyConverter.toByte(obj);
         else if (cl.equals(short.class))
-            retVal= new Short(AnyConverter.toShort(obj));
+            retVal= AnyConverter.toShort(obj);
         else if (cl.equals(int.class))
-            retVal= new Integer(AnyConverter.toInt(obj));
+            retVal= AnyConverter.toInt(obj);
         else if (cl.equals(long.class))
-            retVal= new Long(AnyConverter.toLong(obj));
+            retVal= AnyConverter.toLong(obj);
         else if (cl.equals(float.class))
-            retVal= new Float(AnyConverter.toFloat(obj));
+            retVal= AnyConverter.toFloat(obj);
         else if (cl.equals(double.class))
-            retVal= new Double(AnyConverter.toDouble(obj));
+            retVal= AnyConverter.toDouble(obj);
         else if (cl.equals(String.class))
             retVal= AnyConverter.toString(obj);
         else if (cl.isArray())
@@ -754,21 +754,21 @@ XMultiPropertySet
         else if (cl.equals(Type.class))
             retVal= AnyConverter.toType(obj);
         else if (cl.equals(Boolean.class))
-            retVal= new Boolean(AnyConverter.toBoolean(obj));
+            retVal= AnyConverter.toBoolean(obj);
         else if (cl.equals(Character.class))
-            retVal= new Character(AnyConverter.toChar(obj));
+            retVal= AnyConverter.toChar(obj);
         else if (cl.equals(Byte.class))
-            retVal= new Byte(AnyConverter.toByte(obj));
+            retVal= AnyConverter.toByte(obj);
         else if (cl.equals(Short.class))
-            retVal= new Short(AnyConverter.toShort(obj));
+            retVal= AnyConverter.toShort(obj);
         else if (cl.equals(Integer.class))
-            retVal= new Integer(AnyConverter.toInt(obj));
+            retVal= AnyConverter.toInt(obj);
         else if (cl.equals(Long.class))
-            retVal= new Long(AnyConverter.toLong(obj));
+            retVal= AnyConverter.toLong(obj);
         else if (cl.equals(Float.class))
-            retVal= new Float(AnyConverter.toFloat(obj));
+            retVal= AnyConverter.toFloat(obj);
         else if (cl.equals(Double.class))
-            retVal= new Double(AnyConverter.toDouble(obj));
+            retVal= AnyConverter.toDouble(obj);
         else if (XInterface.class.isAssignableFrom(cl))
             retVal= AnyConverter.toObject(new Type(cl), obj);
         else if (com.sun.star.uno.Enum.class.isAssignableFrom(cl))

Modified: 
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySetMixin.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySetMixin.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- 
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySetMixin.java
 (original)
+++ 
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/PropertySetMixin.java
 Sat Mar 25 14:14:44 2017
@@ -931,7 +931,7 @@ public final class PropertySetMixin {
                         isDefaulted, wrapOptional));
                 UnoRuntime.queryInterface(
                     XIdlField2.class, type.getField("IsAmbiguous")).set(
-                        strct, new Boolean(isAmbiguous));
+                        strct, isAmbiguous);
             } catch (com.sun.star.lang.IllegalArgumentException e) {
                 throw new RuntimeException(
                     "unexpected com.sun.star.lang.IllegalArgumentException: "
@@ -958,7 +958,7 @@ public final class PropertySetMixin {
                         false, false, wrapOptional));
                 UnoRuntime.queryInterface(
                     XIdlField2.class, type.getField("IsDefaulted")).set(
-                        strct, new Boolean(isDefaulted));
+                        strct, isDefaulted);
             } catch (com.sun.star.lang.IllegalArgumentException e) {
                 throw new RuntimeException(
                     "unexpected com.sun.star.lang.IllegalArgumentException: "
@@ -978,7 +978,7 @@ public final class PropertySetMixin {
             try {
                 UnoRuntime.queryInterface(
                     XIdlField2.class, type.getField("IsPresent")).set(
-                        strct, new Boolean(present));
+                        strct, present);
                 if (present) {
                     XIdlField2 field = UnoRuntime.queryInterface(
                         XIdlField2.class, type.getField("Value"));

Modified: 
openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java 
(original)
+++ openoffice/trunk/main/javaunohelper/com/sun/star/lib/uno/helper/UnoUrl.java 
Sat Mar 25 14:14:44 2017
@@ -236,7 +236,7 @@ public class UnoUrl {
                                ch = (hb << 4) | lb;
                        }
 
-                       v.addElement(new Integer(ch));
+                       v.addElement(ch);
                }
 
                int size = v.size();

Modified: 
openoffice/trunk/main/jurt/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/jurt/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- 
openoffice/trunk/main/jurt/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory.java
 (original)
+++ 
openoffice/trunk/main/jurt/com/sun/star/lib/uno/bridges/java_remote/ProxyFactory.java
 Sat Mar 25 14:14:44 2017
@@ -112,7 +112,7 @@ final class ProxyFactory {
                 return Boolean.valueOf(args[0] != null
                         && oid.equals(UnoRuntime.generateOid(args[0])));
             } else if (method.equals(METHOD_HASH_CODE)) {
-                return new Integer(oid.hashCode());
+                return Integer.valueOf(oid.hashCode());
             } else if (method.equals(METHOD_TO_STRING)) {
                 return "[Proxy:" + System.identityHashCode(proxy) + "," + oid
                     + "," + type + "]";

Modified: 
openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/Unmarshal.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/Unmarshal.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- 
openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/Unmarshal.java 
(original)
+++ 
openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/Unmarshal.java 
Sat Mar 25 14:14:44 2017
@@ -215,7 +215,7 @@ final class Unmarshal {
 
     private Byte readByteValue() {
         try {
-            return new Byte(input.readByte());
+            return input.readByte();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
@@ -223,7 +223,7 @@ final class Unmarshal {
 
     private Short readShortValue() {
         try {
-            return new Short(input.readShort());
+            return input.readShort();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
@@ -231,7 +231,7 @@ final class Unmarshal {
 
     private Integer readLongValue() {
         try {
-            return new Integer(input.readInt());
+            return input.readInt();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
@@ -239,7 +239,7 @@ final class Unmarshal {
 
     private Long readHyperValue() {
         try {
-            return new Long(input.readLong());
+            return input.readLong();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
@@ -247,7 +247,7 @@ final class Unmarshal {
 
     private Float readFloatValue() {
         try {
-            return new Float(input.readFloat());
+            return input.readFloat();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
@@ -255,7 +255,7 @@ final class Unmarshal {
 
     private Double readDoubleValue() {
         try {
-            return new Double(input.readDouble());
+            return input.readDouble();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }
@@ -263,7 +263,7 @@ final class Unmarshal {
 
     private Character readCharValue() {
         try {
-            return new Character(input.readChar());
+            return input.readChar();
         } catch (IOException e) {
             throw new RuntimeException(e.toString());
         }

Modified: openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/urp.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/urp.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/urp.java 
(original)
+++ openoffice/trunk/main/jurt/com/sun/star/lib/uno/protocols/urp/urp.java Sat 
Mar 25 14:14:44 2017
@@ -193,7 +193,7 @@ public final class urp implements IProto
             true, PROPERTIES_OID,
             TypeDescription.getTypeDescription(XProtocolProperties.class),
             PROPERTIES_FUN_REQUEST_CHANGE, propertiesTid,
-            new Object[] { new Integer(random) });
+            new Object[] { random });
         state = STATE_REQUESTED;
     }
 
@@ -214,7 +214,7 @@ public final class urp implements IProto
                     case STATE_INITIAL0:
                     case STATE_INITIAL:
                         writeReply(
-                            false, message.getThreadId(), new Integer(1));
+                            false, message.getThreadId(), 1);
                         state = STATE_WAIT;
                         break;
                     case STATE_REQUESTED:
@@ -222,16 +222,16 @@ public final class urp implements IProto
                             = ((Integer) message.getArguments()[0]).intValue();
                         if (random < n) {
                             writeReply(
-                                false, message.getThreadId(), new Integer(1));
+                                false, message.getThreadId(), 1);
                             state = STATE_WAIT;
                         } else if (random == n) {
                             writeReply(
-                                false, message.getThreadId(), new Integer(-1));
+                                false, message.getThreadId(), -1);
                             state = STATE_INITIAL;
                             sendRequestChange();
                         } else {
                             writeReply(
-                                false, message.getThreadId(), new Integer(0));
+                                false, message.getThreadId(), 0);
                         }
                         break;
                     default:

Modified: openoffice/trunk/main/jurt/com/sun/star/uno/AnyConverter.java
URL: 
http://svn.apache.org/viewvc/openoffice/trunk/main/jurt/com/sun/star/uno/AnyConverter.java?rev=1788667&r1=1788666&r2=1788667&view=diff
==============================================================================
--- openoffice/trunk/main/jurt/com/sun/star/uno/AnyConverter.java (original)
+++ openoffice/trunk/main/jurt/com/sun/star/uno/AnyConverter.java Sat Mar 25 
14:14:44 2017
@@ -433,7 +433,7 @@ public class AnyConverter
                 switch (tc)
                 {
                 case TypeClass.BYTE_value:
-                    return new Short( ((Byte)object).byteValue() );
+                    return Short.valueOf( ((Byte)object).byteValue() );
                 case TypeClass.SHORT_value:
                     return object;
                 }
@@ -449,10 +449,10 @@ public class AnyConverter
                 switch (tc)
                 {
                 case TypeClass.BYTE_value:
-                    return new Integer( ((Byte)object).byteValue() );
+                    return Integer.valueOf( ((Byte)object).byteValue() );
                 case TypeClass.SHORT_value:
                 case TypeClass.UNSIGNED_SHORT_value:
-                    return new Integer( ((Short)object).shortValue() );
+                    return Integer.valueOf( ((Short)object).shortValue() );
                 case TypeClass.LONG_value:
                     return object;
                 }
@@ -461,7 +461,7 @@ public class AnyConverter
                 switch (tc)
                 {
                 case TypeClass.UNSIGNED_SHORT_value:
-                    return new Integer( ((Short)object).shortValue() );
+                    return Integer.valueOf( ((Short)object).shortValue() );
                 case TypeClass.UNSIGNED_LONG_value:
                     return object;
                 }
@@ -470,13 +470,13 @@ public class AnyConverter
                 switch (tc)
                 {
                 case TypeClass.BYTE_value:
-                    return new Long( ((Byte)object).byteValue() );
+                    return Long.valueOf( ((Byte)object).byteValue() );
                 case TypeClass.SHORT_value:
                 case TypeClass.UNSIGNED_SHORT_value:
-                    return new Long( ((Short)object).shortValue() );
+                    return Long.valueOf( ((Short)object).shortValue() );
                 case TypeClass.LONG_value:
                 case TypeClass.UNSIGNED_LONG_value:
-                    return new Long( ((Integer)object).intValue() );
+                    return Long.valueOf( ((Integer)object).intValue() );
                 case TypeClass.HYPER_value:
                     return object;
                 }
@@ -485,9 +485,9 @@ public class AnyConverter
                 switch (tc)
                 {
                 case TypeClass.UNSIGNED_SHORT_value:
-                    return new Long( ((Short)object).shortValue() );
+                    return Long.valueOf( ((Short)object).shortValue() );
                 case TypeClass.UNSIGNED_LONG_value:
-                    return new Long( ((Integer)object).intValue() );
+                    return Long.valueOf( ((Integer)object).intValue() );
                 case TypeClass.UNSIGNED_HYPER_value:
                     return object;
                 }
@@ -496,9 +496,9 @@ public class AnyConverter
                 switch (tc)
                 {
                 case TypeClass.BYTE_value:
-                    return new Float( ((Byte)object).byteValue() );
+                    return Float.valueOf( ((Byte)object).byteValue() );
                 case TypeClass.SHORT_value:
-                    return new Float( ((Short)object).shortValue() );
+                    return Float.valueOf( ((Short)object).shortValue() );
                 case TypeClass.FLOAT_value:
                     return object;
                 }
@@ -507,13 +507,13 @@ public class AnyConverter
                 switch (tc)
                 {
                 case TypeClass.BYTE_value:
-                    return new Double( ((Byte)object).byteValue() );
+                    return Double.valueOf( ((Byte)object).byteValue() );
                 case TypeClass.SHORT_value:
-                    return new Double( ((Short)object).shortValue() );
+                    return Double.valueOf( ((Short)object).shortValue() );
                 case TypeClass.LONG_value:
-                    return new Double( ((Integer)object).intValue() );
+                    return Double.valueOf( ((Integer)object).intValue() );
                 case TypeClass.FLOAT_value:
-                    return new Double( ((Float)object).floatValue() );
+                    return Double.valueOf( ((Float)object).floatValue() );
                 case TypeClass.DOUBLE_value:
                     return object;
                 }


Reply via email to