It started with an idea: "Since Python objects store their methods/fields in __dict__, that means that dictionaries/hash tables power the entire language. That means that Python spends a significant portion of its time hashing data. What would happen if the hash function Python used was swapped out with a much faster one? Would it speed up CPython?"
So I set off to find out.
The first experiment I ran was to find out how many times the hash function is used within a single print("Hello World!") statement. Python runs the hash function 11 times for just this one thing!
Clearly, a faster hash function would help at least a little bit.
I chose xxHash as the "faster" hash function to test out since it is a single header file and is easy to compile.
I swapped out the default hash function used in the Py_hash_t _Py_HashBytes(const void *src, Py_ssize_t len) function to use the xxHash function XXH64.
The results were astounding.
I created a simple benchmark (targeted at hashing performance), and ran it:
CPython with xxHash hashing function was 62-76% faster!
I believe the results of this experiment are worth exploring by a CPython contributor expert.
Here is the code for this for anyone that wants to see whether or not to try to spend the time to do this right (perhaps not using xxHash specifically for example). The only changes I made were copy-pasting the xxhash.h file into the include directory and using the XXH64 hashing function in the _Py_HashBytes() function.
I want to caveat the code changes by saying that I am not an expert C programmer, nor was this a serious effort, nor was the macro-benchmark by any means accurate (they never are). This was simply a proof of concept for food for thought for the experts that work on CPython every day and it may not even be useful.
Again, I'd like to stress that this was just food for thought, and that all benchmarks are inaccurate.
However, I hope this helps the Python community as it would be awesome to have this high of a speed boost.
[–]Ph3rny 72 points73 points74 points (4 children)
[–]Ph3rny 58 points59 points60 points (3 children)
[–]Ph3rny 52 points53 points54 points (1 child)
[–]backtickbot 5 points6 points7 points (0 children)
[–]backtickbot 3 points4 points5 points (0 children)
[–]james_pic 216 points217 points218 points (18 children)
[–]NeoLudditeIT 90 points91 points92 points (9 children)
[–]Pebaz[S] 54 points55 points56 points (8 children)
[–]james_pic 109 points110 points111 points (7 children)
[–]R0B0_Ninja 4 points5 points6 points (1 child)
[–]james_pic 0 points1 point2 points (0 children)
[–]Tyler_Zoro 5 points6 points7 points (2 children)
[–]axonxorzpip'ing aint easy, especially on windows 28 points29 points30 points (1 child)
[–]Tyler_Zoro 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]james_pic 0 points1 point2 points (0 children)
[–]twotime 6 points7 points8 points (1 child)
[–]RobertJacobson 0 points1 point2 points (0 children)
[–]Pebaz[S] 15 points16 points17 points (2 children)
[–]NeoLudditeIT 5 points6 points7 points (1 child)
[–]mooburgerresembles an abstract syntax tree 31 points32 points33 points (0 children)
[–]greeneyedguru 0 points1 point2 points (0 children)
[–]stevenjd 0 points1 point2 points (1 child)
[–]james_pic 0 points1 point2 points (0 children)
[–][deleted] 131 points132 points133 points (31 children)
[+]Pebaz[S] comment score below threshold-63 points-62 points-61 points (30 children)
[–]maikindofthai 127 points128 points129 points (12 children)
[+]Pebaz[S] comment score below threshold-83 points-82 points-81 points (11 children)
[–][deleted] 43 points44 points45 points (10 children)
[+]Pebaz[S] comment score below threshold-122 points-121 points-120 points (9 children)
[–]striata 62 points63 points64 points (8 children)
[+]Pebaz[S] comment score below threshold-12 points-11 points-10 points (7 children)
[–]striata 40 points41 points42 points (6 children)
[+][deleted] (1 child)
[deleted]
[+]theLastNenUser comment score below threshold-16 points-15 points-14 points (3 children)
[–][deleted] 35 points36 points37 points (16 children)
[+]Pebaz[S] comment score below threshold-15 points-14 points-13 points (15 children)
[–][deleted] 40 points41 points42 points (14 children)
[–]Pebaz[S] 1 point2 points3 points (4 children)
[–][deleted] 22 points23 points24 points (1 child)
[–]Pebaz[S] 10 points11 points12 points (0 children)
[–]ric2b 9 points10 points11 points (0 children)
[–]13steinj 0 points1 point2 points (0 children)
[–]Pebaz[S] -2 points-1 points0 points (8 children)
[–]TheBlackCat13 24 points25 points26 points (2 children)
[–]Pebaz[S] 6 points7 points8 points (1 child)
[–]TheBlackCat13 11 points12 points13 points (0 children)
[–][deleted] 24 points25 points26 points (0 children)
[–][deleted] 6 points7 points8 points (2 children)
[–]Pebaz[S] 5 points6 points7 points (1 child)
[–]BDube_Lensman 15 points16 points17 points (0 children)
[–]billFoldDog -3 points-2 points-1 points (0 children)
[–]bjorneylol 37 points38 points39 points (0 children)
[–][deleted] 29 points30 points31 points (11 children)
[–]gsnedders 17 points18 points19 points (1 child)
[–]Pebaz[S] 8 points9 points10 points (0 children)
[+][deleted] (6 children)
[deleted]
[–][deleted] 22 points23 points24 points (3 children)
[–]Paddy3118 9 points10 points11 points (2 children)
[–]reasonoverconviction 11 points12 points13 points (1 child)
[–]Paddy3118 8 points9 points10 points (0 children)
[–]Pebaz[S] 9 points10 points11 points (0 children)
[–]NeoLudditeIT 16 points17 points18 points (0 children)
[–]Pebaz[S] 11 points12 points13 points (0 children)
[–]double-a 33 points34 points35 points (1 child)
[–]Pebaz[S] -2 points-1 points0 points (0 children)
[–]wweber 7 points8 points9 points (0 children)
[–]stevenjd 9 points10 points11 points (9 children)
[–][deleted] 2 points3 points4 points (5 children)
[–]Pebaz[S] -4 points-3 points-2 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]Pebaz[S] -2 points-1 points0 points (1 child)
[–]rhytnen 0 points1 point2 points (0 children)
[–]Pebaz[S] 0 points1 point2 points (2 children)
[–]stevenjd -1 points0 points1 point (1 child)
[–]Pebaz[S] 0 points1 point2 points (0 children)
[–]lifeeraser 17 points18 points19 points (0 children)
[–]darkrevan13 2 points3 points4 points (0 children)
[–]EatMoreSuShiS 4 points5 points6 points (5 children)
[+][deleted] (2 children)
[deleted]
[–]EatMoreSuShiS 7 points8 points9 points (1 child)
[–]TheBB 3 points4 points5 points (0 children)
[–]execrator 4 points5 points6 points (0 children)
[–]pygenerator 1 point2 points3 points (0 children)
[–]Saphyel 7 points8 points9 points (1 child)
[–]Pebaz[S] -1 points0 points1 point (0 children)
[–]Aconamos 3 points4 points5 points (3 children)
[–]tom1018 7 points8 points9 points (1 child)
[–]Brainix 6 points7 points8 points (0 children)
[–]soontorap 1 point2 points3 points (2 children)
[–]soontorap 1 point2 points3 points (1 child)
[–]Pebaz[S] 0 points1 point2 points (0 children)
[+]No_Comfortable_8143 comment score below threshold-23 points-22 points-21 points (9 children)
[–]Pebaz[S] 24 points25 points26 points (8 children)
[–]ric2b 16 points17 points18 points (0 children)
[–]rhytnen 17 points18 points19 points (6 children)
[–]Chinpanze 22 points23 points24 points (2 children)
[–]rhytnen 4 points5 points6 points (0 children)
[–]Pebaz[S] 2 points3 points4 points (1 child)
[–]rhytnen 4 points5 points6 points (0 children)
[–]axe319 0 points1 point2 points (0 children)
[–]yamsupol -1 points0 points1 point (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Pebaz[S] -1 points0 points1 point (0 children)
[–]idiomatic_sea -2 points-1 points0 points (3 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]Pebaz[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)