Skip to content

Commit 8c792fe

Browse files
committed
At the head of wchareq, length of (multibyte) character is compared by
using pg_mblen. Therefore, pg_mblen is executed many times, and it becomes a bottleneck. This patch makes a short cut, and reduces execution frequency of pg_mblen by comparing the first byte first. a_ogawa
1 parent bbb586f commit 8c792fe

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/backend/utils/adt/like.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Portions Copyright (c) 1994, Regents of the University of California
1212
*
1313
* IDENTIFICATION
14-
* $PostgreSQL: pgsql/src/backend/utils/adt/like.c,v 1.59 2004/12/31 22:01:22 pgsql Exp $
14+
* $PostgreSQL: pgsql/src/backend/utils/adt/like.c,v 1.60 2005/05/25 22:59:33 momjian Exp $
1515
*
1616
*-------------------------------------------------------------------------
1717
*/
@@ -50,12 +50,18 @@ static text *MB_do_like_escape(text *, text *);
5050
static int
5151
wchareq(unsigned char *p1, unsigned char *p2)
5252
{
53-
int l;
53+
int p1_len;
5454

55-
l = pg_mblen(p1);
56-
if (pg_mblen(p2) != l)
55+
/* Optimization: quickly compare the first byte. */
56+
if(*p1 != *p2)
5757
return (0);
58-
while (l--)
58+
59+
p1_len = pg_mblen(p1);
60+
if (pg_mblen(p2) != p1_len)
61+
return (0);
62+
63+
/* They are the same length */
64+
while (p1_len--)
5965
{
6066
if (*p1++ != *p2++)
6167
return (0);

0 commit comments

Comments
 (0)