Skip to content

Unexplained performance slowdown on both official 3.13.6 and 3.14 regarding hash collisions - reproduced on dict[int, int] and set[int], cannot be reproduced when building Python locally #137636

@JasonMendoza2008

Description

@JasonMendoza2008

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:

out, we usually do -- the table load factor is kept under 2/3, so the odds

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)performancePerformance or resource usagetype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions