Varlena and binary

Started by Radosław Smoguraalmost 15 years ago4 messages
#1Radosław Smogura
mail@smogura.eu
1 attachment(s)

Hi,

I'm sending small patch for textsend. It reduces unnecessary copies, and
memory usage for duplication of varlena data. May you look?

Kind regards,
Radosław Smogura

Attachments:

varlena-perform.patchtext/x-patch; charset=UTF-8; name=varlena-perform.patchDownload
diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c
index e111d26..f24bbcd 100644
--- a/src/backend/utils/adt/varlena.c
+++ b/src/backend/utils/adt/varlena.c
@@ -442,12 +442,20 @@ textrecv(PG_FUNCTION_ARGS)
 Datum
 textsend(PG_FUNCTION_ARGS)
 {
-	text	   *t = PG_GETARG_TEXT_PP(0);
-	StringInfoData buf;
-
-	pq_begintypsend(&buf);
-	pq_sendtext(&buf, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
-	PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+        text       *t = PG_GETARG_TEXT_PP(0);
+        const char* textData = VARDATA_ANY(t);
+        const int   textSize = VARSIZE_ANY_EXHDR(t);
+        char* textConverted = pg_server_to_client(textData, textSize);
+        //Logic based on pq_sendtext
+        if (textConverted == textData) {
+		PG_RETURN_BYTEA_P(t);
+        }else {
+		StringInfoData buf;
+		pq_begintypsend(&buf);
+		appendBinaryStringInfo(&buf, textConverted, strlen(textConverted));
+		pfree(textConverted);
+		PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
+	}
 }
 
 
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Radosław Smogura (#1)
Re: Varlena and binary

=?utf-8?q?Rados=C5=82aw_Smogura?= <mail@smogura.eu> writes:

I'm sending small patch for textsend. It reduces unnecessary copies, and
memory usage for duplication of varlena data. May you look?

This code will break the day that text and bytea don't have the same
internal representation, which seems likely to be soon. Barring some
compelling evidence of a major performance improvement obtainable this
way, I don't think we want this patch.

regards, tom lane

#3Radosław Smogura
rsmogura@softperience.eu
In reply to: Tom Lane (#2)
Re: Varlena and binary

Just from curious may I ask in which direction this will go, and how this will
affect performance of text and binary format?

Actually I started to make smaller improvements, and I think about one big to
encode text (when client and server encoding are different) directly to
StringInfo, without intermediate buffer.

Thanks in advice
Radek

Tom Lane <tgl@sss.pgh.pa.us> Monday 07 February 2011 17:12:07

Show quoted text

=?utf-8?q?Rados=C5=82aw_Smogura?= <mail@smogura.eu> writes:

I'm sending small patch for textsend. It reduces unnecessary copies, and
memory usage for duplication of varlena data. May you look?

This code will break the day that text and bytea don't have the same
internal representation, which seems likely to be soon. Barring some
compelling evidence of a major performance improvement obtainable this
way, I don't think we want this patch.

regards, tom lane

#4Robert Haas
robertmhaas@gmail.com
In reply to: Tom Lane (#2)
Re: Varlena and binary

On Mon, Feb 7, 2011 at 11:12 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:

=?utf-8?q?Rados=C5=82aw_Smogura?= <mail@smogura.eu> writes:

I'm sending small patch for textsend. It reduces unnecessary copies, and
memory usage for duplication of varlena data. May you look?

This code will break the day that text and bytea don't have the same
internal representation, which seems likely to be soon.

Oh, really?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company