Mercurial > cpython-lru_cache-weakref
annotate Modules/rotatingtree.c @ 87690:66c1c9f32567 default tip
Add use_weakref option to functools.lru_cache
The use_weakref option makes lru_cache create weak references to the function
arguments when creating the cache key, which prevents them from staying in the
memory forever even after all other references to them are gone.
This is a proof of concept for http://bugs.python.org/issue19859
| author | Radomir Dopieralski <devel@sheep.art.pl> |
|---|---|
| date | Tue, 03 Dec 2013 16:02:48 +0100 |
| parents | fd25030223b0 |
| children |
| rev | line source |
|---|---|
| 36459 | 1 #include "rotatingtree.h" |
| 2 | |
| 3 #define KEY_LOWER_THAN(key1, key2) ((char*)(key1) < (char*)(key2)) | |
| 4 | |
| 5 /* The randombits() function below is a fast-and-dirty generator that | |
| 6 * is probably irregular enough for our purposes. Note that it's biased: | |
| 7 * I think that ones are slightly more probable than zeroes. It's not | |
| 8 * important here, though. | |
| 9 */ | |
| 10 | |
| 11 static unsigned int random_value = 1; | |
| 12 static unsigned int random_stream = 0; | |
| 13 | |
| 14 static int | |
| 15 randombits(int bits) | |
| 16 { | |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
17 int result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
18 if (random_stream < (1U << bits)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
19 random_value *= 1082527; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
20 random_stream = random_value; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
21 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
22 result = random_stream & ((1<<bits)-1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
23 random_stream >>= bits; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
24 return result; |
| 36459 | 25 } |
| 26 | |
| 27 | |
| 28 /* Insert a new node into the tree. | |
| 29 (*root) is modified to point to the new root. */ | |
| 30 void | |
| 31 RotatingTree_Add(rotating_node_t **root, rotating_node_t *node) | |
| 32 { | |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
33 while (*root != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
34 if (KEY_LOWER_THAN(node->key, (*root)->key)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
35 root = &((*root)->left); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
36 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
37 root = &((*root)->right); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
38 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
39 node->left = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
40 node->right = NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
41 *root = node; |
| 36459 | 42 } |
| 43 | |
| 44 /* Locate the node with the given key. This is the most complicated | |
| 45 function because it occasionally rebalances the tree to move the | |
| 46 resulting node closer to the root. */ | |
| 47 rotating_node_t * | |
| 48 RotatingTree_Get(rotating_node_t **root, void *key) | |
| 49 { | |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
50 if (randombits(3) != 4) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
51 /* Fast path, no rebalancing */ |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
52 rotating_node_t *node = *root; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
53 while (node != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
54 if (node->key == key) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
55 return node; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
56 if (KEY_LOWER_THAN(key, node->key)) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
57 node = node->left; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
58 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
59 node = node->right; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
60 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
61 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
62 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
63 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
64 rotating_node_t **pnode = root; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
65 rotating_node_t *node = *pnode; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
66 rotating_node_t *next; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
67 int rotate; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
68 if (node == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
69 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
70 while (1) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
71 if (node->key == key) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
72 return node; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
73 rotate = !randombits(1); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
74 if (KEY_LOWER_THAN(key, node->key)) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
75 next = node->left; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
76 if (next == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
77 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
78 if (rotate) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
79 node->left = next->right; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
80 next->right = node; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
81 *pnode = next; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
82 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
83 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
84 pnode = &(node->left); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
85 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
86 else { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
87 next = node->right; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
88 if (next == NULL) |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
89 return NULL; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
90 if (rotate) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
91 node->right = next->left; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
92 next->left = node; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
93 *pnode = next; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
94 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
95 else |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
96 pnode = &(node->right); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
97 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
98 node = next; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
99 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
100 } |
| 36459 | 101 } |
| 102 | |
| 103 /* Enumerate all nodes in the tree. The callback enumfn() should return | |
| 104 zero to continue the enumeration, or non-zero to interrupt it. | |
| 105 A non-zero value is directly returned by RotatingTree_Enum(). */ | |
| 106 int | |
| 107 RotatingTree_Enum(rotating_node_t *root, rotating_tree_enum_fn enumfn, | |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
108 void *arg) |
| 36459 | 109 { |
|
61463
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
110 int result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
111 rotating_node_t *node; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
112 while (root != NULL) { |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
113 result = RotatingTree_Enum(root->left, enumfn, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
114 if (result != 0) return result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
115 node = root->right; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
116 result = enumfn(root, arg); |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
117 if (result != 0) return result; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
118 root = node; |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
119 } |
|
fd25030223b0
Untabify C files. Will watch buildbots.
Antoine Pitrou <solipsis@pitrou.net>
parents:
36496
diff
changeset
|
120 return 0; |
| 36459 | 121 } |
