pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andreas Schmitz <schmitz@occamlabs.de>
To: pgsql-jdbc@postgresql.org
Subject: bytea performance tweak
Date: Tue, 21 Jun 2011 15:08:11 +0200
Message-ID: <20110621130713.GB3919@summalogicae.occamlabs.local> (raw)

Hi,

I've noticed that fetching a bytea field produces a lot of overhead when
converting into a byte[] or InputStream. Looking at the code, I've
produced a small patch (a lookup table for the hex codes) which makes
the conversion faster. What do you think?

Best regards, Andreas

Attachments:

patch (text/plain, 2.7K) download ? .settings ? bin Index: org/postgresql/util/PGbytea.java =================================================================== RCS file: /cvsroot/jdbc/pgjdbc/org/postgresql/util/PGbytea.java,v retrieving revision 1.17 diff -u -w -r1.17 PGbytea.java --- org/postgresql/util/PGbytea.java 6 Apr 2010 23:45:09 -0000 1.17 +++ org/postgresql/util/PGbytea.java 21 Jun 2011 12:25:14 -0000 @@ -9,7 +9,8 @@ */ package org.postgresql.util; -import java.sql.*; +import java.sql.SQLException; +import java.util.Arrays; /** * Converts to and from the postgresql bytea datatype used by the backend. @@ -18,6 +19,36 @@ { private static final int MAX_3_BUFF_SIZE = 2 * 1024 * 1024; + private static final byte[][] HEX_LOOKUP = new byte[103][103]; + + static { + // build the hex lookup table + for (int i = 0; i < 256; ++i) { + int lo = i & 0x0f; + int lo2 = lo; + int hi = (i & 0xf0) >> 4; + int hi2 = hi; + // 0-9 == 48-57 + // a-f == 97-102 + // A-F == 65-70 + if (lo < 10) { + lo += 48; + lo2 = lo; + } else { + lo += 87; + lo2 += 55; + } + if (hi < 10) { + hi += 48; + hi2 = hi; + } else { + hi += 87; + hi2 += 55; + } + HEX_LOOKUP[lo][hi] = (byte)i; + } + } + /* * Converts a PG bytea raw value (i.e. the raw binary representation * of the bytea data type) into a java byte[] @@ -37,30 +68,14 @@ return toBytesHexEscaped(s); } - private static byte[] toBytesHexEscaped(byte[] s) + private static byte[] toBytesHexEscaped(final byte[] s) { - byte[] output = new byte[(s.length - 2) / 2]; - for (int i=0; i<output.length; i++) { - byte b1 = gethex(s[2 + i*2]); - byte b2 = gethex(s[2 + i*2 + 1]); - output[i] = (byte)((b1 << 4) | b2); - } + final byte[] output = new byte[(s.length - 2) / 2]; + for (int i=0; i<output.length; i++) + output[i] = HEX_LOOKUP[s[2 + i*2+1]][s[2 + i*2]]; return output; } - private static byte gethex(byte b) { - // 0-9 == 48-57 - if (b <= 57) - return (byte)(b - 48); - - // a-f == 97-102 - if (b >= 97) - return (byte)(b - 97 + 10); - - // A-F == 65-70 - return (byte)(b - 65 + 10); - } - private static byte[] toBytesOctalEscaped(byte[] s) { final int slength = s.length;
signature.asc (application/pgp-signature, 197B) download

view thread (17+ messages)  latest in thread

Message-ID: <20110621130713.GB3919@summalogicae.occamlabs.local>
Permalink:  ../20110621130713.GB3919@summalogicae.occamlabs.local/
Also on:    postgresql.org/message-id/20110621130713.GB3919@summalogicae.occamlabs.local

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-jdbc@postgresql.org
  Cc: schmitz@occamlabs.de
  Subject: Re: bytea performance tweak
  In-Reply-To: <20110621130713.GB3919@summalogicae.occamlabs.local>
  References: <20110621130713.GB3919@summalogicae.occamlabs.local>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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