Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit70c2d1b

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 atrailing NUL byte wasn't actually meaningful, e.g. when appending aninteger which can contain 0 in one of its bytes.Removing this yields some small speedup, but more importantly will bemore consistent when providing faster variants of pq_sendint etc.Author: Andres FreundDiscussion:https://postgr.es/m/20170914063418.sckdzgjfrsbekae4@alap3.anarazel.de
1 parent0b974db commit70c2d1b

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

‎src/backend/lib/stringinfo.c

Lines changed: 20 additions & 1 deletion
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(StringInfostr,constchar*data,intdatalen)
@@ -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(StringInfostr,constchar*data,intdatalen)
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

Lines changed: 9 additions & 9 deletions
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
case1:
244244
n8= (unsignedchar)i;
245-
appendBinaryStringInfo(buf, (char*)&n8,1);
245+
appendBinaryStringInfoNT(buf, (char*)&n8,1);
246246
break;
247247
case2:
248248
n16=pg_hton16((uint16)i);
249-
appendBinaryStringInfo(buf, (char*)&n16,2);
249+
appendBinaryStringInfoNT(buf, (char*)&n16,2);
250250
break;
251251
case4:
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
uint64n64=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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,14 @@ extern void appendStringInfoSpaces(StringInfo str, int count);
143143
externvoidappendBinaryStringInfo(StringInfostr,
144144
constchar*data,intdatalen);
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+
externvoidappendBinaryStringInfoNT(StringInfostr,
152+
constchar*data,intdatalen);
153+
146154
/*------------------------
147155
* enlargeStringInfo
148156
* Make sure a StringInfo's buffer can hold at least 'needed' more bytes.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp