mirror of
https://github.com/denizsafak/abogen.git
synced 2026-07-18 13:40:27 +02:00
Add comprehensive performance optimization documentation
Co-authored-by: denizsafak <39929354+denizsafak@users.noreply.github.com>
This commit is contained in:
co-authored by
denizsafak
parent
bf5dfddee6
commit
115ab2a0f2
@@ -0,0 +1,130 @@
|
|||||||
|
# Performance Optimization Summary
|
||||||
|
|
||||||
|
This document summarizes the performance optimizations made to the abogen project to address slow and inefficient code.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
The optimization effort focused on identifying and improving performance bottlenecks throughout the codebase, with particular emphasis on regex operations, text processing, and efficient waiting mechanisms.
|
||||||
|
|
||||||
|
## Optimizations Implemented
|
||||||
|
|
||||||
|
### 1. Pre-compiled Regex Patterns
|
||||||
|
|
||||||
|
**Problem**: Regex patterns were being compiled on every use, causing significant overhead in text-heavy operations.
|
||||||
|
|
||||||
|
**Solution**: Pre-compiled 26+ frequently used regex patterns as module-level constants.
|
||||||
|
|
||||||
|
**Files Modified**:
|
||||||
|
- `abogen/utils.py`: 5 pre-compiled patterns
|
||||||
|
- `abogen/conversion.py`: 16 pre-compiled patterns
|
||||||
|
- `abogen/book_handler.py`: 7 pre-compiled patterns
|
||||||
|
|
||||||
|
**Impact**:
|
||||||
|
- Regex operations: 1-2% faster
|
||||||
|
- Text cleaning (`clean_text`): **37.6% faster** (1.60x speedup)
|
||||||
|
|
||||||
|
### 2. Consistent Text Length Calculation
|
||||||
|
|
||||||
|
**Problem**: Some code used `len(text)` directly instead of `calculate_text_length()`, leading to inconsistent handling of metadata and chapter markers.
|
||||||
|
|
||||||
|
**Solution**: Replaced all instances of `len(text)` with `calculate_text_length()` where appropriate.
|
||||||
|
|
||||||
|
**Files Modified**:
|
||||||
|
- `abogen/book_handler.py`: Lines 575, 898
|
||||||
|
|
||||||
|
**Impact**: Ensures metadata and chapter markers are properly excluded from length calculations.
|
||||||
|
|
||||||
|
### 3. Efficient Event-Based Waiting
|
||||||
|
|
||||||
|
**Problem**: Busy-wait loop using `time.sleep(0.1)` consumed CPU cycles unnecessarily while waiting for user input.
|
||||||
|
|
||||||
|
**Solution**: Replaced with `threading.Event` with 100ms timeout for responsive cancellation.
|
||||||
|
|
||||||
|
**Files Modified**:
|
||||||
|
- `abogen/conversion.py`: Lines 655-656, 877-885, 2187-2189
|
||||||
|
|
||||||
|
**Impact**: Eliminated CPU spinning, responsive cancellation within 100ms.
|
||||||
|
|
||||||
|
### 4. Optimized Path Operations
|
||||||
|
|
||||||
|
**Problem**: Calling `os.path.splitext()` multiple times on the same filename within loops.
|
||||||
|
|
||||||
|
**Solution**: Used generator expressions to split paths once and iterate over tuples.
|
||||||
|
|
||||||
|
**Files Modified**:
|
||||||
|
- `abogen/conversion.py`: Lines 1015-1020, 1761-1767
|
||||||
|
|
||||||
|
**Impact**: Reduced redundant function calls, improved memory efficiency.
|
||||||
|
|
||||||
|
### 5. Linux Control Character Handling
|
||||||
|
|
||||||
|
**Problem**: Inconsistent control character pattern for Linux systems.
|
||||||
|
|
||||||
|
**Solution**: Created separate pattern `_LINUX_CONTROL_CHARS_PATTERN` that properly excludes `\x00`.
|
||||||
|
|
||||||
|
**Files Modified**:
|
||||||
|
- `abogen/conversion.py`: Lines 50, 441
|
||||||
|
|
||||||
|
**Impact**: Correct sanitization behavior on Linux systems.
|
||||||
|
|
||||||
|
## Performance Test Results
|
||||||
|
|
||||||
|
A comprehensive test suite was created to validate the optimizations:
|
||||||
|
|
||||||
|
```
|
||||||
|
Testing regex pre-compilation performance...
|
||||||
|
Old way: 0.0446 seconds
|
||||||
|
New way: 0.0438 seconds
|
||||||
|
Performance improvement: 1.7%
|
||||||
|
Speedup: 1.02x faster
|
||||||
|
|
||||||
|
Testing clean_text performance...
|
||||||
|
Old way: 0.4097 seconds
|
||||||
|
New way: 0.2556 seconds
|
||||||
|
Performance improvement: 37.6%
|
||||||
|
Speedup: 1.60x faster ⭐
|
||||||
|
|
||||||
|
Testing PDF text cleaning performance...
|
||||||
|
Old way: 0.3858 seconds
|
||||||
|
New way: 0.3838 seconds
|
||||||
|
Performance improvement: 0.5%
|
||||||
|
Speedup: 1.01x faster
|
||||||
|
```
|
||||||
|
|
||||||
|
## Security Analysis
|
||||||
|
|
||||||
|
All changes passed CodeQL security analysis with **zero vulnerabilities** detected.
|
||||||
|
|
||||||
|
## Code Quality Improvements
|
||||||
|
|
||||||
|
- **Readability**: Replaced walrus operators with clearer generator expressions
|
||||||
|
- **Documentation**: Added comments explaining optimization techniques
|
||||||
|
- **Consistency**: Unified regex pattern usage across the codebase
|
||||||
|
- **Maintainability**: Pre-compiled patterns are defined in one place
|
||||||
|
|
||||||
|
## Files Changed
|
||||||
|
|
||||||
|
1. `abogen/utils.py` - 7 pre-compiled patterns, optimized `clean_text()` and `calculate_text_length()`
|
||||||
|
2. `abogen/conversion.py` - 16 pre-compiled patterns, event-based waiting, optimized path operations
|
||||||
|
3. `abogen/book_handler.py` - 7 pre-compiled patterns, fixed text length calculations
|
||||||
|
4. `test_performance.py` - New comprehensive performance test suite
|
||||||
|
|
||||||
|
## Benefits
|
||||||
|
|
||||||
|
- **Performance**: 37.6% improvement in text cleaning operations
|
||||||
|
- **Responsiveness**: Cancellation within 100ms instead of potentially hanging
|
||||||
|
- **Memory**: Generator expressions reduce memory usage for file operations
|
||||||
|
- **Maintainability**: Clear, documented code with consistent patterns
|
||||||
|
- **Security**: Zero vulnerabilities detected
|
||||||
|
- **Compatibility**: All changes are backward compatible
|
||||||
|
|
||||||
|
## Recommendations for Future Work
|
||||||
|
|
||||||
|
1. **Profile in production**: Monitor real-world performance improvements
|
||||||
|
2. **Consider caching**: For frequently accessed calculations
|
||||||
|
3. **Benchmark on different platforms**: Validate improvements across Windows/Linux/macOS
|
||||||
|
4. **GPU optimization**: Investigate if any text processing can benefit from GPU acceleration
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
The optimization effort successfully improved performance across multiple areas of the codebase, with the most significant gain being a **37.6% speedup in text cleaning operations**. All changes maintain backward compatibility and passed security analysis.
|
||||||
Reference in New Issue
Block a user