Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e561dc2

Browse files
committed
XXXROUNDUP(): Turns out this fixed Andrew MacIntyre's memory-mgmt
disaster too, so this change is here to stay. Beefed up the comments and added some stats Andrew reported. Also a small change to the macro body, to make it obvious how XXXROUNDUP(0) ends up returning 0. See SF patch 578297 for context. Not a bugfix candidate, as the functional changes here have already been backported to the 2.2 line (this patch just improves clarity).
1 parent a65523a commit e561dc2

1 file changed

Lines changed: 33 additions & 10 deletions

File tree

Parser/node.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,44 @@ fancy_roundup(int n)
3434
}
3535

3636
/* A gimmick to make massive numbers of reallocs quicker. The result is
37-
* a number >= the input. For n=0 we must return 0.
38-
* For n=1, we return 1, to avoid wasting memory in common 1-child nodes
39-
* (XXX are those actually common?).
40-
* Else for n <= 128, round up to the closest multiple of 4. Why 4?
41-
* Rounding up to a multiple of an exact power of 2 is very efficient.
42-
* Else call fancy_roundup() to grow proportionately to n. We've got an
37+
* a number >= the input. In PyNode_AddChild, it's used like so, when
38+
* we're about to add child number current_size + 1:
39+
*
40+
* if XXXROUNDUP(current_size) < XXXROUNDUP(current_size + 1):
41+
* allocate space for XXXROUNDUP(current_size + 1) total children
42+
* else:
43+
* we already have enough space
44+
*
45+
* Since a node starts out empty, we must have
46+
*
47+
* XXXROUNDUP(0) < XXXROUNDUP(1)
48+
*
49+
* so that we allocate space for the first child. One-child nodes are very
50+
* common (presumably that would change if we used a more abstract form
51+
* of syntax tree), so to avoid wasting memory it's desirable that
52+
* XXXROUNDUP(1) == 1. That in turn forces XXXROUNDUP(0) == 0.
53+
*
54+
* Else for 2 <= n <= 128, we round up to the closest multiple of 4. Why 4?
55+
* Rounding up to a multiple of an exact power of 2 is very efficient, and
56+
* most nodes with more than one child have <= 4 kids.
57+
*
58+
* Else we call fancy_roundup() to grow proportionately to n. We've got an
4359
* extreme case then (like test_longexp.py), and on many platforms doing
4460
* anything less than proportional growth leads to exorbitant runtime
4561
* (e.g., MacPython), or extreme fragmentation of user address space (e.g.,
4662
* Win98).
47-
* This would be straightforward if a node stored its current capacity. The
48-
* code is tricky to avoid that.
63+
*
64+
* In a run of compileall across the 2.3a0 Lib directory, Andrew MacIntyre
65+
* reported that, with this scheme, 89% of PyMem_RESIZE calls in
66+
* PyNode_AddChild passed 1 for the size, and 9% passed 4. So this usually
67+
* wastes very little memory, but is very effective at sidestepping
68+
* platform-realloc disasters on vulnernable platforms.
69+
*
70+
* Note that this would be straightforward if a node stored its current
71+
* capacity. The code is tricky to avoid that.
4972
*/
50-
#define XXXROUNDUP(n) ((n) == 1 ? 1 : \
51-
(n) <= 128 ? (((n) + 3) & ~3) : \
73+
#define XXXROUNDUP(n) ((n) <= 1 ? (n) : \
74+
(n) <= 128 ? (((n) + 3) & ~3) : \
5275
fancy_roundup(n))
5376

5477

0 commit comments

Comments
 (0)