pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Enhancement: toBoolean() and empty String
2+ messages / 2 participants
[nested] [flat]

* Enhancement: toBoolean() and empty String
@ 2006-03-03 09:15 Eberhard Schulte <esc@pixelboxx.de>
  2006-11-05 07:45 ` Re: Enhancement: toBoolean() and empty String Kris Jurka <books@ejurka.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Eberhard Schulte @ 2006-03-03 09:15 UTC (permalink / raw)
  To: pgsql-jdbc

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.

inline patch 1: Encoding.java, Encoding.java 3 Mar 2006 08:45:12 -0000 (+4/-0 lines) download inline-1.patchIndex: 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.
inline patch 2: AbstractJdbc2ResultSet.java, AbstractJdbc2ResultSet.java 3 Mar 2006 08:54:53 -0000 (+10/-4 lines) download inline-2.patchIndex: 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 Patches: download all (2 files, .tar.gz) ^ permalink raw reply [nested|flat] 2+ messages in thread

* Re: Enhancement: toBoolean() and empty String
  2006-03-03 09:15 Enhancement: toBoolean() and empty String Eberhard Schulte <esc@pixelboxx.de>
@ 2006-11-05 07:45 ` Kris Jurka <books@ejurka.com>
  0 siblings, 0 replies; 2+ messages in thread

From: Kris Jurka @ 2006-11-05 07:45 UTC (permalink / raw)
  To: Eberhard Schulte <esc@pixelboxx.de>; +Cc: pgsql-jdbc



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



^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2006-11-05 07:45 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-03-03 09:15 Enhancement: toBoolean() and empty String Eberhard Schulte <esc@pixelboxx.de>
2006-11-05 07:45 ` Kris Jurka <books@ejurka.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox