Commit 4fc115b2 authored by Robert Haas's avatar Robert Haas

Speed up conversion of signed integers to C strings.

A hand-coded implementation turns out to be much faster than calling
printf().  In passing, add a few more regresion tests.

Andres Freund, with assorted, mostly cosmetic changes.
parent 0f61d4dd
......@@ -20,6 +20,7 @@
#include "funcapi.h"
#include "libpq/pqformat.h"
#include "utils/int8.h"
#include "utils/builtins.h"
#define MAXINT8LEN 25
......@@ -157,13 +158,10 @@ Datum
int8out(PG_FUNCTION_ARGS)
{
int64 val = PG_GETARG_INT64(0);
char *result;
int len;
char buf[MAXINT8LEN + 1];
char *result;
if ((len = snprintf(buf, MAXINT8LEN, INT64_FORMAT, val)) < 0)
elog(ERROR, "could not format int8");
pg_lltoa(val, buf);
result = pstrdup(buf);
PG_RETURN_CSTRING(result);
}
......
......@@ -3,8 +3,6 @@
* numutils.c
* utility functions for I/O of built-in numeric types.
*
* integer: pg_atoi, pg_itoa, pg_ltoa
*
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
......@@ -109,27 +107,118 @@ pg_atoi(char *s, int size, int c)
}
/*
* pg_itoa - converts a short int to its string represention
* pg_itoa: converts a signed 16-bit integer to its string representation
*
* Caller must ensure that 'a' points to enough memory to hold the result
* (at least 7 bytes, counting a leading sign and trailing NUL).
*
* Note:
* previously based on ~ingres/source/gutil/atoi.c
* now uses vendor's sprintf conversion
* It doesn't seem worth implementing this separately.
*/
void
pg_itoa(int16 i, char *a)
{
sprintf(a, "%hd", (short) i);
pg_ltoa((int32)i, a);
}
/*
* pg_ltoa: converts a signed 32-bit integer to its string representation
*
* Caller must ensure that 'a' points to enough memory to hold the result
* (at least 12 bytes, counting a leading sign and trailing NUL).
*/
void
pg_ltoa(int32 value, char *a)
{
char *start = a;
bool neg = false;
/*
* Avoid problems with the most negative integer not being representable
* as a positive integer.
*/
if (value == INT32_MIN)
{
memcpy(a, "-2147483648", 12);
return;
}
else if (value < 0)
{
value = -value;
neg = true;
}
/* Compute the result backwards. */
do
{
int32 remainder;
int32 oldval = value;
value /= 10;
remainder = oldval - value * 10;
*a++ = '0' + remainder;
} while (value != 0);
if (neg)
*a++ = '-';
/* Add trailing NUL byte. */
*a-- = '\0';
/* reverse string */
while (start < a)
{
char swap = *start;
*start++ = *a;
*a-- = swap;
}
}
/*
* pg_ltoa - converts a long int to its string represention
* pg_lltoa: convert a signed 64bit integer to its string representation
*
* Note:
* previously based on ~ingres/source/gutil/atoi.c
* now uses vendor's sprintf conversion
* Caller must ensure that 'a' points to enough memory to hold the result
* (at least MAXINT8LEN+1 bytes, counting a leading sign and trailing NUL).
*/
void
pg_ltoa(int32 l, char *a)
pg_lltoa(int64 value, char *a)
{
sprintf(a, "%d", l);
char *start = a;
bool neg = false;
/*
* Avoid problems with the most negative integer not being representable
* as a positive integer.
*/
if (value == INT64_MIN)
{
memcpy(a, "-9223372036854775808", 21);
return;
}
else if (value < 0)
{
value = -value;
neg = true;
}
/* Build the string by computing the wanted string backwards. */
do
{
int64 remainder;
int64 oldval = value;
value /= 10;
remainder = oldval - value * 10;
*a++ = '0' + remainder;
} while (value != 0);
if (neg)
*a++ = '-';
/* Add trailing NUL byte. */
*a-- = '\0';
/* Reverse string. */
while (start < a)
{
char swap = *start;
*start++ = *a;
*a-- = swap;
}
}
......@@ -275,6 +275,7 @@ extern Datum current_schemas(PG_FUNCTION_ARGS);
extern int32 pg_atoi(char *s, int size, int c);
extern void pg_itoa(int16 i, char *a);
extern void pg_ltoa(int32 l, char *a);
extern void pg_lltoa(int64 ll, char *a);
/*
* Per-opclass comparison functions for new btrees. These are
......
......@@ -242,3 +242,16 @@ SELECT '' AS five, i.f1, i.f1 / int4 '2' AS x FROM INT2_TBL i;
| -32767 | -16383
(5 rows)
-- corner cases
SELECT (1<<15-1)::int2::text;
text
-------
16384
(1 row)
SELECT (-1<<15)::int2::text;
text
--------
-32768
(1 row)
......@@ -329,3 +329,16 @@ SELECT (2 + 2) / 2 AS two;
2
(1 row)
-- corner cases
SELECT (1<<31-1)::int4::text;
text
------------
1073741824
(1 row)
SELECT (1<<31)::int4::text;
text
-------------
-2147483648
(1 row)
......@@ -802,3 +802,16 @@ SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::in
4567890123456799
(6 rows)
-- corner cases
SELECT (1<<63-1)::int8::text;
text
------------
1073741824
(1 row)
SELECT (1<<63)::int8::text;
text
-------------
-2147483648
(1 row)
......@@ -83,3 +83,7 @@ SELECT '' AS five, i.f1, i.f1 - int4 '2' AS x FROM INT2_TBL i;
SELECT '' AS five, i.f1, i.f1 / int2 '2' AS x FROM INT2_TBL i;
SELECT '' AS five, i.f1, i.f1 / int4 '2' AS x FROM INT2_TBL i;
-- corner cases
SELECT (1<<15-1)::int2::text;
SELECT (-1<<15)::int2::text;
......@@ -123,3 +123,7 @@ SELECT 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 AS ten;
SELECT 2 + 2 / 2 AS three;
SELECT (2 + 2) / 2 AS two;
-- corner cases
SELECT (1<<31-1)::int4::text;
SELECT (1<<31)::int4::text;
......@@ -190,3 +190,7 @@ SELECT q1, q1 << 2 AS "shl", q1 >> 3 AS "shr" FROM INT8_TBL;
SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8);
SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 0);
SELECT * FROM generate_series('+4567890123456789'::int8, '+4567890123456799'::int8, 2);
-- corner cases
SELECT (1<<63-1)::int8::text;
SELECT (1<<63)::int8::text;
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