JDBC: Better initial capacity for StringBuffers reduces memory usage

From: Fernando Nasser <fnasser(at)redhat(dot)com>
To: pgsql-jdbc <pgsql-jdbc(at)postgresql(dot)org>, pgsql-patches <pgsql-patches(at)postgresql(dot)org>
Subject: JDBC: Better initial capacity for StringBuffers reduces memory usage
Date: 2003-05-22 14:59:52
Message-ID: 3ECCE5E8.7080904@redhat.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc pgsql-patches

Regardless of the streaming patches that are floating around, we should fix the
allocation of the StringBuffers.

On the AbstractJdbc1Statement one, the initial space allocated for the buffer
will never be enough because of the quotes, so we end up using 3 times the size
of the object + 1 and having to copy it from the initial buffer to the double
size one. Instead of just adding '+2' I gave it some slack for the escaping of
quotes.

The one in PGbytea may raise some eyebrows, but I can guarantee you that it
_always_ win, when compared to allocate just the object size for at least that
amount of space.

The changes above reduce by 33% (in the worst case) the memory requirements for
uploading data (without the streaming patches). Although not that important for
smaller data types (once the streaming is in place we will not send large ones
through this path anymore), there is no reason to waste memory so I suggest we
check this in.

Regards to all.

--
Fernando Nasser
Red Hat - Toronto E-Mail: fnasser(at)redhat(dot)com
2323 Yonge Street, Suite #300
Toronto, Ontario M4P 2C9

Attachment Content-Type Size
JDBCCAPACITY.PATCH text/plain 1.8 KB

Responses

Browse pgsql-jdbc by date

  From Date Subject
Next Message Barry Lind 2003-05-22 17:16:13 Re: [Bug] ResultSet#getMetaData() returns null
Previous Message innuendo-nh 2003-05-22 13:57:06 I found a bug in the jdbc driver

Browse pgsql-patches by date

  From Date Subject
Next Message Joe Conway 2003-05-23 00:17:11 Re: array support patch phase 1 patch
Previous Message Rod Taylor 2003-05-22 14:42:54 CVSup doc correction (minor)