SF patch #578297: · pythoncapi/cpython@80d4e2a · GitHub
Skip to content

Commit 80d4e2a

Browse files
author
Andrew MacIntyre
committed
SF patch #578297:
Change the parser and compiler to use PyMalloc. Only the files implementing processes that will request memory allocations small enough for PyMalloc to be a win have been changed, which are:- - Python/compile.c - Parser/acceler.c - Parser/node.c - Parser/parsetok.c This augments the aggressive overallocation strategy implemented by Tim Peters in PyNode_AddChild() [Parser/node.c], in reducing the impact of platform malloc()/realloc()/free() corner case behaviour. Such corner cases are known to be triggered by test_longexp and test_import. Jeremy Hylton, in accepting this patch, recommended this as a bugfix candidate for 2.2. While the changes to Python/compile.c and Parser/node.c backport easily (and could go in), the changes to Parser/acceler.c and Parser/parsetok.c require other not insignificant changes as a result of the differences in the memory APIs between 2.3 and 2.2, which I'm not in a position to work through at the moment. This is a pity, as the Parser/parsetok.c changes are the most important after the Parser/node.c changes, due to the size of the memory requests involved and their frequency.
1 parent 4104db3 commit 80d4e2a

4 files changed

Lines changed: 20 additions & 19 deletions

File tree

Parser/acceler.c

Lines changed: 4 additions & 4 deletions

Parser/node.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
node *
88
PyNode_New(int type)
99
{
10-
node *n = PyMem_NEW(node, 1);
10+
node *n = (node *) PyObject_MALLOC(1 * sizeof(node));
1111
if (n == NULL)
1212
return NULL;
1313
n->n_type = type;
@@ -92,7 +92,8 @@ PyNode_AddChild(register node *n1, int type, char *str, int lineno)
9292
return E_OVERFLOW;
9393
if (current_capacity < required_capacity) {
9494
n = n1->n_child;
95-
PyMem_RESIZE(n, node, required_capacity);
95+
n = (node *) PyObject_REALLOC(n,
96+
required_capacity * sizeof(node));
9697
if (n == NULL)
9798
return E_NOMEM;
9899
n1->n_child = n;
@@ -116,7 +117,7 @@ PyNode_Free(node *n)
116117
{
117118
if (n != NULL) {
118119
freechildren(n);
119-
PyMem_DEL(n);
120+
PyObject_FREE(n);
120121
}
121122
}
122123

@@ -127,7 +128,7 @@ freechildren(node *n)
127128
for (i = NCH(n); --i >= 0; )
128129
freechildren(CHILD(n, i));
129130
if (n->n_child != NULL)
130-
PyMem_DEL(n->n_child);
131+
PyObject_FREE(n->n_child);
131132
if (STR(n) != NULL)
132-
PyMem_DEL(STR(n));
133+
PyObject_FREE(STR(n));
133134
}

Parser/parsetok.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
133133
else
134134
started = 1;
135135
len = b - a; /* XXX this may compute NULL - NULL */
136-
str = PyMem_NEW(char, len + 1);
136+
str = (char *) PyObject_MALLOC(len + 1);
137137
if (str == NULL) {
138138
fprintf(stderr, "no mem for next token\n");
139139
err_ret->error = E_NOMEM;
@@ -157,7 +157,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
157157
PyParser_AddToken(ps, (int)type, str, tok->lineno,
158158
&(err_ret->expected))) != E_OK) {
159159
if (err_ret->error != E_DONE)
160-
PyMem_DEL(str);
160+
PyObject_FREE(str);
161161
break;
162162
}
163163
}
@@ -178,7 +178,7 @@ parsetok(struct tok_state *tok, grammar *g, int start, perrdetail *err_ret,
178178
err_ret->offset = tok->cur - tok->buf;
179179
if (tok->buf != NULL) {
180180
size_t len = tok->inp - tok->buf;
181-
err_ret->text = PyMem_NEW(char, len + 1);
181+
err_ret->text = (char *) PyObject_MALLOC(len + 1);
182182
if (err_ret->text != NULL) {
183183
if (len > 0)
184184
strncpy(err_ret->text, tok->buf, len);

Python/compile.c

Lines changed: 7 additions & 7 deletions

0 commit comments

Comments
 (0)