>From the documentation of object.__hash__(self) [1]:

     The only required property is that objects which compare equal
     have the same hash value; it is advised to mix together the hash
     values of the components of the object that also play a part in
     comparison of objects by packing them into a tuple and hashing
     the tuple.

Right now we use the following to compute the hash in GLModule:

    result = hash(self.name) ^ hash(self.patched)

The way that the documentation recommends writing this:

    result = hash((self.name, self.patched))

But I believe the 'patched' boolean should not be involved in
computing the hash. This would make __hash__ align with the definition
of __eq__. This also seems to better match the way dependencies are
tracked using the following string as a key:

    module1---module2

Since __hash__ is also used for sets using the 'patched' would allow a
set with an unpatched variant and patched variant in it, which I
assume would cause issues. In practice, I think you would have to
create & remove files to make the lookups in GLFileSystem return
these.

Since __hash__ is used a lot from sets I thought it would be
interesting to look at the profiler output as well. These are all from
test-create-testdir-4.sh.

Using hash(self.name) ^ hash(self.patched) as we have now:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   504489    0.183    0.000    0.294    0.000 GLModuleSystem.py:256(__hash__)
  1008978    0.112    0.000    0.112    0.000 {built-in method builtins.hash}

Using hash((self.name, self.patched)) as recommended:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   504489    0.119    0.000    0.184    0.000 GLModuleSystem.py:256(__hash__)
   504489    0.066    0.000    0.066    0.000 {built-in method builtins.hash}

Using hash(self.name) which as in the patch I attached:

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
   504489    0.102    0.000    0.166    0.000 GLModuleSystem.py:256(__hash__)
   504489    0.064    0.000    0.064    0.000 {built-in method builtins.hash}

[1] https://docs.python.org/3/reference/datamodel.html#object.__hash__

Collin
From f86f0505eade741af47a7cb5e603240ba5f03b9d Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.fu...@gmail.com>
Date: Tue, 16 Apr 2024 12:13:08 -0700
Subject: [PATCH] gnulib-tool.py: Make GLModule's __eq__ and __hash__ method
 agree.

* pygnulib/GLModuleSystem.py (GLModuleTable.__hash__): Only use the
module name in hash computations.
---
 ChangeLog                  | 6 ++++++
 pygnulib/GLModuleSystem.py | 2 +-
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/ChangeLog b/ChangeLog
index 03c6919131..613a6c85dd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-04-16  Collin Funk  <collin.fu...@gmail.com>
+
+	gnulib-tool.py: Make GLModule's __eq__ and __hash__ method agree.
+	* pygnulib/GLModuleSystem.py (GLModuleTable.__hash__): Only use the
+	module name in hash computations.
+
 2024-04-16  Collin Funk  <collin.fu...@gmail.com>
 
 	gnulib-tool.py: Prefer 'not in' over 'not ... in'.
diff --git a/pygnulib/GLModuleSystem.py b/pygnulib/GLModuleSystem.py
index 3148f921e1..6aed38b9eb 100644
--- a/pygnulib/GLModuleSystem.py
+++ b/pygnulib/GLModuleSystem.py
@@ -255,7 +255,7 @@ def __gt__(self, module: object) -> bool:
 
     def __hash__(self) -> int:
         '''x.__hash__() <==> hash(x)'''
-        result = hash(self.name) ^ hash(self.patched)
+        result = hash(self.name)
         return result
 
     def __le__(self, module: object) -> bool:
-- 
2.44.0

Reply via email to