Skip to content

Commit 6d4927a

Browse files
authored
[3.8] gh-93065: Fix HAMT to iterate correctly over 7-level deep trees (GH-93066) (#93148)
Also while there, clarify a few things about why we reduce the hash to 32 bits. Co-authored-by: Eli Libman <eli@hyro.ai> Co-authored-by: Yury Selivanov <yury@edgedb.com> Co-authored-by: Łukasz Langa <lukasz@langa.pl> (cherry picked from commit c1f5c90)
1 parent 69cf020 commit 6d4927a

File tree

5 files changed

+66
-4
lines changed

5 files changed

+66
-4
lines changed

Include/internal/pycore_hamt.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,19 @@
55
# error "this header requires Py_BUILD_CORE define"
66
#endif
77

8-
#define _Py_HAMT_MAX_TREE_DEPTH 7
8+
9+
/*
10+
HAMT tree is shaped by hashes of keys. Every group of 5 bits of a hash denotes
11+
the exact position of the key in one level of the tree. Since we're using
12+
32 bit hashes, we can have at most 7 such levels. Although if there are
13+
two distinct keys with equal hashes, they will have to occupy the same
14+
cell in the 7th level of the tree -- so we'd put them in a "collision" node.
15+
Which brings the total possible tree depth to 8. Read more about the actual
16+
layout of the HAMT tree in `hamt.c`.
17+
18+
This constant is used to define a datastucture for storing iteration state.
19+
*/
20+
#define _Py_HAMT_MAX_TREE_DEPTH 8
921

1022

1123
#define PyHamt_Check(o) (Py_TYPE(o) == &_PyHamt_Type)

Lib/test/test_context.py

+35
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,41 @@ def test_hamt_collision_1(self):
537537
self.assertEqual(len(h4), 2)
538538
self.assertEqual(len(h5), 3)
539539

540+
def test_hamt_collision_3(self):
541+
# Test that iteration works with the deepest tree possible.
542+
# https://github.com/python/cpython/issues/93065
543+
544+
C = HashKey(0b10000000_00000000_00000000_00000000, 'C')
545+
D = HashKey(0b10000000_00000000_00000000_00000000, 'D')
546+
547+
E = HashKey(0b00000000_00000000_00000000_00000000, 'E')
548+
549+
h = hamt()
550+
h = h.set(C, 'C')
551+
h = h.set(D, 'D')
552+
h = h.set(E, 'E')
553+
554+
# BitmapNode(size=2 count=1 bitmap=0b1):
555+
# NULL:
556+
# BitmapNode(size=2 count=1 bitmap=0b1):
557+
# NULL:
558+
# BitmapNode(size=2 count=1 bitmap=0b1):
559+
# NULL:
560+
# BitmapNode(size=2 count=1 bitmap=0b1):
561+
# NULL:
562+
# BitmapNode(size=2 count=1 bitmap=0b1):
563+
# NULL:
564+
# BitmapNode(size=2 count=1 bitmap=0b1):
565+
# NULL:
566+
# BitmapNode(size=4 count=2 bitmap=0b101):
567+
# <Key name:E hash:0>: 'E'
568+
# NULL:
569+
# CollisionNode(size=4 id=0x107a24520):
570+
# <Key name:C hash:2147483648>: 'C'
571+
# <Key name:D hash:2147483648>: 'D'
572+
573+
self.assertEqual({k.name for k in h.keys()}, {'C', 'D', 'E'})
574+
540575
def test_hamt_stress(self):
541576
COLLECTION_SIZE = 7000
542577
TEST_ITERS_EVERY = 647

Misc/ACKS

+2
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,8 @@ Robert Li
10021002
Xuanji Li
10031003
Zekun Li
10041004
Zheao Li
1005+
Eli Libman
1006+
Dan Lidral-Porter
10051007
Robert van Liere
10061008
Ross Light
10071009
Shawn Ligocki
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Fix contextvars HAMT implementation to handle iteration over deep trees.
2+
3+
The bug was discovered and fixed by Eli Libman. See
4+
`MagicStack/immutables#84 <https://github.com/MagicStack/immutables/issues/84>`_
5+
for more details.

Python/hamt.c

+11-3
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,22 @@ hamt_hash(PyObject *o)
408408
return -1;
409409
}
410410

411-
/* While it's suboptimal to reduce Python's 64 bit hash to
411+
/* While it's somewhat suboptimal to reduce Python's 64 bit hash to
412412
32 bits via XOR, it seems that the resulting hash function
413413
is good enough (this is also how Long type is hashed in Java.)
414414
Storing 10, 100, 1000 Python strings results in a relatively
415415
shallow and uniform tree structure.
416416
417-
Please don't change this hashing algorithm, as there are many
418-
tests that test some exact tree shape to cover all code paths.
417+
Also it's worth noting that it would be possible to adapt the tree
418+
structure to 64 bit hashes, but that would increase memory pressure
419+
and provide little to no performance benefits for collections with
420+
fewer than billions of key/value pairs.
421+
422+
Important: do not change this hash reducing function. There are many
423+
tests that need an exact tree shape to cover all code paths and
424+
we do that by specifying concrete values for test data's `__hash__`.
425+
If this function is changed most of the regression tests would
426+
become useless.
419427
*/
420428
int32_t xored = (int32_t)(hash & 0xffffffffl) ^ (int32_t)(hash >> 32);
421429
return xored == -1 ? -2 : xored;

0 commit comments

Comments
 (0)