ada: Tune hash function for cross-reference entries
authorPiotr Trojanek <trojanek@adacore.com>
Tue, 18 Oct 2022 07:33:38 +0000 (09:33 +0200)
committerMarc Poulhiès <poulhies@adacore.com>
Mon, 7 Nov 2022 08:36:36 +0000 (09:36 +0100)
Tune the hash function that combines entity identifiers with source
locations of where those entities are referenced. Previously the source
location was multiplied by 2 ** 7 (i.e. shifted left by 7 bits), then
added to the entity identifier, and finally divided modulo 2 ** 16 (i.e.
masked to only use the lowest 16 bits). This hash routine caused
collisions that could make some tests up to twice slower.

With a large entity number the source location was only contributing few
bits to the hash value. This large entity number might correspond to
entity like Ada.Characters.Latin_1.NUL that occurs thousands of times in
generated code.

gcc/ada/

* lib-xref.adb (Hash): Tune hash function.

gcc/ada/lib-xref.adb

index 043444c..5a1538e 100644 (file)
@@ -1271,10 +1271,10 @@ package body Lib.Xref is
       XE : Xref_Entry renames Xrefs.Table (F);
       type M is mod 2**32;
 
-      H : constant M := M (XE.Key.Ent) + 2 ** 7 * M (abs XE.Key.Loc);
+      H : constant M := 3 * M (XE.Key.Ent) + 5 * M (abs XE.Key.Loc);
       --  It would be more natural to write:
       --
-      --    H : constant M := M'Mod (XE.Key.Ent) + 2**7 * M'Mod (XE.Key.Loc);
+      --    H : constant M := 3 * M'Mod (XE.Key.Ent) + 5 * M'Mod (XE.Key.Loc);
       --
       --  But we can't use M'Mod, because it prevents bootstrapping with older
       --  compilers. Loc can be negative, so we do "abs" before converting.