[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
gnulib-tool.py: Make GLModule's __eq__ and __hash__ method agree.
From: |
Collin Funk |
Subject: |
gnulib-tool.py: Make GLModule's __eq__ and __hash__ method agree. |
Date: |
Tue, 16 Apr 2024 12:50:04 -0700 |
User-agent: |
Mozilla Thunderbird |
>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
0001-gnulib-tool.py-Make-GLModule-s-__eq__-and-__hash__-m.patch
Description: Text Data
- gnulib-tool.py: Make GLModule's __eq__ and __hash__ method agree.,
Collin Funk <=