pg.ddx.io pgsql-jdbc@postgresql.org mailing list archive
help / color / mirror / Atom feedFast BigDecimal
5+ messages / 2 participants
[nested] [flat]
* Fast BigDecimal
@ 2009-04-16 13:03 Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
0 siblings, 1 reply; 5+ messages in thread
From: Heikki Linnakangas @ 2009-04-16 13:03 UTC (permalink / raw)
To: pgsql-jdbc
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
Attachments:
fast-getbigdecimal-2.patch (text/x-diff, 2.7K) download
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 );
}
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Fast BigDecimal
@ 2009-04-18 23:56 Kris Jurka <books@ejurka.com>
parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
0 siblings, 1 reply; 5+ messages in thread
From: Kris Jurka @ 2009-04-18 23:56 UTC (permalink / raw)
To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: pgsql-jdbc
On Thu, 16 Apr 2009, Heikki Linnakangas wrote:
> 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.
To maintain the same behavior as the current code, you need to handle the
period more carefully. You must ensure that there is only one and that
there's at least one other number. Consider the case of SELECT '.' OR
SELECT '1.......'.
Kris Jurka
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Fast BigDecimal
@ 2009-04-19 05:20 Kris Jurka <books@ejurka.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 5+ messages in thread
From: Kris Jurka @ 2009-04-19 05:20 UTC (permalink / raw)
To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: pgsql-jdbc
On Sat, 18 Apr 2009, Kris Jurka wrote:
> On Thu, 16 Apr 2009, Heikki Linnakangas wrote:
>
>> 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.
>
> To maintain the same behavior as the current code, you need to handle the
> period more carefully. You must ensure that there is only one and that
> there's at least one other number. Consider the case of SELECT '.' OR SELECT
> '1.......'.
>
For some reason I thought this would be tricky. We just need to track the
total number for periods seen and error out if > 1 or equal to the total
number of characters. I've done that and committed it.
Kris Jurka
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Fast BigDecimal
@ 2009-04-19 08:16 Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
parent: Kris Jurka <books@ejurka.com>
0 siblings, 1 reply; 5+ messages in thread
From: Heikki Linnakangas @ 2009-04-19 08:16 UTC (permalink / raw)
To: Kris Jurka <books@ejurka.com>; +Cc: pgsql-jdbc
Kris Jurka wrote:
>
>
> On Sat, 18 Apr 2009, Kris Jurka wrote:
>
>> On Thu, 16 Apr 2009, Heikki Linnakangas wrote:
>>
>>> 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.
>>
>> To maintain the same behavior as the current code, you need to handle
>> the period more carefully. You must ensure that there is only one and
>> that there's at least one other number. Consider the case of SELECT
>> '.' OR SELECT '1.......'.
>>
>
> For some reason I thought this would be tricky. We just need to track
> the total number for periods seen and error out if > 1 or equal to the
> total number of characters. I've done that and committed it.
Thanks. One more thing: this accepts "-.", which the normal version
doesn't. Patch attached.
--
Heikki Linnakangas
EnterpriseDB http://www.enterprisedb.com
Attachments:
a.patch (text/x-diff, 887B) download
Index: org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
===================================================================
RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v
retrieving revision 1.106
diff -c -r1.106 AbstractJdbc2ResultSet.java
*** org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 19 Apr 2009 05:18:31 -0000 1.106
--- org/postgresql/jdbc2/AbstractJdbc2ResultSet.java 19 Apr 2009 08:15:03 -0000
***************
*** 2191,2197 ****
val += b - '0';
}
! if (periodsSeen > 1 || periodsSeen == bytes.length)
throw FAST_NUMBER_FAILED;
if (neg) {
--- 2191,2198 ----
val += b - '0';
}
! if (periodsSeen > 1 ||
! periodsSeen == (neg ? bytes.length - 1 : bytes.length))
throw FAST_NUMBER_FAILED;
if (neg) {
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Fast BigDecimal
@ 2009-04-19 16:15 Kris Jurka <books@ejurka.com>
parent: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
0 siblings, 0 replies; 5+ messages in thread
From: Kris Jurka @ 2009-04-19 16:15 UTC (permalink / raw)
To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>; +Cc: pgsql-jdbc
On Sun, 19 Apr 2009, Heikki Linnakangas wrote:
> Thanks. One more thing: this accepts "-.", which the normal version doesn't.
> Patch attached.
>
Committed. This was a problem for the getFastInt/Long methods as well
where they allowed a bare negative sign.
Kris Jurka
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2009-04-19 16:15 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-04-16 13:03 Fast BigDecimal Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2009-04-18 23:56 ` Kris Jurka <books@ejurka.com>
2009-04-19 05:20 ` Kris Jurka <books@ejurka.com>
2009-04-19 08:16 ` Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
2009-04-19 16:15 ` 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