At the head of wchareq, length of (multibyte) character is compared by
authorBruce Momjian <[email protected]>
Wed, 25 May 2005 22:59:33 +0000 (22:59 +0000)
committerBruce Momjian <[email protected]>
Wed, 25 May 2005 22:59:33 +0000 (22:59 +0000)
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

src/backend/utils/adt/like.c

index eb6018498a9f4a03a277ac2f210909a5db2b9fdc..367dd69dea0dcfd27c6e9595cda900411cde5df4 100644 (file)
@@ -50,12 +50,18 @@ static text *MB_do_like_escape(text *, text *);
 static int
 wchareq(unsigned char *p1, unsigned char *p2)
 {
-       int                     l;
+       int                     p1_len;
 
-       l = pg_mblen(p1);
-       if (pg_mblen(p2) != l)
+       /* Optimization:  quickly compare the first byte. */
+       if(*p1 != *p2)
                return (0);
-       while (l--)
+
+       p1_len = pg_mblen(p1);
+       if (pg_mblen(p2) != p1_len)
+               return (0);
+
+       /* They are the same length */
+       while (p1_len--)
        {
                if (*p1++ != *p2++)
                        return (0);