$ show gliwka/hyperscan-java
hyperscan-java provides Java bindings for Vectorscan, a high-performance multiple regex matching library (fork of Intel's Hyperscan). It allows simultaneous matching of thousands of regular expressions against input streams with exceptional throughput. The library offers two usage modes: a direct Vectorscan API for maximum performance (with limited regex syntax) and a PatternFilter utility that combines Vectorscan pre-filtering with Java's full regex engine for complete PCRE support. Key features include UTF-8 handling with byte-to-character mapping, database serialization for avoiding recompilation, callback-based scanning for efficient match processing, and support for both string and byte-oriented scanning. The architecture is a clean wrapper around the native library via JavaCPP, with well-separated concerns: Expression (pattern definition), Database (compiled pattern set), Scanner (matching engine), and utility classes. The codebase demonstrates strong engineering practices with comprehensive test coverage, including regression tests for upstream bugs and a JMH benchmark. It is actively maintained with recent releases and CI across Linux and macOS (x86_64 and arm64).
The LLM identified 3 files worth highlighting for reuse:
A utility class that combines Vectorscan's high-speed pre-filtering with Java's full regex engine. It takes a list of Java Pattern objects, converts them to Vectorscan expressions (with PREFILTER flag), and uses Vectorscan to quickly identify potential matches. Only patterns flagged as potential matches (plus those Vectorscan cannot analyze) are returned for final confirmation via Matcher.find(). This pattern is reusable for any scenario requiring fast pre-filtering of many regex patterns against text, and it is self-contained with minimal dependencies on the wrapper API.
A manual UTF-8 encoder that encodes a Java String to a ByteBuffer while simultaneously building a byte-to-character index mapping. It handles all UTF-8 encoding cases (1-4 byte sequences), including valid surrogate pairs and lonely surrogates (replaced with '?'). The mapping is used by Scanner to convert byte offsets from the native library back to Java character indices. This component is self-contained and could be extracted for any project needing efficient UTF-8 encoding with position mapping, demonstrating careful handling of edge cases and performance optimization.
An interface and factory for mapping byte indices to character indices with memory-efficient implementations. The factory selects between ByteMapping (byte array, for strings up to 255 chars), ShortMapping (short array, up to 65535 chars), and IntMapping (int array, larger strings) based on the maximum character index. This strategy pattern minimizes memory usage for the mapping array, which can be large for long strings. The interface is clean and could be reused in any context requiring a compact index mapping with dynamic type selection.