Skip to content

Commit a3caa83

Browse files
committed
Use std::array instead of std::vector to reduce memory allocations.
1 parent 752c01c commit a3caa83

File tree

4 files changed

+35
-35
lines changed

4 files changed

+35
-35
lines changed

modules/cudaarithm/src/cuda/split_merge.cu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace
6767
{
6868
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
6969
{
70-
const std::vector<GlobPtrSz<T>> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1])};
70+
const std::array<GlobPtrSz<T>, 2> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1])};
7171
gridMerge(d_src,
7272
globPtr<typename MakeVec<T, 2>::type>(dst),
7373
stream);
@@ -78,7 +78,7 @@ namespace
7878
{
7979
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
8080
{
81-
const std::vector<GlobPtrSz<T>> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2])};
81+
const std::array<GlobPtrSz<T>, 3> d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2])};
8282
gridMerge(d_src,
8383
globPtr<typename MakeVec<T, 3>::type>(dst),
8484
stream);
@@ -89,7 +89,7 @@ namespace
8989
{
9090
static void call(const GpuMat* src, GpuMat& dst, Stream& stream)
9191
{
92-
const std::vector<GlobPtrSz<T> > d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2]), globPtr<T>(src[3])};
92+
const std::array<GlobPtrSz<T>, 4 > d_src = {globPtr<T>(src[0]), globPtr<T>(src[1]), globPtr<T>(src[2]), globPtr<T>(src[3])};
9393
gridMerge(d_src,
9494
globPtr<typename MakeVec<T, 4>::type>(dst),
9595
stream);

modules/cudev/include/opencv2/cudev/grid/detail/split_merge.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ namespace grid_split_merge_detail
162162
mergeC2<Policy>(get<0>(src), get<1>(src), dst, mask, rows, cols, stream);
163163
}
164164

165-
template <typename VecType, typename DstType, class MaskPtr>
166-
__host__ static void mergeVector(const std::vector<VecType>& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
165+
template <class SrcPtrArray, typename DstType, class MaskPtr>
166+
__host__ static void mergeArray(const SrcPtrArray& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
167167
{
168168
mergeC2<Policy>(src[0], src[1], dst, mask, rows, cols, stream);
169169
}
@@ -178,8 +178,8 @@ namespace grid_split_merge_detail
178178
mergeC3<Policy>(get<0>(src), get<1>(src), get<2>(src), dst, mask, rows, cols, stream);
179179
}
180180

181-
template <typename VecType, typename DstType, class MaskPtr>
182-
__host__ static void mergeVector(const std::vector<VecType>& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
181+
template <class SrcPtrArray, typename DstType, class MaskPtr>
182+
__host__ static void mergeArray(const SrcPtrArray& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
183183
{
184184
mergeC3<Policy>(src[0], src[1], src[2], dst, mask, rows, cols, stream);
185185
}
@@ -193,8 +193,8 @@ namespace grid_split_merge_detail
193193
mergeC4<Policy>(get<0>(src), get<1>(src), get<2>(src), get<3>(src), dst, mask, rows, cols, stream);
194194
}
195195

196-
template <typename VecType, typename DstType, class MaskPtr>
197-
__host__ static void mergeVector(const std::vector<VecType>& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
196+
template <class SrcPtrArray, typename DstType, class MaskPtr>
197+
__host__ static void mergeArray(const SrcPtrArray& src, const GlobPtr<DstType>& dst, const MaskPtr& mask, int rows, int cols, cudaStream_t stream)
198198
{
199199
mergeC4<Policy>(src[0], src[1], src[2], src[3], dst, mask, rows, cols, stream);
200200
}

modules/cudev/include/opencv2/cudev/grid/split_merge.hpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ __host__ void gridMerge_(const SrcPtrTuple& src, const GlobPtrSz<DstType>& dst,
131131
StreamAccessor::getStream(stream));
132132
}
133133

134-
template <class Policy, typename VecType, typename DstType, class MaskPtr>
135-
__host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
134+
template <class Policy, class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
135+
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
136136
{
137137
CV_Assert( VecTraits<DstType>::cn == src.size() );
138138

@@ -143,15 +143,15 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>
143143

144144
dst.create(rows, cols);
145145

146-
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
146+
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
147147
shrinkPtr(dst),
148148
shrinkPtr(mask),
149149
rows, cols,
150150
StreamAccessor::getStream(stream));
151151
}
152152

153-
template <class Policy, typename VecType, typename DstType, class MaskPtr>
154-
__host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
153+
template <class Policy, class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
154+
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
155155
{
156156
CV_Assert( VecTraits<DstType>::cn == src.size() );
157157

@@ -161,15 +161,15 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<
161161
CV_Assert( getRows(dst) == rows && getCols(dst) == cols );
162162
CV_Assert( getRows(mask) == rows && getCols(mask) == cols );
163163

164-
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
164+
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
165165
shrinkPtr(dst),
166166
shrinkPtr(mask),
167167
rows, cols,
168168
StreamAccessor::getStream(stream));
169169
}
170170

171-
template <class Policy, typename VecType, typename DstType>
172-
__host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
171+
template <class Policy, class ArrayType, size_t ArraySize, typename DstType>
172+
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
173173
{
174174
CV_Assert( VecTraits<DstType>::cn == src.size() );
175175

@@ -178,15 +178,15 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, GpuMat_<DstType>
178178

179179
dst.create(rows, cols);
180180

181-
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
181+
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
182182
shrinkPtr(dst),
183183
WithOutMask(),
184184
rows, cols,
185185
StreamAccessor::getStream(stream));
186186
}
187187

188-
template <class Policy, typename VecType, typename DstType>
189-
__host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
188+
template <class Policy, class ArrayType, size_t ArraySize, typename DstType>
189+
__host__ void gridMergeArray_(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
190190
{
191191
CV_Assert( VecTraits<DstType>::cn == src.size() );
192192

@@ -195,7 +195,7 @@ __host__ void gridMergeVector_(const std::vector<VecType>& src, const GlobPtrSz<
195195

196196
CV_Assert( getRows(dst) == rows && getCols(dst) == cols );
197197

198-
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeVector(src,
198+
grid_split_merge_detail::MergeImpl<VecTraits<DstType>::cn, Policy>::mergeArray(src,
199199
shrinkPtr(dst),
200200
WithOutMask(),
201201
rows, cols,
@@ -596,28 +596,28 @@ __host__ void gridMerge(const SrcPtrTuple& src, const GlobPtrSz<DstType>& dst, S
596596
gridMerge_<DefaultSplitMergePolicy>(src, dst, stream);
597597
}
598598

599-
template <typename VecType, typename DstType, class MaskPtr>
600-
__host__ void gridMergeVector(const std::vector<VecType>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
599+
template <class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
600+
__host__ void gridMergeArray(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
601601
{
602-
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, mask, stream);
602+
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, mask, stream);
603603
}
604604

605-
template <typename VecType, typename DstType, class MaskPtr>
606-
__host__ void gridMerge(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
605+
template <class ArrayType, size_t ArraySize, typename DstType, class MaskPtr>
606+
__host__ void gridMerge(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, const MaskPtr& mask, Stream& stream = Stream::Null())
607607
{
608-
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, mask, stream);
608+
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, mask, stream);
609609
}
610610

611-
template <typename VecType, typename DstType>
612-
__host__ void gridMerge(const std::vector<VecType>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
611+
template <class ArrayType, size_t ArraySize, typename DstType>
612+
__host__ void gridMerge(const std::array<ArrayType, ArraySize>& src, GpuMat_<DstType>& dst, Stream& stream = Stream::Null())
613613
{
614-
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, stream);
614+
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, stream);
615615
}
616616

617-
template <typename VecType, typename DstType>
618-
__host__ void gridMerge(const std::vector<VecType>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
617+
template <class ArrayType, size_t ArraySize, typename DstType>
618+
__host__ void gridMerge(const std::array<ArrayType, ArraySize>& src, const GlobPtrSz<DstType>& dst, Stream& stream = Stream::Null())
619619
{
620-
gridMergeVector_<DefaultSplitMergePolicy>(src, dst, stream);
620+
gridMergeArray_<DefaultSplitMergePolicy>(src, dst, stream);
621621
}
622622

623623
template <class SrcPtr, typename DstType, class MaskPtr>

modules/cudev/test/test_split_merge.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public:
7070
GpuMat_<T> d_src2(src2);
7171

7272
GpuMat_<typename MakeVec<T, 2>::type> dst;
73-
std::vector<GlobPtrSz<T> > d_src = {globPtr(d_src1), globPtr(d_src2)};
73+
std::array<GlobPtrSz<T>, 2 > d_src = {globPtr(d_src1), globPtr(d_src2)};
7474
gridMerge(d_src, dst);
7575

7676
Mat dst_gold;
@@ -94,7 +94,7 @@ public:
9494
GpuMat_<T> d_src2(src2);
9595
GpuMat_<T> d_src3(src3);
9696

97-
std::vector<GlobPtrSz<T> > d_src = {globPtr(d_src1), globPtr(d_src2), globPtr(d_src3)};
97+
std::array<GlobPtrSz<T>, 3 > d_src = {globPtr(d_src1), globPtr(d_src2), globPtr(d_src3)};
9898

9999
GpuMat_<typename MakeVec<T, 3>::type> dst;
100100
gridMerge(d_src, dst);

0 commit comments

Comments
 (0)