Utilities for rapid text file processing using Intel Hyperscan in Python
Project description
HyperGrep
Note: This project had been replaced by VectorGrep. No additional features or enhancements will be made to this library. Due to licensing changes in Intel Hyperscan starting in 5.5, all future development besides bug fixes will be dedicated to maintaining the Vectorscan version of this library. Vectorscan/VectorGrep also provides more options for increasing portability and supporting multiple architectures.
HyperGrep is a fast (Hyperspeed) Global Regular Expression Processing library for Python. It uses Intel Hyperscan to maximize performance, and can be used with multi-threaded or multi-processed applications. While a standard grep if designed to print, this is designed to allow full control over processing matches. The library supports scanning plaintext, gzip, and ztsd compressed files for regular expressions, and customizing the action to take when matched.
For full information on the amazing performance that can be obtained through Intel Hyperscan with, refer to:
Hyperscan
Table Of Contents
Key Features
- Simplicity
- No experience with Hyperscan required. Provides "grep" styled interfaces.
- No external dependencies, and no building required (on natively supported platforms).
- Built in support for compressed and uncompressed files.
- Speed
- Uses Hyperscan, a high-performance multiple regex matching library.
- Performs read and regex operations outside Python.
- Batches results for Python, reducing overhead (customizable).
- Parallelism
- Bypasses GIL (Global Interpreter Lock) during read and regex operations to allow proper multithreading.
- Python consumer threads (callbacks) are able to handle many producer threads (readers).
Compatibility
- Supports Python 3.10+
- Supports Linux systems with x86_64 architecture
- Tested on Ubuntu Trusty (14.04) and above
- Other Linux distros may work, but are not guaranteed
- May be able to be built on Windows/OSX manually
- More platforms are planned to be supported (natively) in the future
- Some regex constructs are not supported by Hyperscan in order to guarantee stable performance
- For more information refer to: Unsupported Constructs
Getting Started
Installation
-
Install HyperGrep via pip:
pip install hypergrep
-
Or via git clone:
git clone <path to fork> cd hypergrep pip install .
-
Or build and install from wheel:
# Build locally. git clone <path to fork> cd hypergrep make wheel # Push dist/hypergrep*.tar.gz to environment where it will be installed. pip install dist/hypergrep*.tar.gz
Examples
-
Read one file with the example single threaded command:
# hypergrep/scanner.py <regex> <file> hypergrep/scanner.py pattern ./hypergrep/scanner.py
-
Read multiple files with the multithreaded command (drop in replacement for
grep
where patterns are compatible):# From install: # hypergrep <regex> <file(s)> hypergrep pattern ./hypergrep/scanner.py # From package: # hypergrep/multiscanner.py <regex> <file> hypergrep/multiscanner.py pattern ./hypergrep/scanner.py
-
Collect all matches from a file, similar to grep, and perform a custom operation on results:
import hypergrep file = "./hypergrep/scanner.py" pattern = 'pattern' results, return_code = hypergrep.grep(file, [pattern]) for index, line in results: print(f'{index}: {line}')
-
Manually scan a file and perform a custom operation on match:
import hypergrep file = "./hypergrep/scanner.py" pattern = 'pattern' def on_match(matches: list, count: int) -> None: for index in range(count): match = matches[index] line = match.line.decode(errors='ignore') print(f'Custom print: {line.rstrip()}') hypergrep.scan(file, [pattern], on_match)
-
Override the
libhs
and/orlibzstd
libraries to use files outside the package. Must be called before any other usage ofhypergrep
:import hypergrep hypergrep.configure_libraries( libhs='/home/myuser/libhs.so.mybuild', libzstd='/home/myuser/libzstd.so.mybuild', )
Contributing
Refer to the Contributing Guide for information on how to contribute to this project.
Advanced Guides
Refer to How Tos for more advanced topics, such as building the shared library objects.
FAQ
Q: How does HyperGrep compare to other Hyperscan python libraries?
A: HyperGrep has a specific goal: provide a high performance "grep" like interface in python, but with more control. It is not intended to be a full set of bindings to Hyperscan. If you need full control over the low level backend, there are other python libraries intended for that use case. Here are a few of the reasons for the focused goal of this library:
- Simplify developer integration.
- No experience with Hyperscan required.
- Familiarity with
grep
variants beneficial, but not required.
- Avoid messy subprocess chains common in "parallel grep" implementations.
- Commands like
zgrep
are actually azcat
+grep
. This can lead to 3+ processes per file read. - Subprocessing is messy in general, best to minimize its use as much as possible.
- Commands like
- Optimize performance.
- Reduce callbacks to/from python to reduce overhead.
- Allow true multithreading during read and regex matching.
- Provide the pattern matched in multi-regex searches, without having to repeat the search in Python.
When it comes to performance, here is an example of the benefit of this design. Due to the performance of
Hyperscan, it is also often faster than native grep
variants, even while using python. Scenario setup:
- 2.10GHz Intel x86_64 Processor
- ~17M line file (~300M gzip compressed, ~3G uncompressed).
- ~800 PCRE patterns.
- Counting only, no extra processing of lines.
- Each job run 5 times and averaged (lower is better).
Scenario (Uncompressed timings in parenthesis) | HyperGrep | Full bindings | zgrep (grep) | |
---|---|---|---|---|
1 | ~90K matches, 1 pattern | 8.2s (2.5s) | 22.8s (15.5s) | 12.5s (5.2s) |
2 | ~900K matches, 10 patterns | 9.7s (3.8s) | 25.7s (16.8s) | 19.8s (17.3s) |
3 | ~15M matches, ~800 patterns | 44.2s (38.1s) | 73.5s (57.7s) | * |
4 | Scenario #3 (x4 files), 1 process (4 threads) | 49.6s (46.8s) | 1432.6s (1302.2s) | * |
- GNU grep does not allow multiple PCRE patterns natively, and concatenation via "or" failed.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file hypergrep-3.2.0.tar.gz
.
File metadata
- Download URL: hypergrep-3.2.0.tar.gz
- Upload date:
- Size: 3.6 MB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45251a60eab41547459a389dd29dee10aade1aa060db784ea11ba1bc4466f213 |
|
MD5 | 1b72d26f9f2cd4e313eee966f30f524c |
|
BLAKE2b-256 | c32b737f83b33480e0378d897240e9285ed325c06e9aff409b228acc6efef72f |
File details
Details for the file hypergrep-3.2.0-py3-none-any.whl
.
File metadata
- Download URL: hypergrep-3.2.0-py3-none-any.whl
- Upload date:
- Size: 3.7 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2dc14c5db37a899b6a1a9244d297f0a83795addf57ab494a05ca6bb6325d32d |
|
MD5 | 04d1ba03636afc99ef7361dc3d9e4fd4 |
|
BLAKE2b-256 | 68f5f0dabce131280865ea5eb084daeac129403a0c61ad118326eddffad10a0d |