Commit 70c2d1be authored by Andres Freund's avatar Andres Freund

Allow to avoid NUL-byte management for stringinfos and use in format.c.

In a lot of the places having appendBinaryStringInfo() maintain a
trailing NUL byte wasn't actually meaningful, e.g. when appending an
integer which can contain 0 in one of its bytes.

Removing this yields some small speedup, but more importantly will be
more consistent when providing faster variants of pq_sendint etc.

Author: Andres Freund
Discussion: https://postgr.es/m/20170914063418.sckdzgjfrsbekae4@alap3.anarazel.de
parent 0b974dba
...@@ -202,7 +202,7 @@ appendStringInfoSpaces(StringInfo str, int count) ...@@ -202,7 +202,7 @@ appendStringInfoSpaces(StringInfo str, int count)
* appendBinaryStringInfo * appendBinaryStringInfo
* *
* Append arbitrary binary data to a StringInfo, allocating more space * Append arbitrary binary data to a StringInfo, allocating more space
* if necessary. * if necessary. Ensures that a trailing null byte is present.
*/ */
void void
appendBinaryStringInfo(StringInfo str, const char *data, int datalen) appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
...@@ -224,6 +224,25 @@ appendBinaryStringInfo(StringInfo str, const char *data, int datalen) ...@@ -224,6 +224,25 @@ appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
str->data[str->len] = '\0'; str->data[str->len] = '\0';
} }
/*
* appendBinaryStringInfoNT
*
* Append arbitrary binary data to a StringInfo, allocating more space
* if necessary. Does not ensure a trailing null-byte exists.
*/
void
appendBinaryStringInfoNT(StringInfo str, const char *data, int datalen)
{
Assert(str != NULL);
/* Make more room if needed */
enlargeStringInfo(str, datalen);
/* OK, append the data */
memcpy(str->data + str->len, data, datalen);
str->len += datalen;
}
/* /*
* enlargeStringInfo * enlargeStringInfo
* *
......
...@@ -138,13 +138,13 @@ pq_sendcountedtext(StringInfo buf, const char *str, int slen, ...@@ -138,13 +138,13 @@ pq_sendcountedtext(StringInfo buf, const char *str, int slen,
{ {
slen = strlen(p); slen = strlen(p);
pq_sendint(buf, slen + extra, 4); pq_sendint(buf, slen + extra, 4);
appendBinaryStringInfo(buf, p, slen); appendBinaryStringInfoNT(buf, p, slen);
pfree(p); pfree(p);
} }
else else
{ {
pq_sendint(buf, slen + extra, 4); pq_sendint(buf, slen + extra, 4);
appendBinaryStringInfo(buf, str, slen); appendBinaryStringInfoNT(buf, str, slen);
} }
} }
...@@ -191,11 +191,11 @@ pq_sendstring(StringInfo buf, const char *str) ...@@ -191,11 +191,11 @@ pq_sendstring(StringInfo buf, const char *str)
if (p != str) /* actual conversion has been done? */ if (p != str) /* actual conversion has been done? */
{ {
slen = strlen(p); slen = strlen(p);
appendBinaryStringInfo(buf, p, slen + 1); appendBinaryStringInfoNT(buf, p, slen + 1);
pfree(p); pfree(p);
} }
else else
appendBinaryStringInfo(buf, str, slen + 1); appendBinaryStringInfoNT(buf, str, slen + 1);
} }
/* -------------------------------- /* --------------------------------
...@@ -242,15 +242,15 @@ pq_sendint(StringInfo buf, int i, int b) ...@@ -242,15 +242,15 @@ pq_sendint(StringInfo buf, int i, int b)
{ {
case 1: case 1:
n8 = (unsigned char) i; n8 = (unsigned char) i;
appendBinaryStringInfo(buf, (char *) &n8, 1); appendBinaryStringInfoNT(buf, (char *) &n8, 1);
break; break;
case 2: case 2:
n16 = pg_hton16((uint16) i); n16 = pg_hton16((uint16) i);
appendBinaryStringInfo(buf, (char *) &n16, 2); appendBinaryStringInfoNT(buf, (char *) &n16, 2);
break; break;
case 4: case 4:
n32 = pg_hton32((uint32) i); n32 = pg_hton32((uint32) i);
appendBinaryStringInfo(buf, (char *) &n32, 4); appendBinaryStringInfoNT(buf, (char *) &n32, 4);
break; break;
default: default:
elog(ERROR, "unsupported integer size %d", b); elog(ERROR, "unsupported integer size %d", b);
...@@ -271,7 +271,7 @@ pq_sendint64(StringInfo buf, int64 i) ...@@ -271,7 +271,7 @@ pq_sendint64(StringInfo buf, int64 i)
{ {
uint64 n64 = pg_hton64(i); uint64 n64 = pg_hton64(i);
appendBinaryStringInfo(buf, (char *) &n64, sizeof(n64)); appendBinaryStringInfoNT(buf, (char *) &n64, sizeof(n64));
} }
/* -------------------------------- /* --------------------------------
...@@ -297,7 +297,7 @@ pq_sendfloat4(StringInfo buf, float4 f) ...@@ -297,7 +297,7 @@ pq_sendfloat4(StringInfo buf, float4 f)
swap.f = f; swap.f = f;
swap.i = pg_hton32(swap.i); swap.i = pg_hton32(swap.i);
appendBinaryStringInfo(buf, (char *) &swap.i, 4); appendBinaryStringInfoNT(buf, (char *) &swap.i, 4);
} }
/* -------------------------------- /* --------------------------------
......
...@@ -143,6 +143,14 @@ extern void appendStringInfoSpaces(StringInfo str, int count); ...@@ -143,6 +143,14 @@ extern void appendStringInfoSpaces(StringInfo str, int count);
extern void appendBinaryStringInfo(StringInfo str, extern void appendBinaryStringInfo(StringInfo str,
const char *data, int datalen); const char *data, int datalen);
/*------------------------
* appendBinaryStringInfoNT
* Append arbitrary binary data to a StringInfo, allocating more space
* if necessary. Does not ensure a trailing null-byte exists.
*/
extern void appendBinaryStringInfoNT(StringInfo str,
const char *data, int datalen);
/*------------------------ /*------------------------
* enlargeStringInfo * enlargeStringInfo
* Make sure a StringInfo's buffer can hold at least 'needed' more bytes. * Make sure a StringInfo's buffer can hold at least 'needed' more bytes.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment