develooper Front page | perl.perl5.porters | Postings from January 2001

2% speedup with Duff's device

Thread Next
From:
Nicholas Clark
Date:
January 23, 2001 04:19
Subject:
2% speedup with Duff's device
Message ID:
20010123121946.U72556@plum.flirble.org
If one uses Duff's device in PERL_HASH, the regression tests go about 2%
faster.

Without:

All tests successful.
u=1.33  s=0.46  cu=62.27  cs=10.54  scripts=273  tests=21120

real    2m16.388s
user    1m3.610s
sys     0m11.000s

With:

All tests successful.
u=1.4  s=0.32  cu=59.86  cs=10.71  scripts=273  tests=21120

real    2m11.748s
user    1m1.260s
sys     0m11.030s


perl gets about .25% bigger:

-rwxrwxr-x    1 nick     1001       785667 Jan 20 15:36 bleadperl/perl
-rwxrwxr-x    1 nick     1001       787683 Jan 23 11:51 bleadperl-duff/perl

[both 8462, same everything except -DHASH_DUFF in the latter]


I've not experimented with unrolling to 4 or 16.

Nicholas Clark


--- hv.h.orig	Mon Jan  1 00:02:30 2001
+++ hv.h	Tue Jan 23 11:46:34 2001
@@ -43,6 +43,7 @@
 };
 
 /* hash a key */
+#ifndef HASH_DUFF
 #define PERL_HASH(hash,str,len) \
      STMT_START	{ \
 	register const char *s_PeRlHaSh = str; \
@@ -52,7 +53,26 @@
 	    hash_PeRlHaSh = hash_PeRlHaSh * 33 + *s_PeRlHaSh++; \
 	(hash) = hash_PeRlHaSh + (hash_PeRlHaSh>>5); \
     } STMT_END
-
+#else
+#define PERL_HASH1 hash_PeRlHaSh = hash_PeRlHaSh * 33 + *s_PeRlHaSh++
+#define PERL_HASH(hash,str,len) \
+    STMT_START	{ \
+      register U32 hash_PeRlHaSh = 0; \
+      if (len > 0) {\
+	register const char *s_PeRlHaSh = str; \
+	register I32 i_PeRlHaSh = ((((U32) len) + 8 - 1) >> 3); \
+	switch (len & (8-1)) { \
+	case 0: do { \
+		PERL_HASH1; case 7: PERL_HASH1; \
+	case 6:	PERL_HASH1; case 5: PERL_HASH1; \
+	case 4:	PERL_HASH1; case 3: PERL_HASH1; \
+	case 2:	PERL_HASH1; case 1: PERL_HASH1; \
+	  } while (--i_PeRlHaSh); \
+	} \
+      } \
+      (hash) = hash_PeRlHaSh + (hash_PeRlHaSh>>5); \
+    } STMT_END
+#endif
 /*
 =for apidoc AmU||HEf_SVKEY
 This flag, used in the length slot of hash entries and magic structures,

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About