Browse Source

Removing duplicated hash functions in compiler/cclasses.pas

FPHash was duplicated 4 times for different input types. I've
consolidated it to a single function with 3 proxy-functions which can be
inlined.

I've also modified the hash function itself slightly, it now resembled
the original DJB hash and seems to perform a tiny bit better.

git-svn-id: trunk@23850 -
masta 12 years ago
parent
commit
d60049a1fd
1 changed files with 29 additions and 50 deletions
  1. 29 50
      compiler/cclasses.pas

+ 29 - 50
compiler/cclasses.pas

@@ -579,10 +579,10 @@ type
       end;
 
 
-    function FPHash(const s:shortstring):LongWord;
-    function FPHash(P: PChar; Len: Integer): LongWord;
     function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord;
-    function FPHash(const a:ansistring):LongWord;
+    function FPHash(P: PChar; Len: Integer): LongWord; inline;
+    function FPHash(const s:shortstring):LongWord; inline;
+    function FPHash(const a:ansistring):LongWord; inline;
 
 
 implementation
@@ -1137,59 +1137,38 @@ end;
                             TFPHashList
 *****************************************************************************}
 
-    function FPHash(const s:shortstring):LongWord;
-      Var
-        p,pmax : pchar;
-      begin
-{$push}
-{$q-,r-}
-        result:=0;
-        p:=@s[1];
-        pmax:=@s[length(s)+1];
-        while (p<pmax) do
-          begin
-            result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
-            inc(p);
-          end;
-{$pop}
-      end;
-
-    function FPHash(P: PChar; Len: Integer): LongWord;
-      Var
-        pmax : pchar;
-      begin
-{$push}
-{$q-,r-}
-        result:=0;
-        pmax:=p+len;
-        while (p<pmax) do
-          begin
-            result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
-            inc(p);
-          end;
-{$pop}
-      end;
 
     function FPHash(P: PChar; Len: Integer; Tag: LongWord): LongWord;
-      Var
-        pmax : pchar;
-      begin
+    Var
+      pmax : pchar;
+    begin
 {$push}
 {$q-,r-}
-        result:=Tag;
-        pmax:=p+len;
-        while (p<pmax) do
-          begin
-            result:=LongWord(LongInt(result shl 5) - LongInt(result)) xor LongWord(P^);
-            inc(p);
-          end;
+      result:=Tag;
+      pmax:=p+len;
+      while (p<pmax) do
+        begin
+          {DJBHash: result:=result*33 + next_char}
+          result:=LongWord(LongInt(result shl 5) + LongInt(result)) + LongWord(P^);
+          inc(p);
+        end;
 {$pop}
-      end;
+    end;
 
-    function FPHash(const a: ansistring): LongWord;
-      begin
-         result:=fphash(pchar(a),length(a));
-      end;
+    function FPHash(P: PChar; Len: Integer): LongWord; inline;
+    begin
+      result:=fphash(P,Len, 5381);
+    end;
+
+    function FPHash(const s: shortstring): LongWord; inline;
+    begin
+      result:=fphash(pchar(@s[1]),length(s));
+    end;
+
+    function FPHash(const a: ansistring): LongWord; inline;
+    begin
+      result:=fphash(pchar(a),length(a));
+    end;
 
 
 procedure TFPHashList.RaiseIndexError(Index : Integer);