Commit 634950ff authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #615 from kmod/sre_compile3

Optimize creating tuples from lists
parents 424539a1 d1da6f57
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)
...@@ -135,7 +135,7 @@ int BoxedTuple::Resize(BoxedTuple** pv, size_t newsize) noexcept { ...@@ -135,7 +135,7 @@ int BoxedTuple::Resize(BoxedTuple** pv, size_t newsize) noexcept {
BoxedTuple* resized = new (newsize) BoxedTuple* resized = new (newsize)
BoxedTuple(newsize); // we want an uninitialized tuple, but this will memset it with 0. BoxedTuple(newsize); // we want an uninitialized tuple, but this will memset it with 0.
memmove(resized->elts, t->elts, t->size()); memmove(resized->elts, t->elts, sizeof(Box*) * t->size());
*pv = resized; *pv = resized;
return 0; return 0;
...@@ -286,6 +286,15 @@ extern "C" Box* tupleNew(Box* _cls, BoxedTuple* args, BoxedDict* kwargs) { ...@@ -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()); 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; std::vector<Box*, StlCompatAllocator<Box*>> elts;
for (auto e : elements->pyElements()) for (auto e : elements->pyElements())
elts.push_back(e); elts.push_back(e);
......
# skip-if: True
# This test works but 1) is very slow [the importing is, not the regex itself], and 2) throws warnings
# This test also seems to leak a lot of memory.
import sre_compile import sre_compile
import sre_constants
r = sre_compile.compile("a(b+)c", 0) r = sre_compile.compile("a(b+)c", 0)
print r.match("") print r.match("")
print r.match("ac") print r.match("ac")
print r.match("abc").groups() print r.match("abc").groups()
for i in xrange(100000): for i in xrange(100000):
r.match("abbc").groups() r.match("abbc").groups()
if i % 1000 == 0: if i % 10000 == 0:
print i print i
def identity(o):
return o
charset = [(sre_constants.RANGE, (128, 65535))]
print sre_compile._optimize_charset(charset, identity)
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