Skip to content

Commit 011d60c

Browse files
committed
Speed up uuid_out() by not relying on a StringInfo
Since the size of the string representation of an uuid is fixed, there is no benefit in using a StringInfo. This commit simplifies uuid_oud() to not rely on a StringInfo, where avoiding the overhead of the string manipulation makes the function substantially faster. A COPY TO on a relation with one UUID attribute can show up to a 40% speedup when the bottleneck is the COPY computation with uuid_out() showing up at the top of the profiles (numbered measure here, Laurenz has mentioned something closer to 20% faster runtimes), for example when the data is fully in shared buffers or the OS cache. Author: Laurenz Albe Reviewed-by: Andres Freund, Michael Paquier Description: https://postgr.es/m/679d5455cbbb0af667ccb753da51a475bae1eaed.camel@cybertec.at
1 parent 943f7ae commit 011d60c

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/backend/utils/adt/uuid.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,13 @@ uuid_out(PG_FUNCTION_ARGS)
5353
{
5454
pg_uuid_t *uuid = PG_GETARG_UUID_P(0);
5555
static const char hex_chars[] = "0123456789abcdef";
56-
StringInfoData buf;
56+
char *buf,
57+
*p;
5758
int i;
5859

59-
initStringInfo(&buf);
60+
/* counts for the four hyphens and the zero-terminator */
61+
buf = palloc(2 * UUID_LEN + 5);
62+
p = buf;
6063
for (i = 0; i < UUID_LEN; i++)
6164
{
6265
int hi;
@@ -68,16 +71,17 @@ uuid_out(PG_FUNCTION_ARGS)
6871
* ("-"). Therefore, add the hyphens at the appropriate places here.
6972
*/
7073
if (i == 4 || i == 6 || i == 8 || i == 10)
71-
appendStringInfoChar(&buf, '-');
74+
*p++ = '-';
7275

7376
hi = uuid->data[i] >> 4;
7477
lo = uuid->data[i] & 0x0F;
7578

76-
appendStringInfoChar(&buf, hex_chars[hi]);
77-
appendStringInfoChar(&buf, hex_chars[lo]);
79+
*p++ = hex_chars[hi];
80+
*p++ = hex_chars[lo];
7881
}
82+
*p = '\0';
7983

80-
PG_RETURN_CSTRING(buf.data);
84+
PG_RETURN_CSTRING(buf);
8185
}
8286

8387
/*

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy