Skip to content

Improve filter performances - #31

Open
sevdog wants to merge 1 commit into
masterfrom
filter-perf-improvements
Open

sevdog wants to merge 1 commit into
masterfrom
filter-perf-improvements

Conversation

@sevdog

@sevdog sevdog commented Nov 26, 2026

Copy link
Copy Markdown

Improved logics to better use python implementation for some comparison:

  1. EqualFilter._check_value: normalise with a list comprehension and also build self._value_set = frozenset(self._value); match tests membership against it. NotEqualFilter inherits it.

  2. StartswithFilter / EndswithFilter._check_value: normalise with a list comprehension and also build self._prefixes / self._suffixes as tuples; _check_startswith / _check_endswith become a single value.lower().startswith(self._prefixes) / .endswith(self._suffixes).

  3. KeywordFilter._check_value: normalise with a list comprehension; _check_keyword: lowercase value once, then keyword in value.

  4. DomainFilter._check_value: drop the redundant str() cast and also build self._domains (frozenset) and self._dot_suffixes (tuple of "." + domain); _check_domain returns value in self._domains or value.endswith(self._dot_suffixes).

  5. NETWORK:

    • add a module-level _parse_network(text) -> tuple[int, int, int] | None (version, first address, last address) decorated with functools.lru_cache(maxsize=4096). It returns None on ValueError/TypeError, so invalid values are cached as well.
    • NetworkFilter._check_value: also build per-version sorted _starts / _ends lists, keeping only the outermost blocks (CIDR blocks are either nested or disjoint, so the result is a list of disjoint intervals).
    • _check_network: bisect_right on _starts[version], then check that the event block ends within that interval. NotNetworkFilter inherits it.
    • The debug log for an unparsable event value stays, but without the exception text, because the cache stores None and not the exception.
  6. ComparatorFilter._check_value: also compute self._threshold as in D8 (None when there is no non-NaN term). _compare compares once against it and catches OverflowError.

  7. Add a DictQuery subclass that memoises get() keyed by (path, repr(default)), because the default can end up inside list results. Paths starting with certego.routing_history bypass the cache, and so do non-string paths. The check runs only on a miss, by never storing those paths. DictQuery's own traversal is untouched.

  8. Routing.match wraps the event in that subclass instead of DictQuery, and nothing else changes. Rule.output keeps the plain DictQuery.

@sevdog
sevdog requested a review from eugenioseveri November 26, 2026 08:34
@CLAassistant

CLAassistant commented Nov 26, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants