]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-38328: Speed up the creation time of constant list and set display. (GH-17114)
authorBrandt Bucher <brandtbucher@gmail.com>
Tue, 26 Nov 2019 06:16:53 +0000 (22:16 -0800)
committerInada Naoki <songofacandy@gmail.com>
Tue, 26 Nov 2019 06:16:53 +0000 (15:16 +0900)
Lib/test/test_sys.py
Misc/NEWS.d/next/Core and Builtins/2019-11-11-23-44-15.bpo-38328.IFrrjq.rst [new file with mode: 0644]
Python/compile.c

index 9961dee754c6bd2c891e0911cde4362fadcc88e0..947c935f34728adb20dc1e6dfdc0dae16229dfe2 100644 (file)
@@ -1222,7 +1222,7 @@ class SizeofTest(unittest.TestCase):
         # list
         samples = [[], [1,2,3], ['1', '2', '3']]
         for sample in samples:
-            check(sample, vsize('Pn') + len(sample)*self.P)
+            check(list(sample), vsize('Pn') + len(sample)*self.P)
         # sortwrapper (list)
         # XXX
         # cmpwrapper (list)
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-11-11-23-44-15.bpo-38328.IFrrjq.rst b/Misc/NEWS.d/next/Core and Builtins/2019-11-11-23-44-15.bpo-38328.IFrrjq.rst
new file mode 100644 (file)
index 0000000..e0c5ca7
--- /dev/null
@@ -0,0 +1,2 @@
+Sped up the creation time of constant :class:`list` and :class:`set` displays.
+Patch by Brandt Bucher.
index f56c015fb96a3941de87bd3e07bf0c3f8e50ceff..98a4afa168c47cce0f116e0fb1f7aaf541ec1405 100644 (file)
@@ -197,6 +197,7 @@ static int compiler_visit_slice(struct compiler *, slice_ty,
                                 expr_context_ty);
 
 static int inplace_binop(struct compiler *, operator_ty);
+static int are_all_items_const(asdl_seq *, Py_ssize_t, Py_ssize_t);
 static int expr_constant(expr_ty);
 
 static int compiler_with(struct compiler *, stmt_ty, int);
@@ -3655,6 +3656,27 @@ starunpack_helper(struct compiler *c, asdl_seq *elts,
 {
     Py_ssize_t n = asdl_seq_LEN(elts);
     Py_ssize_t i, nsubitems = 0, nseen = 0;
+    if (n > 2 && are_all_items_const(elts, 0, n)) {
+        PyObject *folded = PyTuple_New(n);
+        if (folded == NULL) {
+            return 0;
+        }
+        PyObject *val;
+        for (i = 0; i < n; i++) {
+            val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
+            Py_INCREF(val);
+            PyTuple_SET_ITEM(folded, i, val);
+        }
+        if (outer_op == BUILD_SET_UNPACK) {
+            Py_SETREF(folded, PyFrozenSet_New(folded));
+            if (folded == NULL) {
+                return 0;
+            }
+        }
+        ADDOP_LOAD_CONST_NEW(c, folded);
+        ADDOP_I(c, outer_op, 1);
+        return 1;
+    }
     for (i = 0; i < n; i++) {
         expr_ty elt = asdl_seq_GET(elts, i);
         if (elt->kind == Starred_kind) {