|
| 1 | +Performance Configuration |
| 2 | +========================= |
| 3 | + |
| 4 | +The Flask-InputFilter library provides various performance optimization settings that can be tuned based on your specific use case. |
| 5 | + |
| 6 | +Overview |
| 7 | +-------- |
| 8 | + |
| 9 | +The ``PerformanceConfig`` class allows you to control various performance-related settings such as lazy evaluation, caching, and string interning. |
| 10 | + |
| 11 | +Configuration Options |
| 12 | +--------------------- |
| 13 | + |
| 14 | +The following options are available: |
| 15 | + |
| 16 | +- **USE_LAZY_EVALUATION**: Enable lazy evaluation for filter chains (default: ``True``) |
| 17 | +- **LAZY_EVALUATION_THRESHOLD**: Minimum number of filters to trigger lazy evaluation (default: ``5``) |
| 18 | +- **REGEX_CACHE_SIZE**: Maximum number of compiled regex patterns to cache (default: ``128``) |
| 19 | +- **USE_STRING_INTERNING**: Enable automatic string interning for field names (default: ``True``) |
| 20 | +- **LARGE_DATASET_THRESHOLD**: Number of fields to consider as "large" for optimization (default: ``10``) |
| 21 | +- **USE_CYTHON**: Use Cython-compiled modules when available (default: ``True``) |
| 22 | + |
| 23 | +Usage Examples |
| 24 | +-------------- |
| 25 | + |
| 26 | +Basic Configuration |
| 27 | +~~~~~~~~~~~~~~~~~~~ |
| 28 | + |
| 29 | +.. code-block:: python |
| 30 | +
|
| 31 | + from flask_inputfilter.performance_config import PerformanceConfig |
| 32 | + |
| 33 | + # Set individual options |
| 34 | + PerformanceConfig.USE_LAZY_EVALUATION = True |
| 35 | + PerformanceConfig.REGEX_CACHE_SIZE = 256 |
| 36 | +
|
| 37 | +Preset Configurations |
| 38 | +~~~~~~~~~~~~~~~~~~~~~ |
| 39 | + |
| 40 | +The library provides preset configurations for common scenarios: |
| 41 | + |
| 42 | +**High Performance Mode** |
| 43 | + |
| 44 | +Optimized for maximum speed, may use more memory: |
| 45 | + |
| 46 | +.. code-block:: python |
| 47 | +
|
| 48 | + from flask_inputfilter.performance_config import PerformanceConfig |
| 49 | + |
| 50 | + PerformanceConfig.set_high_performance() |
| 51 | +
|
| 52 | +**Low Memory Mode** |
| 53 | + |
| 54 | +Optimized for minimal memory usage, may be slower: |
| 55 | + |
| 56 | +.. code-block:: python |
| 57 | +
|
| 58 | + from flask_inputfilter.performance_config import PerformanceConfig |
| 59 | + |
| 60 | + PerformanceConfig.set_low_memory() |
| 61 | +
|
| 62 | +**Balanced Mode** |
| 63 | + |
| 64 | +Reset to default balanced configuration: |
| 65 | + |
| 66 | +.. code-block:: python |
| 67 | +
|
| 68 | + from flask_inputfilter.performance_config import PerformanceConfig |
| 69 | + |
| 70 | + PerformanceConfig.set_balanced() |
| 71 | +
|
| 72 | +Lazy Filter Chain |
| 73 | +----------------- |
| 74 | + |
| 75 | +The ``LazyFilterChain`` class provides lazy evaluation for filter chains, which can significantly improve performance when dealing with large filter chains or when early filters might invalidate the need for later processing. |
| 76 | + |
| 77 | +.. autoclass:: flask_inputfilter.filters.LazyFilterChain |
| 78 | + :members: |
| 79 | + :undoc-members: |
| 80 | + :show-inheritance: |
| 81 | + |
| 82 | +Performance Config API |
| 83 | +---------------------- |
| 84 | + |
| 85 | +.. autoclass:: flask_inputfilter.performance_config.PerformanceConfig |
| 86 | + :members: |
| 87 | + :undoc-members: |
| 88 | + :show-inheritance: |
| 89 | + |
| 90 | +Best Practices |
| 91 | +-------------- |
| 92 | + |
| 93 | +1. **For High-Volume APIs**: Use ``set_high_performance()`` to maximize throughput |
| 94 | +2. **For Memory-Constrained Environments**: Use ``set_low_memory()`` to minimize memory footprint |
| 95 | +3. **For Complex Validation Chains**: Keep ``USE_LAZY_EVALUATION`` enabled to avoid unnecessary processing |
| 96 | +4. **For Regex-Heavy Validation**: Increase ``REGEX_CACHE_SIZE`` if you have many unique regex patterns |
| 97 | + |
| 98 | +Performance Tips |
| 99 | +---------------- |
| 100 | + |
| 101 | +- String interning is most effective when you have many repeated field names across requests |
| 102 | +- Lazy evaluation provides the most benefit when you have long filter chains (>5 filters) |
| 103 | +- The regex cache is shared across all validator instances, so increasing its size helps with diverse patterns |
| 104 | +- Cython compilation provides 30-50% performance improvement for core validation logic |
0 commit comments