Commit d0fb0616 authored by Kevin Modzelewski's avatar Kevin Modzelewski

Make a small optimization to sre_compile._compile

Switch from expensive old-style-class iteration to our faster iteration.
We could/should probably make old-style iteration much faster than it currently
is for us (10x slower than CPython), but this is a pretty targeted change and
makes compiling regular expressions much faster: it cuts (non-LLVM) _compile()
time by 2x.
parent 215f52d2
......@@ -37,7 +37,22 @@ def _compile(code, pattern, flags):
REPEATING_CODES = _REPEATING_CODES
SUCCESS_CODES = _SUCCESS_CODES
ASSERT_CODES = _ASSERT_CODES
for op, av in pattern:
# Pyston change: iterating over a SubPattern is fairly expensive:
# - SubPattern defines __getitem__ but not __iter__, so we create a seqiter
# - for each iteration, we call instance.getitem, which looks up
# SubPattern.__getitem__, and calls it
# - SubPattern.__getitem__ just does `return self.data[idx]`, which throws an
# IndexError to signal that the iteration is done.
# All the exceptions and extra layers of indirection add up.
# Cut that out by simply iterating over pattern.data, which looks like is
# usually a list, and we can use our fast iteration.
if isinstance(pattern, sre_parse.SubPattern):
pattern_iter = pattern.data
else:
pattern_iter = pattern
for op, av in pattern_iter:
if op in LITERAL_CODES:
if flags & SRE_FLAG_IGNORECASE:
emit(OPCODES[OP_IGNORE[op]])
......
class C:
def __init__(self):
self.l = range(10)
def __getitem__(self, idx):
return self.l[idx]
def f():
c = C()
total = 0
for _ in xrange(100000):
for i in c:
total += i
print total
f()
......@@ -58,6 +58,9 @@ void raiseRaw(const ExcInfo& e) {
assert(gc::isValidGCObject(e.traceback));
#if STAT_EXCEPTIONS
static StatCounter num_exceptions("num_exceptions");
num_exceptions.log();
std::string stat_name;
if (PyType_Check(e.type))
stat_name = "num_exceptions_" + std::string(static_cast<BoxedClass*>(e.type)->tp_name);
......
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