An Evaluation of N-Gram Selection Strategies for Regular Expression Indexing in Contemporary Text Analysis Tasks

Ling Zhang, Shaleen Deep, Jignesh M. Patel, Karthikeyan Sankaralingam

Abstract

Regular expression (regex) matching is a cornerstone of text analysis but faces significant performance bottlenecks on massive datasets. N-gram indexing mitigates this by pre-calculating string locations to filter potential matches. This paper provides the first systematic evaluation of various n-gram selection strategies—FREE, BEST, LPMS, and VGGraph—across diverse workloads ranging from genomic sequences to 100GB production logs. Our results indicate that while complex, query-aware optimizations offer high precision for static workloads, simpler query-agnostic methods often provide superior scalability and robustness on modern hardware.

Introduction

Regular expressions are essential for modern data processing, yet brute-force execution on large-scale datasets is prohibitively slow. Systems typically employ n-gram indexing to accelerate this process: by indexing short strings (e.g., trigrams), they can quickly discard data segments that cannot possibly satisfy a query. However, the optimal choice of which n-grams to index remains a challenge. Traditional strategies, often designed for smaller datasets and single-core machines, lack a unified comparison in the context of modern, multi-core infrastructure and massive data volumes.

Methodology

The study evaluates four primary n-gram selection strategies within a unified framework:

  • FREE (Frequency-based): A query-agnostic approach that selects rare n-grams based solely on the dataset's properties.
  • BEST (Coverage-optimized): A query-aware strategy that treats selection as a budgeted maximum set cover problem to maximize filtering for specific queries.
  • LPMS (Linear Programming): An optimization-based approach using linear programming to approximate the optimal n-gram set.
  • VGGraph: A hybrid method combining frequency-based and coverage-optimized insights.
The system architecture for index construction and regex evaluation.

As illustrated in the architecture above, the evaluation pipeline involves input processing, index construction (including n-gram selection and building inverted or B+ tree indexes), and a regex engine for final evaluation.

Experimental Setup

The benchmarking suite includes both legacy and modern production workloads:

  • Legacy Benchmarks: Webpages (HTML), DBLP (bibliographic data), and Prosite (protein sequences).
  • Modern Production Workloads: System logs (SQL-Srvr, 14GB), traffic accident descriptions (US-Acc), and large-scale cloud logs (ADX, 100GB).
  • Synthetic Data: Used for robustness testing against unpredictable or "unseen" queries.

Results and Discussion

The evaluation reveals several critical insights regarding the trade-offs between construction efficiency and query performance.

The Construction-Query Trade-off

Complex algorithms like BEST achieve high precision but suffer from quadratic construction time. On datasets exceeding 10GB, BEST becomes computationally impractical. In contrast, the query-agnostic FREE algorithm scales linearly, achieving up to a 92% reduction in index build time with only a marginal (1.2%) increase in query latency.

Precision and Robustness

While query-aware strategies (BEST, LPMS) excel when the query set is static and known, they are inherently brittle. When faced with new, unseen queries, these optimized indexes often underperform compared to the more general FREE index.

Comparison of precision and query time across different n-gram selection methods as the number of index keys increases.

As shown in the results, while BEST maintains consistent precision, FREE's precision improves significantly as the number of index keys ($K$) increases, eventually rivaling or surpassing more complex methods in certain configurations.

Hardware Considerations

On modern hardware, the primary bottleneck has shifted from index storage size to construction time and multi-threaded efficiency. Methods that leverage parallelism, like the modernized FREE implementation, are better suited for today's high-memory, multi-core environments.

Conclusion

There is no "silver bullet" for n-gram selection; the optimal strategy depends on the specific workload. Engineers should favor FREE for large, diverse, or unpredictable workloads due to its scalability and robustness. BEST remains suitable for repeated queries on smaller, static datasets, while LPMS is effective for highly structured data. Future research should explore adaptive, on-the-fly indexing and the potential of hardware acceleration via GPUs or FPGAs.

Summary updated

BibTeX
@article{zhang2025evaluation,
  author = {Ling Zhang and Shaleen Deep and Jignesh M. Patel and Karthikeyan Sankaralingam},
  title = {{An Evaluation of N-Gram Selection Strategies for Regular Expression Indexing in Contemporary Text Analysis Tasks}},
  journal = {Proc. VLDB Endow.},
  year = {2025},
}
Figure