bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Collin Funk <collin.funk1@gmail.com>
To: bug-gnulib@gnu.org
Subject: gnulib-tool.py: Make GLModule's __eq__ and __hash__ method agree.
Date: Tue, 16 Apr 2024 12:50:04 -0700	[thread overview]
Message-ID: <7747ebe2-4275-4ccb-8cde-2e53d8cd6ff5@gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 2206 bytes --]

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

[-- Attachment #2: 0001-gnulib-tool.py-Make-GLModule-s-__eq__-and-__hash__-m.patch --]
[-- Type: text/x-patch, Size: 1402 bytes --]

From f86f0505eade741af47a7cb5e603240ba5f03b9d Mon Sep 17 00:00:00 2001
From: Collin Funk <collin.funk1@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.funk1@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.funk1@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	other threads:[~2024-04-16 19:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-16 19:50 Collin Funk [this message]
2024-04-16 23:00 ` gnulib-tool.py: Make GLModule's __eq__ and __hash__ method agree Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7747ebe2-4275-4ccb-8cde-2e53d8cd6ff5@gmail.com \
    --to=collin.funk1@gmail.com \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).