Skip to content

Commit e24d770

Browse files
committed
Speed up hex_encode with bytewise lookup
Previously, hex_encode looked up each nibble of the input separately. We now use a larger lookup table containing the two-byte encoding of every possible input byte, resulting in a 1/3 reduction in encoding time. Reviewed by Tom Lane, Michael Paquier, Nathan Bossart, David Rowley Discussion: https://postgr.es/m/CANWCAZZvXuJMgqMN4u068Yqa19CEjS31tQKZp_qFFFbgYfaXqQ%40mail.gmail.com
1 parent 0869ea4 commit e24d770

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/backend/utils/adt/encode.c

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,26 @@ binary_decode(PG_FUNCTION_ARGS)
145145
* HEX
146146
*/
147147

148-
static const char hextbl[] = "0123456789abcdef";
148+
/*
149+
* The hex expansion of each possible byte value (two chars per value).
150+
*/
151+
static const char hextbl[512] =
152+
"000102030405060708090a0b0c0d0e0f"
153+
"101112131415161718191a1b1c1d1e1f"
154+
"202122232425262728292a2b2c2d2e2f"
155+
"303132333435363738393a3b3c3d3e3f"
156+
"404142434445464748494a4b4c4d4e4f"
157+
"505152535455565758595a5b5c5d5e5f"
158+
"606162636465666768696a6b6c6d6e6f"
159+
"707172737475767778797a7b7c7d7e7f"
160+
"808182838485868788898a8b8c8d8e8f"
161+
"909192939495969798999a9b9c9d9e9f"
162+
"a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
163+
"b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
164+
"c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
165+
"d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
166+
"e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
167+
"f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
149168

150169
static const int8 hexlookup[128] = {
151170
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -165,9 +184,11 @@ hex_encode(const char *src, size_t len, char *dst)
165184

166185
while (src < end)
167186
{
168-
*dst++ = hextbl[(*src >> 4) & 0xF];
169-
*dst++ = hextbl[*src & 0xF];
187+
unsigned char usrc = *((const unsigned char *) src);
188+
189+
memcpy(dst, &hextbl[2 * usrc], 2);
170190
src++;
191+
dst += 2;
171192
}
172193
return (uint64) len * 2;
173194
}

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