Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id B1F45633B7A for ; Thu, 16 Apr 2009 10:03:21 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 58511-05 for ; Thu, 16 Apr 2009 10:03:11 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from exprod7og106.obsmtp.com (exprod7og106.obsmtp.com [64.18.2.165]) by mail.postgresql.org (Postfix) with SMTP id 7F8CF6339FD for ; Thu, 16 Apr 2009 10:03:18 -0300 (ADT) Received: from source ([209.85.219.169]) by exprod7ob106.postini.com ([64.18.6.12]) with SMTP ID DSNKSecslNA5x/PFlRyikrPA1ERS1TBU3WC8@postini.com; Thu, 16 Apr 2009 06:03:18 PDT Received: by ewy17 with SMTP id 17so374574ewy.28 for ; Thu, 16 Apr 2009 06:03:15 -0700 (PDT) Received: by 10.210.126.18 with SMTP id y18mr1452230ebc.45.1239886995835; Thu, 16 Apr 2009 06:03:15 -0700 (PDT) Received: from ?80.223.212.7? (dsl-hkibrasgw2-fed4df00-7.dhcp.inet.fi [80.223.212.7]) by mx.google.com with ESMTPS id 5sm1429422eyh.41.2009.04.16.06.03.14 (version=TLSv1/SSLv3 cipher=RC4-MD5); Thu, 16 Apr 2009 06:03:15 -0700 (PDT) Message-ID: <49E72C90.9010903@enterprisedb.com> Date: Thu, 16 Apr 2009 16:03:12 +0300 From: Heikki Linnakangas Organization: EnterpriseDB User-Agent: Mozilla-Thunderbird 2.0.0.19 (X11/20090103) MIME-Version: 1.0 To: pgsql-jdbc@postgresql.org Subject: Fast BigDecimal Content-Type: multipart/mixed; boundary="------------040807050207030604080008" X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200904/64 X-Sequence-Number: 19511 This is a multi-part message in MIME format. --------------040807050207030604080008 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Here's another patch I put together while trying to improve performance for the customer that bumped into the repeated Describe thing yesterday. It speeds up ResultSet.getBigDecimal() with the same fastpath implementation we have for getInt and getLong. I'm testing this with a test program that reads 10000 rows from a table with 100 numeric columns, calling getBigDecimal() on each column. This patch cuts the execution time roughly in half, from 2.4s to 1.2s. -- Heikki Linnakangas EnterpriseDB http://www.enterprisedb.com --------------040807050207030604080008 Content-Type: text/x-diff; name="fast-getbigdecimal-2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="fast-getbigdecimal-2.patch" Index: org/postgresql/jdbc2/AbstractJdbc2ResultSet.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v retrieving revision 1.105 diff -u -r1.105 AbstractJdbc2ResultSet.java --- org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 30 Sep 2008 04:34:51 -0000 1.105 +++ org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 16 Apr 2009 13:01:21 -0000 @@ -2137,6 +2137,65 @@ return val; } + /** + * Optimised byte[] to number parser. This code does not + * handle null values, so the caller must do checkResultSet + * and handle null values prior to calling this function. + * + * @param columnIndex The column to parse. + * @return The parsed number. + * @throws SQLException If an error occurs while fetching column. + * @throws NumberFormatException If the number is invalid or the + * out of range for fast parsing. The value must then be parsed by + * {@link #toBigDecimal(String)}. + */ + private BigDecimal getFastBigDecimal(int columnIndex) throws SQLException, + NumberFormatException { + + byte[] bytes = this_row[columnIndex - 1]; + + if (bytes.length == 0) { + throw FAST_NUMBER_FAILED; + } + + int scale = 0; + long val = 0; + int start; + boolean neg; + if (bytes[0] == '-') { + neg = true; + start = 1; + if (bytes.length > 19) { + throw FAST_NUMBER_FAILED; + } + } else { + start = 0; + neg = false; + if (bytes.length > 18) { + throw FAST_NUMBER_FAILED; + } + } + + while (start < bytes.length) { + byte b = bytes[start++]; + if (b < '0' || b > '9') { + if (b == '.') { + scale = bytes.length - start; + continue; + } else + throw FAST_NUMBER_FAILED; + } + val *= 10; + val += b - '0'; + } + + if (neg) { + val = -val; + } + + return BigDecimal.valueOf(val, scale); + } + public float getFloat(int columnIndex) throws SQLException { checkResultSet(columnIndex); @@ -2161,6 +2220,14 @@ if (wasNullFlag) return null; + Encoding encoding = connection.getEncoding(); + if (encoding.hasAsciiNumbers()) { + try { + return getFastBigDecimal(columnIndex); + } catch (NumberFormatException ex) { + } + } + return toBigDecimal( getFixedString(columnIndex), scale ); } --------------040807050207030604080008--