-
-
Notifications
You must be signed in to change notification settings - Fork 32.6k
Description
Bug report
Bug description:
On my computer (Windows 11, AMD CPU), this code is 5 times slower on Python 3.14 than it is on Python 3.12.
(don't mind the generation of evil_all_numbers
, it's just to generate collision hashes based on those Python 3.14 comments which, as far as I can see, has not changed if we look at the Python 3.12 version):
evil_all_numbers = []
i = 40 & (2**16-1)
perturb = i
while len(evil_all_numbers) < 40000:
evil_all_numbers.append(i+2**17)
perturb >>= 5
i = (5*i+ 1 + perturb) & (2**16-1)
my_dict = {}
for number in evil_all_numbers:
my_dict[number] = 1
import timeit
print(timeit.timeit("40 in my_dict", globals=globals(), number=50000))
print(timeit.timeit("39 in my_dict", globals=globals(), number=50000))
I (surprisingly) get:
PS C:\Users\lhott\Downloads> & "C:\Users\lhott\AppData\Local\Programs\Python\Python312\python.exe" .\testHackHash.py
1.8733288999646902
0.001043599913828075
PS C:\Users\lhott\Downloads> & "C:\Users\lhott\AppData\Local\Programs\Python\Python314\python.exe" .\testHackHash.py
5.002686099964194
0.0010038999607786536
I'm using versions Python 3.14.0rc1 (tags/v3.14.0rc1:48f8831, Jul 22 2025, 17:09:57) [MSC v.1944 64 bit (AMD64)] on win32
and Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
for the comparisons. Both were downloaded from the official python website.
It might just be a change in how dictionaries are handled that made the worst case worse, but I never heard of it (although admittedly I haven't scoured the changelogs).
If I'm not mistaken, the load factor is indicated to be the same in both versions as well, so that's not an explanation:
Line 168 in c9d9f78
out, we usually do -- the table load factor is kept under 2/3, so the odds |
Line 316 in 1bde13b
out, we usually do -- the table load factor is kept under 2/3, so the odds |
CPython versions tested on:
3.12, 3.13 (see this and this), 3.14
Operating systems tested on:
Windows
Sets
For the same slowdown on sets, see this.