Skip to content

Commit c771cbe

Browse files
gh-93065: Fix HAMT to iterate correctly over 7-level deep trees (GH-93066) (GH-93145)
Also while there, clarify a few things about why we reduce the hash to 32 bits. Co-authored-by: Eli Libman <[email protected]> Co-authored-by: Yury Selivanov <[email protected]> Co-authored-by: Łukasz Langa <[email protected]> (cherry picked from commit c1f5c90)
1 parent 97fe65a commit c771cbe

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

Include/internal/pycore_hamt.h

Lines changed: 13 additions & 1 deletion
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
extern PyTypeObject _PyHamt_Type;

Lib/test/test_context.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,41 @@ def test_hamt_collision_1(self):
535535
self.assertEqual(len(h4), 2)
536536
self.assertEqual(len(h5), 3)
537537

538+
def test_hamt_collision_3(self):
539+
# Test that iteration works with the deepest tree possible.
540+
# https://github.com/python/cpython/issues/93065
541+
542+
C = HashKey(0b10000000_00000000_00000000_00000000, 'C')
543+
D = HashKey(0b10000000_00000000_00000000_00000000, 'D')
544+
545+
E = HashKey(0b00000000_00000000_00000000_00000000, 'E')
546+
547+
h = hamt()
548+
h = h.set(C, 'C')
549+
h = h.set(D, 'D')
550+
h = h.set(E, 'E')
551+
552+
# BitmapNode(size=2 count=1 bitmap=0b1):
553+
# NULL:
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=4 count=2 bitmap=0b101):
565+
# <Key name:E hash:0>: 'E'
566+
# NULL:
567+
# CollisionNode(size=4 id=0x107a24520):
568+
# <Key name:C hash:2147483648>: 'C'
569+
# <Key name:D hash:2147483648>: 'D'
570+
571+
self.assertEqual({k.name for k in h.keys()}, {'C', 'D', 'E'})
572+
538573
def test_hamt_stress(self):
539574
COLLECTION_SIZE = 7000
540575
TEST_ITERS_EVERY = 647

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,7 @@ Robert Li
10621062
Xuanji Li
10631063
Zekun Li
10641064
Zheao Li
1065+
Eli Libman
10651066
Dan Lidral-Porter
10661067
Robert van Liere
10671068
Ross Light
Lines changed: 5 additions & 0 deletions
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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,14 +409,22 @@ hamt_hash(PyObject *o)
409409
return -1;
410410
}
411411

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

0 commit comments

Comments
 (0)