Skip to content

Conversation

@YuyuZha0
Copy link

Key Motivation

The existing PatternFilter class is effective for single-threaded use but has two critical limitations in concurrent environments:

  1. It is not thread-safe.
  2. It is expensive to instantiate, as each new PatternFilter() triggers a full Hyperscan database compilation.

This forces consumers to implement their own complex and error-prone pooling or ThreadLocal management to use Hyperscan filtering safely and efficiently in multi-threaded applications like web servers.

This PR introduces ScopedPatternFilterFactory as a high-level, managed solution that solves these problems out of the box.

Key Differences & Improvements

The new ScopedPatternFilterFactory supersedes the manual PatternFilter approach by providing:

  • Thread-Safety by Design: The factory manages a unique, isolated ScopedPatternFilter instance for each thread, eliminating the need for external synchronization.

  • High Performance via Caching: The expensive database compilation is performed only once per thread. Subsequent requests for a filter on the same thread reuse the already-compiled instance, drastically improving performance.

  • Simplified and Safer API: Users interact with a single factory instance and obtain a filter via try-with-resources (var filter = factory.get()). This returns a proxy that prevents accidental closure of the shared, thread-local resource.

  • Automatic Resource Cleanup: It introduces a robust, PhantomReference-based mechanism to automatically release native resources for threads that have terminated, preventing memory leaks in dynamic environments.

@YuyuZha0
Copy link
Author

@gliwka Could you please help me review this PR? Thank you!

@gliwka
Copy link
Owner

gliwka commented Oct 31, 2025

Thank you for your contribution. I'll take a look over the weekend.

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