comparison Python/ceval.c @ 72550:df6deb7bb772

Remove commented code: str+=str is no more super-optimized
author Victor Stinner <victor.stinner@haypocalc.com>
date Sat, 01 Oct 2011 01:26:08 +0200
parents 8beaa9a37387
children 6da962d77eb3
comparison
equal deleted inserted replaced
72549:d94b0b371878 72550:df6deb7bb772
134 static PyObject * cmp_outcome(int, PyObject *, PyObject *); 134 static PyObject * cmp_outcome(int, PyObject *, PyObject *);
135 static PyObject * import_from(PyObject *, PyObject *); 135 static PyObject * import_from(PyObject *, PyObject *);
136 static int import_all_from(PyObject *, PyObject *); 136 static int import_all_from(PyObject *, PyObject *);
137 static void format_exc_check_arg(PyObject *, const char *, PyObject *); 137 static void format_exc_check_arg(PyObject *, const char *, PyObject *);
138 static void format_exc_unbound(PyCodeObject *co, int oparg); 138 static void format_exc_unbound(PyCodeObject *co, int oparg);
139 static PyObject * unicode_concatenate(PyObject *, PyObject *,
140 PyFrameObject *, unsigned char *);
141 static PyObject * special_lookup(PyObject *, char *, PyObject **); 139 static PyObject * special_lookup(PyObject *, char *, PyObject **);
142 140
143 #define NAME_ERROR_MSG \ 141 #define NAME_ERROR_MSG \
144 "name '%.200s' is not defined" 142 "name '%.200s' is not defined"
145 #define GLOBAL_NAME_ERROR_MSG \ 143 #define GLOBAL_NAME_ERROR_MSG \
1507 break; 1505 break;
1508 1506
1509 TARGET(BINARY_ADD) 1507 TARGET(BINARY_ADD)
1510 w = POP(); 1508 w = POP();
1511 v = TOP(); 1509 v = TOP();
1512 if (PyUnicode_CheckExact(v) && 1510 if (PyUnicode_Check(v) && PyUnicode_Check(w))
1513 PyUnicode_CheckExact(w)) { 1511 x = PyUnicode_Concat(v, w);
1514 x = unicode_concatenate(v, w, f, next_instr); 1512 else
1515 /* unicode_concatenate consumed the ref to v */
1516 goto skip_decref_vx;
1517 }
1518 else {
1519 x = PyNumber_Add(v, w); 1513 x = PyNumber_Add(v, w);
1520 } 1514 Py_DECREF(v);
1521 Py_DECREF(v);
1522 skip_decref_vx:
1523 Py_DECREF(w); 1515 Py_DECREF(w);
1524 SET_TOP(x); 1516 SET_TOP(x);
1525 if (x != NULL) DISPATCH(); 1517 if (x != NULL) DISPATCH();
1526 break; 1518 break;
1527 1519
1668 break; 1660 break;
1669 1661
1670 TARGET(INPLACE_ADD) 1662 TARGET(INPLACE_ADD)
1671 w = POP(); 1663 w = POP();
1672 v = TOP(); 1664 v = TOP();
1673 if (PyUnicode_CheckExact(v) && 1665 if (PyUnicode_Check(v) && PyUnicode_Check(w))
1674 PyUnicode_CheckExact(w)) { 1666 x = PyUnicode_Concat(v, w);
1675 x = unicode_concatenate(v, w, f, next_instr); 1667 else
1676 /* unicode_concatenate consumed the ref to v */
1677 goto skip_decref_v;
1678 }
1679 else {
1680 x = PyNumber_InPlaceAdd(v, w); 1668 x = PyNumber_InPlaceAdd(v, w);
1681 } 1669 Py_DECREF(v);
1682 Py_DECREF(v);
1683 skip_decref_v:
1684 Py_DECREF(w); 1670 Py_DECREF(w);
1685 SET_TOP(x); 1671 SET_TOP(x);
1686 if (x != NULL) DISPATCH(); 1672 if (x != NULL) DISPATCH();
1687 break; 1673 break;
1688 1674
4513 format_exc_check_arg(PyExc_NameError, 4499 format_exc_check_arg(PyExc_NameError,
4514 UNBOUNDFREE_ERROR_MSG, name); 4500 UNBOUNDFREE_ERROR_MSG, name);
4515 } 4501 }
4516 } 4502 }
4517 4503
4518 static PyObject *
4519 unicode_concatenate(PyObject *v, PyObject *w,
4520 PyFrameObject *f, unsigned char *next_instr)
4521 {
4522 /* This function implements 'variable += expr' when both arguments
4523 are (Unicode) strings. */
4524
4525 w = PyUnicode_Concat(v, w);
4526 Py_DECREF(v);
4527 return w;
4528
4529 /* XXX: This optimization is currently disabled as unicode objects in the
4530 new flexible representation are not in-place resizable anymore. */
4531 #if 0
4532 Py_ssize_t v_len = PyUnicode_GET_SIZE(v);
4533 Py_ssize_t w_len = PyUnicode_GET_SIZE(w);
4534 Py_ssize_t new_len = v_len + w_len;
4535 if (new_len < 0) {
4536 PyErr_SetString(PyExc_OverflowError,
4537 "strings are too large to concat");
4538 return NULL;
4539 }
4540
4541 if (Py_REFCNT(v) == 2) {
4542 /* In the common case, there are 2 references to the value
4543 * stored in 'variable' when the += is performed: one on the
4544 * value stack (in 'v') and one still stored in the
4545 * 'variable'. We try to delete the variable now to reduce
4546 * the refcnt to 1.
4547 */
4548 switch (*next_instr) {
4549 case STORE_FAST:
4550 {
4551 int oparg = PEEKARG();
4552 PyObject **fastlocals = f->f_localsplus;
4553 if (GETLOCAL(oparg) == v)
4554 SETLOCAL(oparg, NULL);
4555 break;
4556 }
4557 case STORE_DEREF:
4558 {
4559 PyObject **freevars = (f->f_localsplus +
4560 f->f_code->co_nlocals);
4561 PyObject *c = freevars[PEEKARG()];
4562 if (PyCell_GET(c) == v)
4563 PyCell_Set(c, NULL);
4564 break;
4565 }
4566 case STORE_NAME:
4567 {
4568 PyObject *names = f->f_code->co_names;
4569 PyObject *name = GETITEM(names, PEEKARG());
4570 PyObject *locals = f->f_locals;
4571 if (PyDict_CheckExact(locals) &&
4572 PyDict_GetItem(locals, name) == v) {
4573 if (PyDict_DelItem(locals, name) != 0) {
4574 PyErr_Clear();
4575 }
4576 }
4577 break;
4578 }
4579 }
4580 }
4581
4582 if (Py_REFCNT(v) == 1 && !PyUnicode_CHECK_INTERNED(v) &&
4583 !PyUnicode_IS_COMPACT((PyUnicodeObject *)v)) {
4584 /* Now we own the last reference to 'v', so we can resize it
4585 * in-place.
4586 */
4587 if (PyUnicode_Resize(&v, new_len) != 0) {
4588 /* XXX if PyUnicode_Resize() fails, 'v' has been
4589 * deallocated so it cannot be put back into
4590 * 'variable'. The MemoryError is raised when there
4591 * is no value in 'variable', which might (very
4592 * remotely) be a cause of incompatibilities.
4593 */
4594 return NULL;
4595 }
4596 /* copy 'w' into the newly allocated area of 'v' */
4597 memcpy(PyUnicode_AS_UNICODE(v) + v_len,
4598 PyUnicode_AS_UNICODE(w), w_len*sizeof(Py_UNICODE));
4599 return v;
4600 }
4601 else {
4602 /* When in-place resizing is not an option. */
4603 w = PyUnicode_Concat(v, w);
4604 Py_DECREF(v);
4605 return w;
4606 }
4607 #endif
4608 }
4609
4610 #ifdef DYNAMIC_EXECUTION_PROFILE 4504 #ifdef DYNAMIC_EXECUTION_PROFILE
4611 4505
4612 static PyObject * 4506 static PyObject *
4613 getarray(long a[256]) 4507 getarray(long a[256])
4614 { 4508 {