X-Original-To: pgsql-jdbc-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 48B3B3A3CC9 for ; Tue, 2 Nov 2004 15:08:57 +0000 (GMT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 74508-10 for ; Tue, 2 Nov 2004 15:08:48 +0000 (GMT) Received: from eeyore.sebster.com (home.sebster.com [80.126.244.3]) by svr1.postgresql.org (Postfix) with SMTP id 59D4B3A3D53 for ; Tue, 2 Nov 2004 15:08:50 +0000 (GMT) Received: (qmail 95403 invoked by uid 1000); 2 Nov 2004 15:08:49 -0000 Date: Tue, 2 Nov 2004 16:08:49 +0100 From: Sebastiaan van Erk To: pgsql-jdbc@postgresql.org Subject: Re: ps.setCharacterStream() and memory usage Message-ID: <20041102150849.GB12903@sebster.com> References: <20041029115651.GE4199@sebster.com> <200411020946.09434@vadim.nasardinov> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <200411020946.09434@vadim.nasardinov> User-Agent: Mutt/1.5.6i X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200411/19 X-Sequence-Number: 11441 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