From: <pj...@us...> - 2009-10-17 02:11:52
|
Revision: 6865 http://jython.svn.sourceforge.net/jython/?rev=6865&view=rev Author: pjenvey Date: 2009-10-17 02:11:46 +0000 (Sat, 17 Oct 2009) Log Message: ----------- fast path lists along with tuples and avoid the overhead of __len__'ing generators which can be a somewhat common make_array argument Modified Paths: -------------- trunk/jython/src/org/python/core/Py.java Modified: trunk/jython/src/org/python/core/Py.java =================================================================== --- trunk/jython/src/org/python/core/Py.java 2009-10-16 05:51:46 UTC (rev 6864) +++ trunk/jython/src/org/python/core/Py.java 2009-10-17 02:11:46 UTC (rev 6865) @@ -1904,23 +1904,29 @@ } } - static PyObject[] make_array(PyObject o) { - if (o instanceof PyTuple) { - return ((PyTuple) o).getArray(); + static PyObject[] make_array(PyObject iterable) { + // Special-case the common tuple and list cases, for efficiency + if (iterable instanceof PySequenceList) { + return ((PySequenceList) iterable).getArray(); } - // Guess result size and allocate space. + + // Guess result size and allocate space. The typical make_array arg supports + // __len__, with one exception being generators, so avoid the overhead of an + // exception from __len__ in their case int n = 10; - try { - n = o.__len__(); - } catch (PyException exc) { + if (!(iterable instanceof PyGenerator)) { + try { + n = iterable.__len__(); + } catch (PyException pye) { + // ok + } } List<PyObject> objs = new ArrayList<PyObject>(n); - for (PyObject item : o.asIterable()) { + for (PyObject item : iterable.asIterable()) { objs.add(item); } - PyObject dest[] = new PyObject[0]; - return (objs.toArray(dest)); + return objs.toArray(Py.EmptyObjects); } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |