r51494 - python/branches/p3yk-noslice/Objects/stringobject.c python/branches/p3yk-noslice/Objects/tupleobject.c python/branches/p3yk-noslice/Objects/unicodeobject.c
Author: thomas.wouters Date: Wed Aug 23 00:37:35 2006 New Revision: 51494 Modified: python/branches/p3yk-noslice/Objects/stringobject.c python/branches/p3yk-noslice/Objects/tupleobject.c python/branches/p3yk-noslice/Objects/unicodeobject.c Log: Copy some of the special cases from normal slicing to extended slicing, for general speedup. Modified: python/branches/p3yk-noslice/Objects/stringobject.c ============================================================================== --- python/branches/p3yk-noslice/Objects/stringobject.c (original) +++ python/branches/p3yk-noslice/Objects/stringobject.c Wed Aug 23 00:37:35 2006 @@ -1210,6 +1210,17 @@ if (slicelength <= 0) { return PyString_FromStringAndSize("", 0); } + else if (start == 0 && step == 1 && + slicelength == PyString_GET_SIZE(self) && + PyString_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } + else if (step == 1) { + return PyString_FromStringAndSize( + PyString_AS_STRING(self) + start, + slicelength); + } else { source_buf = PyString_AsString((PyObject*)self); result_buf = (char *)PyMem_Malloc(slicelength); Modified: python/branches/p3yk-noslice/Objects/tupleobject.c ============================================================================== --- python/branches/p3yk-noslice/Objects/tupleobject.c (original) +++ python/branches/p3yk-noslice/Objects/tupleobject.c Wed Aug 23 00:37:35 2006 @@ -603,6 +603,12 @@ if (slicelength <= 0) { return PyTuple_New(0); } + else if (start == 0 && step == 1 && + slicelength == PyTuple_GET_SIZE(self) && + PyTuple_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } else { result = PyTuple_New(slicelength); if (!result) return NULL; Modified: python/branches/p3yk-noslice/Objects/unicodeobject.c ============================================================================== --- python/branches/p3yk-noslice/Objects/unicodeobject.c (original) +++ python/branches/p3yk-noslice/Objects/unicodeobject.c Wed Aug 23 00:37:35 2006 @@ -7083,6 +7083,12 @@ if (slicelength <= 0) { return PyUnicode_FromUnicode(NULL, 0); + } else if (start == 0 && step == 1 && slicelength == self->length && + PyUnicode_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } else if (step == 1) { + return PyUnicode_FromUnicode(self->str + start, slicelength); } else { source_buf = PyUnicode_AS_UNICODE((PyObject*)self); result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength*
(1) I would personally prefer if (step == 1) { if (start == 0 && ...) {} else {} } I realize that the compiler can do this, but I like flagging "this is an odd special case" (2) Why the CheckExact? Is there a promise (even in py3K) that constructors won't return a subclass? I had thought that the question was only when/if to *guarantee* that the subclass would be used. If the intent is to guard against subclasses with a different internal layout, then it should come before the GET_SIZE macros. -jJ On 8/22/06, thomas.wouters <python-checkins@python.org> wrote:
Author: thomas.wouters Date: Wed Aug 23 00:37:35 2006 New Revision: 51494
Modified: python/branches/p3yk-noslice/Objects/stringobject.c python/branches/p3yk-noslice/Objects/tupleobject.c python/branches/p3yk-noslice/Objects/unicodeobject.c Log:
Copy some of the special cases from normal slicing to extended slicing, for general speedup.
Modified: python/branches/p3yk-noslice/Objects/stringobject.c ============================================================================== --- python/branches/p3yk-noslice/Objects/stringobject.c (original) +++ python/branches/p3yk-noslice/Objects/stringobject.c Wed Aug 23 00:37:35 2006 @@ -1210,6 +1210,17 @@ if (slicelength <= 0) { return PyString_FromStringAndSize("", 0); } + else if (start == 0 && step == 1 && + slicelength == PyString_GET_SIZE(self) && + PyString_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } + else if (step == 1) { + return PyString_FromStringAndSize( + PyString_AS_STRING(self) + start, + slicelength); + } else { source_buf = PyString_AsString((PyObject*)self); result_buf = (char *)PyMem_Malloc(slicelength);
Modified: python/branches/p3yk-noslice/Objects/tupleobject.c ============================================================================== --- python/branches/p3yk-noslice/Objects/tupleobject.c (original) +++ python/branches/p3yk-noslice/Objects/tupleobject.c Wed Aug 23 00:37:35 2006 @@ -603,6 +603,12 @@ if (slicelength <= 0) { return PyTuple_New(0); } + else if (start == 0 && step == 1 && + slicelength == PyTuple_GET_SIZE(self) && + PyTuple_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } else { result = PyTuple_New(slicelength); if (!result) return NULL;
Modified: python/branches/p3yk-noslice/Objects/unicodeobject.c ============================================================================== --- python/branches/p3yk-noslice/Objects/unicodeobject.c (original) +++ python/branches/p3yk-noslice/Objects/unicodeobject.c Wed Aug 23 00:37:35 2006 @@ -7083,6 +7083,12 @@
if (slicelength <= 0) { return PyUnicode_FromUnicode(NULL, 0); + } else if (start == 0 && step == 1 && slicelength == self->length && + PyUnicode_CheckExact(self)) { + Py_INCREF(self); + return (PyObject *)self; + } else if (step == 1) { + return PyUnicode_FromUnicode(self->str + start, slicelength); } else { source_buf = PyUnicode_AS_UNICODE((PyObject*)self); result_buf = (Py_UNICODE *)PyMem_MALLOC(slicelength* _______________________________________________ Python-checkins mailing list Python-checkins@python.org http://mail.python.org/mailman/listinfo/python-checkins
On 8/23/06, Jim Jewett <jimjjewett@gmail.com> wrote:
(1) I would personally prefer
if (step == 1) { if (start == 0 && ...) {} else {} }
I realize that the compiler can do this, but I like flagging "this is an odd special case"
Well, they are two separate special cases. I personally prefer to keep them separate this way, and it reduces indentation level. (2) Why the CheckExact? Is there a promise (even in py3K) that
constructors won't return a subclass? I had thought that the question was only when/if to *guarantee* that the subclass would be used. If the intent is to guard against subclasses with a different internal layout, then it should come before the GET_SIZE macros.
As the checkin message says, those special cases are actually taken from normal slicing. It's safe for s[:] to return s only for exact instances of string (and unicode, and tuple) since it's completely equivalent. The same is not true for subclasses -- it might be, but it doesn't have to be. The issue isn't memory layout, it's mutability and type. Memory layout should be the same, anyway -- that's how C-type-subclasses work. There are explicit checks for this behaviour, by the way. -- Thomas Wouters <thomas@python.org> Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
participants (3)
-
Jim Jewett
-
Thomas Wouters
-
thomas.wouters