Commit 5aed5b77 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Optimize creating tuples from lists

parent 77b84b6f
import sre_compile
import sre_constants
def identity(o):
return o
charset = [(sre_constants.RANGE, (128, 65535))]
for i in xrange(100):
sre_compile._optimize_unicode(charset, identity)
# print sre_compile._optimize_charset(charset, identity)
l = range(256)
for i in xrange(200000):
tuple(l)
......@@ -286,6 +286,15 @@ extern "C" Box* tupleNew(Box* _cls, BoxedTuple* args, BoxedDict* kwargs) {
raiseExcHelper(TypeError, "'%s' is an invalid keyword argument for this function", kw->data());
}
if (cls == tuple_cls) {
// Call PySequence_Tuple since it has some perf special-cases
// that can make it quite a bit faster than the generic pyElements iteration:
Box* r = PySequence_Tuple(elements);
if (!r)
throwCAPIException();
return r;
}
std::vector<Box*, StlCompatAllocator<Box*>> elts;
for (auto e : elements->pyElements())
elts.push_back(e);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment