Skip to content

Commit 3b04c79

Browse files
optimize for list
1 parent 4fd006e commit 3b04c79

File tree

6 files changed

+28
-18
lines changed

6 files changed

+28
-18
lines changed

Include/internal/pycore_magic_number.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ PC/launcher.c must also be updated.
300300
301301
*/
302302

303-
#define PYC_MAGIC_NUMBER 3656
303+
#define PYC_MAGIC_NUMBER 3657
304304
/* This is equivalent to converting PYC_MAGIC_NUMBER to 2 bytes
305305
(little-endian) and then appending b'\r\n'. */
306306
#define PYC_MAGIC_NUMBER_TOKEN \

Include/internal/pycore_opcode_utils.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ extern "C" {
7373
#define CONSTANT_BUILTIN_TUPLE 2
7474
#define CONSTANT_BUILTIN_ALL 3
7575
#define CONSTANT_BUILTIN_ANY 4
76-
#define NUM_COMMON_CONSTANTS 5
76+
#define CONSTANT_BUILTIN_LIST 5
77+
#define NUM_COMMON_CONSTANTS 6
7778

7879
/* Values used in the oparg for RESUME */
7980
#define RESUME_AT_FUNC_START 0

Lib/opcode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
_intrinsic_2_descs = _opcode.get_intrinsic2_descs()
4141
_special_method_names = _opcode.get_special_method_names()
4242
_common_constants = [builtins.AssertionError, builtins.NotImplementedError,
43-
builtins.tuple, builtins.all, builtins.any]
43+
builtins.tuple, builtins.all, builtins.any, builtins.list]
4444
_nb_ops = _opcode.get_nb_ops()
4545

4646
hascompare = [opmap["COMPARE_OP"]]

Lib/test/test_builtin.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def test_any(self):
246246
S = [10, 20, 30]
247247
self.assertEqual(any(x > 42 for x in S), False)
248248

249-
def test_all_any_tuple_optimization(self):
249+
def test_all_any_tuple_list_optimization(self):
250250
def f_all():
251251
return all(x-2 for x in [1,2,3])
252252

@@ -256,7 +256,10 @@ def f_any():
256256
def f_tuple():
257257
return tuple(2*x for x in [1,2,3])
258258

259-
funcs = [f_all, f_any, f_tuple]
259+
def f_list():
260+
return list(2*x for x in [1,2,3])
261+
262+
funcs = [f_all, f_any, f_tuple, f_list]
260263

261264
for f in funcs:
262265
# check that generator code object is not duplicated
@@ -266,33 +269,33 @@ def f_tuple():
266269

267270
# check the overriding the builtins works
268271

269-
global all, any, tuple
270-
saved = all, any, tuple
272+
global all, any, tuple, list
273+
saved = all, any, tuple, list
271274
try:
272275
all = lambda x : "all"
273276
any = lambda x : "any"
274277
tuple = lambda x : "tuple"
278+
list = lambda x : "list"
275279

276280
overridden_outputs = [f() for f in funcs]
277281
finally:
278-
all, any, tuple = saved
279-
280-
self.assertEqual(overridden_outputs, ['all', 'any', 'tuple'])
282+
all, any, tuple, list = saved
281283

284+
self.assertEqual(overridden_outputs, ['all', 'any', 'tuple', 'list'])
282285
# Now repeat, overriding the builtins module as well
283-
saved = all, any, tuple
286+
saved = all, any, tuple, list
284287
try:
285288
builtins.all = all = lambda x : "all"
286289
builtins.any = any = lambda x : "any"
287290
builtins.tuple = tuple = lambda x : "tuple"
291+
builtins.list = list = lambda x : "list"
288292

289293
overridden_outputs = [f() for f in funcs]
290294
finally:
291-
all, any, tuple = saved
292-
builtins.all, builtins.any, builtins.tuple = saved
293-
294-
self.assertEqual(overridden_outputs, ['all', 'any', 'tuple'])
295+
all, any, tuple, list = saved
296+
builtins.all, builtins.any, builtins.tuple, builtins.list = saved
295297

298+
self.assertEqual(overridden_outputs, ['all', 'any', 'tuple', 'list'])
296299

297300
def test_ascii(self):
298301
self.assertEqual(ascii(''), '\'\'')

Python/codegen.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3892,14 +3892,17 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end)
38923892
else if (_PyUnicode_EqualToASCIIString(func->v.Name.id, "tuple")) {
38933893
const_oparg = CONSTANT_BUILTIN_TUPLE;
38943894
}
3895+
else if (_PyUnicode_EqualToASCIIString(func->v.Name.id, "list")) {
3896+
const_oparg = CONSTANT_BUILTIN_LIST;
3897+
}
38953898
if (const_oparg != -1) {
38963899
ADDOP_I(c, loc, COPY, 1); // the function
38973900
ADDOP_I(c, loc, LOAD_COMMON_CONSTANT, const_oparg);
38983901
ADDOP_COMPARE(c, loc, Is);
38993902
ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, skip_optimization);
39003903
ADDOP(c, loc, POP_TOP);
39013904

3902-
if (const_oparg == CONSTANT_BUILTIN_TUPLE) {
3905+
if (const_oparg == CONSTANT_BUILTIN_TUPLE || const_oparg == CONSTANT_BUILTIN_LIST) {
39033906
ADDOP_I(c, loc, BUILD_LIST, 0);
39043907
}
39053908
expr_ty generator_exp = asdl_seq_GET(args, 0);
@@ -3911,7 +3914,7 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end)
39113914
ADDOP(c, loc, PUSH_NULL); // Push NULL index for loop
39123915
USE_LABEL(c, loop);
39133916
ADDOP_JUMP(c, loc, FOR_ITER, cleanup);
3914-
if (const_oparg == CONSTANT_BUILTIN_TUPLE) {
3917+
if (const_oparg == CONSTANT_BUILTIN_TUPLE || const_oparg == CONSTANT_BUILTIN_LIST) {
39153918
ADDOP_I(c, loc, LIST_APPEND, 3);
39163919
ADDOP_JUMP(c, loc, JUMP, loop);
39173920
}
@@ -3921,7 +3924,7 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end)
39213924
}
39223925

39233926
ADDOP(c, NO_LOCATION, POP_ITER);
3924-
if (const_oparg != CONSTANT_BUILTIN_TUPLE) {
3927+
if (const_oparg != CONSTANT_BUILTIN_TUPLE && const_oparg != CONSTANT_BUILTIN_LIST) {
39253928
ADDOP_LOAD_CONST(c, loc, initial_res == Py_True ? Py_False : Py_True);
39263929
}
39273930
ADDOP_JUMP(c, loc, JUMP, end);
@@ -3931,6 +3934,8 @@ maybe_optimize_function_call(compiler *c, expr_ty e, jump_target_label end)
39313934
ADDOP(c, NO_LOCATION, POP_ITER);
39323935
if (const_oparg == CONSTANT_BUILTIN_TUPLE) {
39333936
ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_LIST_TO_TUPLE);
3937+
} else if (const_oparg == CONSTANT_BUILTIN_LIST) {
3938+
// result is already a list
39343939
}
39353940
else {
39363941
ADDOP_LOAD_CONST(c, loc, initial_res);

Python/pylifecycle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,6 +829,7 @@ pycore_init_builtins(PyThreadState *tstate)
829829
interp->common_consts[CONSTANT_BUILTIN_TUPLE] = (PyObject*)&PyTuple_Type;
830830
interp->common_consts[CONSTANT_BUILTIN_ALL] = all;
831831
interp->common_consts[CONSTANT_BUILTIN_ANY] = any;
832+
interp->common_consts[CONSTANT_BUILTIN_LIST] = (PyObject*)&PyList_Type;
832833

833834
for (int i=0; i < NUM_COMMON_CONSTANTS; i++) {
834835
assert(interp->common_consts[i] != NULL);

0 commit comments

Comments
 (0)