Skip to content

Commit 1fd687a

Browse files
committed
Optimizations for integer to decimal output.
Using a lookup table of digit pairs reduces the number of divisions needed, and calculating the length upfront saves some work; these ideas are taken from the code previously committed for floats. David Fetter, reviewed by Kyotaro Horiguchi, Tels, and me. Discussion: https://postgr.es/m/20190924052620.GP31596%40fetter.org
1 parent 7bae0ad commit 1fd687a

File tree

5 files changed

+287
-227
lines changed

5 files changed

+287
-227
lines changed

src/backend/access/common/printsimple.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ printsimple(TupleTableSlot *slot, DestReceiver *self)
112112
case INT8OID:
113113
{
114114
int64 num = DatumGetInt64(value);
115-
char str[23]; /* sign, 21 digits and '\0' */
115+
char str[MAXINT8LEN + 1];
116116

117117
pg_lltoa(num, str);
118118
pq_sendcountedtext(&buf, str, strlen(str), false);

src/backend/utils/adt/datetime.c

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,9 @@ AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
388388
Assert(precision >= 0);
389389

390390
if (fillzeros)
391-
cp = pg_ltostr_zeropad(cp, Abs(sec), 2);
391+
cp = pg_ultostr_zeropad(cp, Abs(sec), 2);
392392
else
393-
cp = pg_ltostr(cp, Abs(sec));
393+
cp = pg_ultostr(cp, Abs(sec));
394394

395395
/* fsec_t is just an int32 */
396396
if (fsec != 0)
@@ -430,7 +430,7 @@ AppendSeconds(char *cp, int sec, fsec_t fsec, int precision, bool fillzeros)
430430
* which will generate a correct answer in the minimum valid width.
431431
*/
432432
if (value)
433-
return pg_ltostr(cp, Abs(fsec));
433+
return pg_ultostr(cp, Abs(fsec));
434434

435435
return end;
436436
}
@@ -3831,20 +3831,20 @@ EncodeTimezone(char *str, int tz, int style)
38313831

38323832
if (sec != 0)
38333833
{
3834-
str = pg_ltostr_zeropad(str, hour, 2);
3834+
str = pg_ultostr_zeropad(str, hour, 2);
38353835
*str++ = ':';
3836-
str = pg_ltostr_zeropad(str, min, 2);
3836+
str = pg_ultostr_zeropad(str, min, 2);
38373837
*str++ = ':';
3838-
str = pg_ltostr_zeropad(str, sec, 2);
3838+
str = pg_ultostr_zeropad(str, sec, 2);
38393839
}
38403840
else if (min != 0 || style == USE_XSD_DATES)
38413841
{
3842-
str = pg_ltostr_zeropad(str, hour, 2);
3842+
str = pg_ultostr_zeropad(str, hour, 2);
38433843
*str++ = ':';
3844-
str = pg_ltostr_zeropad(str, min, 2);
3844+
str = pg_ultostr_zeropad(str, min, 2);
38453845
}
38463846
else
3847-
str = pg_ltostr_zeropad(str, hour, 2);
3847+
str = pg_ultostr_zeropad(str, hour, 2);
38483848
return str;
38493849
}
38503850

