From 4e808c82ef5ed0ee261d20b86cb2f861982f049d Mon Sep 17 00:00:00 2001
From: Per Hedbor <ph@opera.com>
Date: Sun, 1 May 2011 05:20:50 +0200
Subject: [PATCH] Check strings for magnitude in the forward, not backward,
 direction.

This saves a few % of CPU for short (<1Kb) strings, but not for long ones
(I guess the hardware prefetcher catches on rather quickly)
---
 src/stralloc.c | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)

diff --git a/src/stralloc.c b/src/stralloc.c
index 34b798e4f7..a09c56bba8 100644
--- a/src/stralloc.c
+++ b/src/stralloc.c
@@ -102,7 +102,7 @@ PMOD_EXPORT struct pike_string *empty_pike_string = 0;
 
 #define StrHash(s,len) low_do_hash(s,len,0)
 
-static INLINE size_t low_do_hash(const void *s,
+static size_t low_do_hash(const void *s,
 				 ptrdiff_t len__,
 				 int size_shift)
 {
@@ -119,23 +119,25 @@ static INLINE size_t do_hash(struct pike_string *s)
 
 static INLINE int find_magnitude1(const p_wchar1 *s, ptrdiff_t len)
 {
-  while(--len>=0)
-    if(s[len]>=256)
+  const p_wchar1 *e=s+len;
+  while(s<e)
+    if(*s++>=256)
       return 1;
   return 0;
 }
 
 static INLINE int find_magnitude2(const p_wchar2 *s, ptrdiff_t len)
 {
-  while(--len>=0)
+  const p_wchar2 *e=s+len;
+  while(s<e)
   {    
-    if((unsigned INT32)s[len]>=256)
+    if((unsigned INT32)*s++>=256)
     {
       do
       {
-	if((unsigned INT32)s[len]>=65536)
+	if((unsigned INT32)*s++>=65536)
 	  return 2;
-      }while(--len>=0);
+      }while(s<e);
       return 1;
     }
   }
-- 
GitLab