Megascan
Megascan is a high-performance ultra-large-scale rule matching library of Kunpeng BoostKit. It can effectively process matching based on tens of millions of rules.
Rule matching is a fundamental requirement in intelligent data analytics scenarios. However, massive-scale rule matching is a big challenge. Rule matching libraries, such as Ultrascan, experience performance degradation when processing over 100,000 rules. In such cases, a dedicated algorithm library essential for massive-scale rule matching needs to be customized.
Megascan provides different algorithms for two different scenarios. For rule sets containing hundreds of thousands of variable-length rules, it employs an Aho-Corasick (AC) Trie-based unanchored multi-pattern matching algorithm. For rule sets with tens of millions of fixed-length rules, it utilizes a Hash-based anchored multi-pattern matching algorithm.

Megascan operates in two phases: rule preprocessing and rule matching.

Megascan uses a two-level hash structure to maintain rules in an ultra-large rule set, improving the search speed without deteriorating the efficiency of adding, deleting, and modifying rules.

Megascan provides two sets of external APIs: legacy handler-compatible APIs (ms_handler, ms_rule, ms_compile, and ms_runtime) and three-layer object model APIs (database, scratch, and stream). In terms of functionality, Megascan supports not only basic rules but also composite rules formed by logical operations (&, |, and !). It also supports full block scanning, cross-segment stream scanning, dynamic rule deletion during runtime, and type-specific custom memory allocators (database, scratch, stream, and misc).
- Logical combination
Composite rules (such as 101&102 and !101) do not perform bottom-layer matching. Instead, they are derived based on the matching results of basic rules. During compilation, each composite expression is parsed into a logical expression tree, and a logical bit is allocated to each basic rule to record whether it is matched. In addition, a reverse index is created from basic rules to the composite rules that depend on them. At runtime, when a basic rule is matched, its logical bit is set, and then the reverse index is used to evaluate only a small number of composite rules affected by this match (incremental evaluation), instead of traversing all composite rules. This greatly reduces unnecessary calculations when there are a large number of composite rules.

- Stream mode
In stream mode, continuous data is split into multiple segments for sequential scanning. The core idea is to separate the cross-segment persistent state from the temporary workspace for a single scan. A stream stores the AC automaton state nodes, accumulated offsets, and logical states, enabling matching to span arbitrary segment boundaries. The scratch space stores temporary data such as the hit buffer and evaluation results of the current scan and is reused across multiple segments. It records only the state bytes that have been modified in the current segment, so that only these dirty bytes need to be cleared during reset, avoiding full clearing. This ensures efficiency in long stream and multi-segment scenarios.

- Rule aging
Rule aging refers to the dynamic deletion of rules at runtime. A deletion request first applies all changes in the candidate state: the basic rules to be deleted are excluded from the compact AC structure, and the corresponding composite rules and their dependencies are removed from the compilation context. Once validation succeeds, the replacement is committed atomically in one step and the version number is incremented. Since the version number is incremented with each change, the stream state object and temporary workspace created before the deletion will detect the version change upon the next use. The stream state object must be reset, while the temporary workspace is automatically refreshed. Deletion should be performed after all ongoing scans are complete to ensure that subsequent scans use a consistent rule library state.

Key technologies:
AC Trie array-based table lookup optimization, AC automaton, multi-level hashing, parallelized hashing, and vectorized collision check.
Applicable scenarios:
Information security data distribution, fine-grained data distribution for carriers, public safety data distribution, IDPS, WAF, foundation model application firewalls, etc.