mirror of
https://github.com/opencv/opencv.git
synced 2026-07-27 22:33:03 +04:00
7942f976c1
optimize(dnn): parallelize Slice layer implementation (4.x) #28511 ### Summary Backport of PR #28447 to 4.x branch. ### Description This PR optimizes the SliceLayer implementation for strided inputs (where step > 1). The original implementation used a recursive element-wise copy (getSliceRecursive) for any strided slice, which was extremely inefficient. This PR introduces: - **Parallelization**: Uses `cv::parallel_for_` to parallelize the outermost dimension of the slice operation. - **Memcpy Optimization**: Automatically detects "pseudo-contiguous" blocks in strided slices (e.g., slicing an outer dimension but keeping inner dimensions intact) and uses `std::memcpy` instead of scalar loops. - **Refactoring**: Replaces the recursive function with a dedicated `ParallelSlice` loop body. ### Impact Significant performance improvement for strided slice operations (common in detection heads, strided sampling, etc.). ### Benchmark Results Tested on CPU with 20 threads. | Test Case | Baseline (ms) | Optimized (ms) | Speedup | | :--- | :--- | :--- | :--- | | Strided Axis 0 [::2, ...] | 1.10 | 0.02 | **~55x** | | Strided Axis 2 [..., ::2] | 1.15 | 0.11 | **~10.5x** | ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable - [ ] The feature is well documented and sample code can be built with the project CMake