[PATCH] backend: compare word-at-a-time in bcTruelen - Mailing list pgsql-hackers

From Jeremy Kerr
Subject [PATCH] backend: compare word-at-a-time in bcTruelen
Date
Msg-id 1245114277.71574.860187444713.1.gpush@pingu
Whole thread Raw
In response to Re: char() overhead on read-only workloads not so insignifcant as the docs claim it is...  (Alvaro Herrera <alvherre@commandprompt.com>)
Responses Re: [PATCH] backend: compare word-at-a-time in bcTruelen
Re: [PATCH] backend: compare word-at-a-time in bcTruelen
List pgsql-hackers
Signed-off-by: Jeremy Kerr <jk@ozlabs.org>

---src/backend/utils/adt/varchar.c |   24 +++++++++++++++++++++---1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c
index 5f3c658..6889dff 100644
--- a/src/backend/utils/adt/varchar.c
+++ b/src/backend/utils/adt/varchar.c
@@ -624,16 +624,34 @@ varchartypmodout(PG_FUNCTION_ARGS)static intbcTruelen(BpChar *arg){
+    const unsigned int spaces = 0x20202020;
+    const int    wordsize = sizeof(spaces);    char       *s = VARDATA_ANY(arg);    int            i;
-    int            len;
-    len = VARSIZE_ANY_EXHDR(arg);
-    for (i = len - 1; i >= 0; i--)
+    i = VARSIZE_ANY_EXHDR(arg) - 1;
+
+    /* compare down to an aligned boundary */
+    for (; i >= 0 && i % wordsize != wordsize - 1; i--)    {        if (s[i] != ' ')
+            return i + 1;
+    }
+
+    /* now that we're aligned, compare word at a time */
+    for (; i >= wordsize - 1; i -= wordsize)
+    {
+        if (*(unsigned int *)(s + i - (wordsize - 1)) != spaces)            break;    }
+
+    /* check within the last non-matching word */
+    for (; i >= 0; i--)
+    {
+        if (s[i] != ' ')
+            break;
+    }
+    return i + 1;}


pgsql-hackers by date:

Previous
From: Jeremy Kerr
Date:
Subject: Re: char() overhead on read-only workloads not so insignifcant as the docs claim it is...
Next
From: Robert Haas
Date:
Subject: Re: [PATCH] backend: compare word-at-a-time in bcTruelen