From a329147d28b159ae40e4686e8a5d1ed27d97fd77 Mon Sep 17 00:00:00 2001 From: William Ballenthin Date: Fri, 5 Nov 2021 16:32:23 -0600 Subject: [PATCH] engine: some: short circuit --- capa/engine.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/capa/engine.py b/capa/engine.py index 871119c9..0f78a6c1 100644 --- a/capa/engine.py +++ b/capa/engine.py @@ -207,14 +207,16 @@ class Some(Statement): def evaluate(self, ctx): capa.perf.counters["evaluate.feature"] += 1 capa.perf.counters["evaluate.feature.some"] += 1 - - results = [child.evaluate(ctx) for child in self.children] - # note that here we cast the child result as a bool - # because we've overridden `__bool__` above. - # - # we can't use `if child is True` because the instance is not True. - success = sum([1 for child in results if bool(child) is True]) >= self.count - return Result(success, self, results) + + results = [] + for child in self.children: + result = child.evaluate(ctx) + results.append(result) + if len(results) >= self.count: + # short circuit as soon as we hit the threshold + return Result(True, self, results) + + return Result(False, self, results) class Range(Statement):