@@ -3861,40 +3861,40 @@ EncodeDateOnly(struct pg_tm *tm, int style, char *str)
38613861
case USE_ISO_DATES:
38623862
case USE_XSD_DATES:
38633863
/* compatible with ISO date formats */
3864-
str = pg_ltostr_zeropad(str,
3864+
str = pg_ultostr_zeropad(str,
38653865
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
38663866
*str++ = '-';
3867-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
3867+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
38683868
*str++ = '-';
3869-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
3869+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
38703870
break;
38713871

38723872
case USE_SQL_DATES:
38733873
/* compatible with Oracle/Ingres date formats */
38743874
if (DateOrder == DATEORDER_DMY)
38753875
{
3876-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
3876+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
38773877
*str++ = '/';
3878-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
3878+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
38793879
}
38803880
else
38813881
{
3882-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
3882+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
38833883
*str++ = '/';
3884-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
3884+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
38853885
}
38863886
*str++ = '/';
3887-
str = pg_ltostr_zeropad(str,
3887+
str = pg_ultostr_zeropad(str,
38883888
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
38893889
break;
38903890

38913891
case USE_GERMAN_DATES:
38923892
/* German-style date format */
3893-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
3893+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
38943894
*str++ = '.';
3895-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
3895+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
38963896
*str++ = '.';
3897-
str = pg_ltostr_zeropad(str,
3897+
str = pg_ultostr_zeropad(str,
38983898
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
38993899
break;
39003900

@@ -3903,18 +3903,18 @@ EncodeDateOnly(struct pg_tm *tm, int style, char *str)
39033903
/* traditional date-only style for Postgres */
39043904
if (DateOrder == DATEORDER_DMY)
39053905
{
3906-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
3906+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
39073907
*str++ = '-';
3908-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
3908+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
39093909
}
39103910
else
39113911
{
3912-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
3912+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
39133913
*str++ = '-';
3914-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
3914+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
39153915
}
39163916
*str++ = '-';
3917-
str = pg_ltostr_zeropad(str,
3917+
str = pg_ultostr_zeropad(str,
39183918
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
39193919
break;
39203920
}
@@ -3939,9 +3939,9 @@ EncodeDateOnly(struct pg_tm *tm, int style, char *str)
39393939
void
39403940
EncodeTimeOnly(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, int style, char *str)
39413941
{
3942-
str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
3942+
str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
39433943
*str++ = ':';
3944-
str = pg_ltostr_zeropad(str, tm->tm_min, 2);
3944+
str = pg_ultostr_zeropad(str, tm->tm_min, 2);
39453945
*str++ = ':';
39463946
str = AppendSeconds(str, tm->tm_sec, fsec, MAX_TIME_PRECISION, true);
39473947
if (print_tz)
@@ -3984,16 +3984,16 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
39843984
case USE_ISO_DATES:
39853985
case USE_XSD_DATES:
39863986
/* Compatible with ISO-8601 date formats */
3987-
str = pg_ltostr_zeropad(str,
3987+
str = pg_ultostr_zeropad(str,
39883988
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
39893989
*str++ = '-';
3990-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
3990+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
39913991
*str++ = '-';
3992-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
3992+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
39933993
*str++ = (style == USE_ISO_DATES) ? ' ' : 'T';
3994-
str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
3994+
str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
39953995
*str++ = ':';
3996-
str = pg_ltostr_zeropad(str, tm->tm_min, 2);
3996+
str = pg_ultostr_zeropad(str, tm->tm_min, 2);
39973997
*str++ = ':';
39983998
str = AppendTimestampSeconds(str, tm, fsec);
39993999
if (print_tz)
@@ -4004,23 +4004,23 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
40044004
/* Compatible with Oracle/Ingres date formats */
40054005
if (DateOrder == DATEORDER_DMY)
40064006
{
4007-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
4007+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
40084008
*str++ = '/';
4009-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
4009+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
40104010
}
40114011
else
40124012
{
4013-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
4013+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
40144014
*str++ = '/';
4015-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
4015+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
40164016
}
40174017
*str++ = '/';
4018-
str = pg_ltostr_zeropad(str,
4018+
str = pg_ultostr_zeropad(str,
40194019
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
40204020
*str++ = ' ';
4021-
str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
4021+
str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
40224022
*str++ = ':';
4023-
str = pg_ltostr_zeropad(str, tm->tm_min, 2);
4023+
str = pg_ultostr_zeropad(str, tm->tm_min, 2);
40244024
*str++ = ':';
40254025
str = AppendTimestampSeconds(str, tm, fsec);
40264026

@@ -4043,16 +4043,16 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
40434043

40444044
case USE_GERMAN_DATES:
40454045
/* German variant on European style */
4046-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
4046+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
40474047
*str++ = '.';
4048-
str = pg_ltostr_zeropad(str, tm->tm_mon, 2);
4048+
str = pg_ultostr_zeropad(str, tm->tm_mon, 2);
40494049
*str++ = '.';
4050-
str = pg_ltostr_zeropad(str,
4050+
str = pg_ultostr_zeropad(str,
40514051
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
40524052
*str++ = ' ';
4053-
str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
4053+
str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
40544054
*str++ = ':';
4055-
str = pg_ltostr_zeropad(str, tm->tm_min, 2);
4055+
str = pg_ultostr_zeropad(str, tm->tm_min, 2);
40564056
*str++ = ':';
40574057
str = AppendTimestampSeconds(str, tm, fsec);
40584058

@@ -4078,7 +4078,7 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
40784078
*str++ = ' ';
40794079
if (DateOrder == DATEORDER_DMY)
40804080
{
4081-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
4081+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
40824082
*str++ = ' ';
40834083
memcpy(str, months[tm->tm_mon - 1], 3);
40844084
str += 3;
@@ -4088,16 +4088,16 @@ EncodeDateTime(struct pg_tm *tm, fsec_t fsec, bool print_tz, int tz, const char
40884088
memcpy(str, months[tm->tm_mon - 1], 3);
40894089
str += 3;
40904090
*str++ = ' ';
4091-
str = pg_ltostr_zeropad(str, tm->tm_mday, 2);
4091+
str = pg_ultostr_zeropad(str, tm->tm_mday, 2);
40924092
}
40934093
*str++ = ' ';
4094-
str = pg_ltostr_zeropad(str, tm->tm_hour, 2);
4094+
str = pg_ultostr_zeropad(str, tm->tm_hour, 2);
40954095
*str++ = ':';
4096-
str = pg_ltostr_zeropad(str, tm->tm_min, 2);
4096+
str = pg_ultostr_zeropad(str, tm->tm_min, 2);
40974097
*str++ = ':';
40984098
str = AppendTimestampSeconds(str, tm, fsec);
40994099
*str++ = ' ';
4100-
str = pg_ltostr_zeropad(str,
4100+
str = pg_ultostr_zeropad(str,
41014101
(tm->tm_year > 0) ? tm->tm_year : -(tm->tm_year - 1), 4);
41024102

41034103
if (print_tz)

src/backend/utils/adt/int8.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "utils/builtins.h"
2727
#include "utils/int8.h"
2828

29-
#define MAXINT8LEN 25
3029

3130
typedef struct
3231
{

0 commit comments

Comments
 (0)