Thread: Enhancement: toBoolean() and empty String

Enhancement: toBoolean() and empty String

From
Eberhard Schulte
Date:
Hello,

I habe two enhancements. I found the problems even in older versions.

1. Encoding.decode() creates for each empty String a new String object. Memory is wasted unnecessary.

Index: Encoding.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/core/Encoding.java,v
retrieving revision 1.21
diff -u -r1.21 Encoding.java
--- Encoding.java       4 Jul 2005 02:18:32 -0000       1.21
+++ Encoding.java       3 Mar 2006 08:45:12 -0000
@@ -175,6 +175,10 @@
       */
      public String decode(byte[] encodedString, int offset, int length) throws IOException
      {
+        if(encodedString == null || encodedString.length == 0) {
+            return ""; // do not create new String() use constant String
+        }
+
          if (encoding == null)
              return new String(encodedString, offset, length);



2. AbstractJdbc2ResultSet.toBoolean(): there is no explicite false check. A false is produced by an
NumberFormatException.


Index: AbstractJdbc2ResultSet.java
===================================================================
RCS file: /usr/local/cvsroot/pgjdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v
retrieving revision 1.83
diff -u -r1.83 AbstractJdbc2ResultSet.java
--- AbstractJdbc2ResultSet.java 4 Dec 2005 21:40:33 -0000       1.83
+++ AbstractJdbc2ResultSet.java 3 Mar 2006 08:54:53 -0000
@@ -2434,22 +2434,28 @@

      //----------------- Formatting Methods -------------------

-    public static boolean toBoolean(String s)
+    public static boolean toBoolean(String s) throws PSQLException
      {
          if (s != null)
          {
              s = s.trim();

-            if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t"))
-                return true;
-
+            if (s.equalsIgnoreCase("t") || s.equalsIgnoreCase("true")) { // examine the most frequent case first
+                return true;
+            } else if (s.equalsIgnoreCase("f") || s.equalsIgnoreCase("false")) { // explicite false check
+                return false;
+            }
+
              try
              {
                  if (Double.valueOf(s).doubleValue() == 1)
                      return true;
+                // TODO / FIXME: explicite false check
              }
              catch (NumberFormatException e)
              {
+                // throw exception if occurs
+                throw new PSQLException ("postgresql.res.baddouble", PSQLState.NUMERIC_VALUE_OUT_OF_RANGE, e);
              }
          }
          return false;  // SQL NULL





Schönen Gruß

Eberhard Schulte


Re: Enhancement: toBoolean() and empty String

From
Kris Jurka
Date:

On Fri, 3 Mar 2006, Eberhard Schulte wrote:

> 1. Encoding.decode() creates for each empty String a new String object.
> Memory is wasted unnecessary.
>

I have been unable to measure a difference in a test of "SELECT NULL FROM
generate_series(1,1000000)" and calling getString for each result.  Do you
have another test case that shows an impact?

> 2. AbstractJdbc2ResultSet.toBoolean(): there is no explicite false check. A
> false is produced by an NumberFormatException.

Yes, that's a problem.  I timed a factor of two or so speedup with your
patch.  I've extended it to also check for "1" and "0" as strings which
will also be common and applied it to cvs.  I did not put in the exception
for invalid data like "hello".  The spec is a little vague about this and
since we haven't heard any complaints, I'm inclined to retain backwards
compatible behavior.

Kris Jurka