From: Sebastiaan van Erk <sebster@sebster.com>
To: pgsql-jdbc@postgresql.org
Subject: Re: ps.setCharacterStream() and memory usage
Date: Tue, 2 Nov 2004 16:08:49 +0100
Message-ID: <20041102150849.GB12903@sebster.com> (raw)
In-Reply-To: <200411020946.09434@vadim.nasardinov>
References: <20041029115651.GE4199@sebster.com>
<200411020946.09434@vadim.nasardinov>
Vadim Nasardinov wrote:
> I assume you're talking about
> org/postgresql/jdbc2/AbstractJdbc2Statement.java which has this piece
> of code (slightly reformatted to fit on a 80-column page):
[snip]
> This does appear to be optimized for reading smallish chunks of text.
> If the character source behind the Reader is sufficiently large, I
> believe we'd be better off building up a StringBuffer rather than a
> character array. Something like this:
>
> if (connection.haveMinimumCompatibleVersion("7.2")) {
> StringBuffer sb = new StringBuffer(length);
> BufferedReader br = new BufferedReader(x);
> try {
> while(true) {
> String chunk = br.readLine();
> if (chunk == null) { break; }
> else {sb.append(chunk); }
> }
> } catch (IOException l_ioe) {
> throw new PSQLException(GT.tr("Provided Reader failed."),
> PSQLState.UNEXPECTED_ERROR, l_ioe);
> }
> setString(i, sb.toString());
> }
>
> The reason this is better is because
>
> (a) the String(char[],int,int) constructor always defensively copies
> the passed in character array. So, as you point out, if you
> pass in a 10-million-character array, a new 10-million character
> array will be allocated.
>
> (b) in contrast, if you construct a String from a StringBuffer, they
> can share the underlying character array:
> http://www.google.com/search?q=Heinz+Kabutz+StringBuffer+Issue+068&btnI
This is indeed part of the code I was talking about. And indeed a
StringBuffer is already a definate improvement.
However, the setString() method will also cause the data to be duplicated
when escapeString() is called. A version of escapeString() which works
on a StringBuffer would be another improvement in my opinion, saving
(if the allocated StringBuffer is made a bit larger to allow for the
escaping), in most cases another allocation of the whole string.
Greetings,
Sebastiaan van Erk
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: sebster@sebster.com
Subject: Re: ps.setCharacterStream() and memory usage
In-Reply-To: <20041102150849.GB12903@sebster.com>
References: <20041029115651.GE4199@sebster.com> <200411020946.09434@vadim.nasardinov> <20041102150849.GB12903@sebster.com>
* 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