Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 70c2d1b

Browse files
committed
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
1 parent 0b974db commit 70c2d1b

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

src/backend/lib/stringinfo.c

+20-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ appendStringInfoSpaces(StringInfo str, int count)
202202
* appendBinaryStringInfo
203203
*
204204
* Append arbitrary binary data to a StringInfo, allocating more space
205-
* if necessary.
205+
* if necessary. Ensures that a trailing null byte is present.
206206
*/
207207
void
208208
appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
@@ -224,6 +224,25 @@ appendBinaryStringInfo(StringInfo str, const char *data, int datalen)
224224
str->data[str->len] = '\0';
225225
}
226226

227+
/*
228+
* appendBinaryStringInfoNT
229+
*
230+
* Append arbitrary binary data to a StringInfo, allocating more space
231+
* if necessary. Does not ensure a trailing null-byte exists.
232+
*/
233+
void
234+
appendBinaryStringInfoNT(StringInfo str, const char *data, int datalen)
235+
{
236+
Assert(str != NULL);
237+
238+
/* Make more room if needed */
239+
enlargeStringInfo(str, datalen);
240+
241+
/* OK, append the data */
242+
memcpy(str->data + str->len, data, datalen);
243+
str->len += datalen;
244+
}
245+
227246
/*
228247
* enlargeStringInfo
229248
*

src/backend/libpq/pqformat.c

+9-9
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,13 @@ pq_sendcountedtext(StringInfo buf, const char *str, int slen,
138138
{
139139
slen = strlen(p);
140140
pq_sendint(buf, slen + extra, 4);
141-
appendBinaryStringInfo(buf, p, slen);
141+
appendBinaryStringInfoNT(buf, p, slen);
142142
pfree(p);
143143
}
144144
else
145145
{
146146
pq_sendint(buf, slen + extra, 4);
147-
appendBinaryStringInfo(buf, str, slen);
147+
appendBinaryStringInfoNT(buf, str, slen);
148148
}
149149
}
150150

@@ -191,11 +191,11 @@ pq_sendstring(StringInfo buf, const char *str)
191191
if (p != str) /* actual conversion has been done? */
192192
{
193193
slen = strlen(p);
194-
appendBinaryStringInfo(buf, p, slen + 1);
194+
appendBinaryStringInfoNT(buf, p, slen + 1);
195195
pfree(p);
196196
}
197197
else
198-
appendBinaryStringInfo(buf, str, slen + 1);
198+
appendBinaryStringInfoNT(buf, str, slen + 1);
199199
}
200200

201201
/* --------------------------------
@@ -242,15 +242,15 @@ pq_sendint(StringInfo buf, int i, int b)
242242
{
243243
case 1:
244244
n8 = (unsigned char) i;
245-
appendBinaryStringInfo(buf, (char *) &n8, 1);
245+
appendBinaryStringInfoNT(buf, (char *) &n8, 1);
246246
break;
247247
case 2:
248248
n16 = pg_hton16((uint16) i);
249-
appendBinaryStringInfo(buf, (char *) &n16, 2);
249+
appendBinaryStringInfoNT(buf, (char *) &n16, 2);
250250
break;
251251
case 4:
252252
n32 = pg_hton32((uint32) i);
253-
appendBinaryStringInfo(buf, (char *) &n32, 4);
253+
appendBinaryStringInfoNT(buf, (char *) &n32, 4);
254254
break;
255255
default:
256256
elog(ERROR, "unsupported integer size %d", b);
@@ -271,7 +271,7 @@ pq_sendint64(StringInfo buf, int64 i)
271271
{
272272
uint64 n64 = pg_hton64(i);
273273

274-
appendBinaryStringInfo(buf, (char *) &n64, sizeof(n64));
274+
appendBinaryStringInfoNT(buf, (char *) &n64, sizeof(n64));
275275
}
276276

277277
/* --------------------------------
@@ -297,7 +297,7 @@ pq_sendfloat4(StringInfo buf, float4 f)
297297
swap.f = f;
298298
swap.i = pg_hton32(swap.i);
299299

300-
appendBinaryStringInfo(buf, (char *) &swap.i, 4);
300+
appendBinaryStringInfoNT(buf, (char *) &swap.i, 4);
301301
}
302302

303303
/* --------------------------------

src/include/lib/stringinfo.h

+8
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ extern void appendStringInfoSpaces(StringInfo str, int count);
143143
extern void appendBinaryStringInfo(StringInfo str,
144144
const char *data, int datalen);
145145

146+
/*------------------------
147+
* appendBinaryStringInfoNT
148+
* Append arbitrary binary data to a StringInfo, allocating more space
149+
* if necessary. Does not ensure a trailing null-byte exists.
150+
*/
151+
extern void appendBinaryStringInfoNT(StringInfo str,
152+
const char *data, int datalen);
153+
146154
/*------------------------
147155
* enlargeStringInfo
148156
* Make sure a StringInfo's buffer can hold at least 'needed' more bytes.

0 commit comments

Comments
 (0)