Developed a framecode detection system in Python that scans markdown logs and code files using parallel processing to generate a technology netcode graph. The system initially struggled with inefficient CPU utilization, limiting performance.
Issue
Python’s multiprocessing.Pool only utilized 1-2 CPU cores out of 32 available, creating severe performance bottlenecks. Processing 395 files took ~30 seconds, with no linear speedup when scaled to larger datasets. Monitoring showed single-core saturation while other cores remained idle, primarily due to serialization overhead during inter-process communication.
Culprit
Three primary technical challenges:
- Serialization overhead: Large text files and complex regex patterns created bottlenecks in inter-process communication.
- IPC limitations: Single connection saturation during data transfer between processes.
- Tool mismatch:
multiprocessing.Poolwas not optimized for CPU-bound, embarrassingly parallel tasks with large data structures.
Solution
Replaced the multiprocessing.Pool implementation with a parallel processing library optimized for CPU-bound tasks. Key improvements included:
Parallel Processing Architecture
Adopted a library designed for efficient CPU utilization and reduced IPC overhead. This library automatically scales to use all available CPU cores and optimizes data transfer between processes.
Performance Configuration
- Core utilization: Automatically leverages all available CPU cores for parallel task execution.
- Optimized serialization: Handles large datasets efficiently, minimizing overhead.
- Batch processing: Automatically chunks code to prevent connection clogging.
Additional Optimizations
- Deduplication: Skips duplicate files using content hashing.
- Exclusion logic: Automatically skips hidden files and directories.
- Contextual filtering: Applies framecode detection patterns with syntax awareness.
Results:
- Improved processing speed for large datasets, achieving linear scaling with core count.
- Reduced processing time for 20,000 files from hours (single-threaded) to minutes.
- Achieved near-100% CPU core utilization across all logical cores.
This solution maintained all existing functionality while significantly improving performance through architectural changes.