1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-26 13:53:03 +04:00

1 Commits

Author SHA1 Message Date
Hanbin Bae ca0d47c8e6 Merge pull request #28447 from Anemptyship:optimize/slice-layer
optimize(dnn): optimize Slice layer for strided inputs #28447

## 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:
1.  **Parallelization**: Uses `cv::parallel_for_` to parallelize the outermost dimension of the slice operation.
2.  **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.
3.  **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:**
| Test Case | Before (ms) | After (ms) | Speedup |
| :--- | :--- | :--- | :--- |
| **Strided Axis 0** `[::2, ...]` | 1.10 | **0.02** | **~55x** |
| **Strided Axis 2** `[..., ::2]` | 1.10 | **0.06** | **~18x** |
| **Contiguous** (Baseline) | 0.10 | 0.10 | 1.0x (Unchanged) |

### 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
      Patch to opencv_extra has the same branch name.
- [ ] The feature is well documented and sample code can be built with the project CMake
2026-02-07 13:17:23 +03:00