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

Commitdad75eb

Browse files
committed
Have pg_itoa, pg_ltoa and pg_lltoa return the length of the string
Core by no means makes excessive use of these functions, but quite a largenumber of those usages do require the caller to call strlen() on thereturned string. This is quite wasteful since these functions do alreadyhave a good idea of the length of the string, so we might as well justhave them return that.Reviewed-by: Andrew GierthDiscussion:https://postgr.es/m/CAApHDvrm2A5x2uHYxsqriO2cUaGcFvND%2BksC9e7Tjep0t2RK_A%40mail.gmail.com
1 parent9a7fccd commitdad75eb

File tree

5 files changed

+38
-25
lines changed

5 files changed

+38
-25
lines changed

‎src/backend/access/common/printsimple.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,19 +103,21 @@ printsimple(TupleTableSlot *slot, DestReceiver *self)
103103
{
104104
int32num=DatumGetInt32(value);
105105
charstr[12];/* sign, 10 digits and '\0' */
106+
intlen;
106107

107-
pg_ltoa(num,str);
108-
pq_sendcountedtext(&buf,str,strlen(str), false);
108+
len=pg_ltoa(num,str);
109+
pq_sendcountedtext(&buf,str,len, false);
109110
}
110111
break;
111112

112113
caseINT8OID:
113114
{
114115
int64num=DatumGetInt64(value);
115116
charstr[MAXINT8LEN+1];
117+
intlen;
116118

117-
pg_lltoa(num,str);
118-
pq_sendcountedtext(&buf,str,strlen(str), false);
119+
len=pg_lltoa(num,str);
120+
pq_sendcountedtext(&buf,str,len, false);
119121
}
120122
break;
121123

‎src/backend/utils/adt/int.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,7 @@ int2vectorout(PG_FUNCTION_ARGS)
191191
{
192192
if (num!=0)
193193
*rp++=' ';
194-
pg_itoa(int2Array->values[num],rp);
195-
while (*++rp!='\0')
196-
;
194+
rp+=pg_itoa(int2Array->values[num],rp);
197195
}
198196
*rp='\0';
199197
PG_RETURN_CSTRING(result);

‎src/backend/utils/adt/int8.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,16 @@ int8out(PG_FUNCTION_ARGS)
149149
int64val=PG_GETARG_INT64(0);
150150
charbuf[MAXINT8LEN+1];
151151
char*result;
152+
intlen;
152153

153-
pg_lltoa(val,buf);
154-
result=pstrdup(buf);
154+
len=pg_lltoa(val,buf)+1;
155+
156+
/*
157+
* Since the length is already known, we do a manual palloc() and memcpy()
158+
* to avoid the strlen() call that would otherwise be done in pstrdup().
159+
*/
160+
result=palloc(len);
161+
memcpy(result,buf,len);
155162
PG_RETURN_CSTRING(result);
156163
}
157164

‎src/backend/utils/adt/numutils.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -327,16 +327,17 @@ pg_strtoint32(const char *s)
327327

328328
/*
329329
* pg_itoa: converts a signed 16-bit integer to its string representation
330+
* and returns strlen(a).
330331
*
331332
* Caller must ensure that 'a' points to enough memory to hold the result
332333
* (at least 7 bytes, counting a leading sign and trailing NUL).
333334
*
334335
* It doesn't seem worth implementing this separately.
335336
*/
336-
void
337+
int
337338
pg_itoa(int16i,char*a)
338339
{
339-
pg_ltoa((int32)i,a);
340+
returnpg_ltoa((int32)i,a);
340341
}
341342

342343
/*
@@ -404,25 +405,27 @@ pg_ultoa_n(uint32 value, char *a)
404405
}
405406

406407
/*
407-
* NUL-terminate the output of pg_ultoa_n.
408+
* pg_ltoa: converts a signed 32-bit integer to its string representation and
409+
* returns strlen(a).
408410
*
409411
* It is the caller's responsibility to ensure that a is at least 12 bytes long,
410412
* which is enough room to hold a minus sign, a maximally long int32, and the
411413
* above terminating NUL.
412414
*/
413-
void
415+
int
414416
pg_ltoa(int32value,char*a)
415417
{
416418
uint32uvalue= (uint32)value;
417-
intlen;
419+
intlen=0;
418420

419421
if (value<0)
420422
{
421423
uvalue= (uint32)0-uvalue;
422-
*a++='-';
424+
a[len++]='-';
423425
}
424-
len=pg_ultoa_n(uvalue,a);
426+
len+=pg_ultoa_n(uvalue,a+len);
425427
a[len]='\0';
428+
returnlen;
426429
}
427430

428431
/*
@@ -510,24 +513,27 @@ pg_ulltoa_n(uint64 value, char *a)
510513
}
511514

512515
/*
513-
* pg_lltoa: convert a signed 64-bit integer to its string representation
516+
* pg_lltoa: converts a signed 64-bit integer to its string representation and
517+
* returns strlen(a).
514518
*
515519
* Caller must ensure that 'a' points to enough memory to hold the result
516520
* (at least MAXINT8LEN + 1 bytes, counting a leading sign and trailing NUL).
517521
*/
518-
void
522+
int
519523
pg_lltoa(int64value,char*a)
520524
{
521-
intlen;
522525
uint64uvalue=value;
526+
intlen=0;
523527

524528
if (value<0)
525529
{
526-
*a++='-';
527530
uvalue= (uint64)0-uvalue;
531+
a[len++]='-';
528532
}
529-
len=pg_ulltoa_n(uvalue,a);
530-
a[len]=0;
533+
534+
len+=pg_ulltoa_n(uvalue,a+len);
535+
a[len]='\0';
536+
returnlen;
531537
}
532538

533539

‎src/include/utils/builtins.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ extern intnamestrcmp(Name name, const char *str);
4747
externint32pg_atoi(constchar*s,intsize,intc);
4848
externint16pg_strtoint16(constchar*s);
4949
externint32pg_strtoint32(constchar*s);
50-
externvoidpg_itoa(int16i,char*a);
50+
externintpg_itoa(int16i,char*a);
5151
externintpg_ultoa_n(uint32l,char*a);
5252
externintpg_ulltoa_n(uint64l,char*a);
53-
externvoidpg_ltoa(int32l,char*a);
54-
externvoidpg_lltoa(int64ll,char*a);
53+
externintpg_ltoa(int32l,char*a);
54+
externintpg_lltoa(int64ll,char*a);
5555
externchar*pg_ultostr_zeropad(char*str,uint32value,int32minwidth);
5656
externchar*pg_ultostr(char*str,uint32value);
5757
externuint64pg_strtouint64(constchar*str,char**endptr,intbase);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp