Skip to content

Commit 7b813c5

Browse files
committed
Use std::transform with pre-allocated buffer in transform_tensor
fplus::transform_convert uses reserve() + back_insert_iterator, which prevents auto-vectorization because push_back updates the vector's internal end pointer on every write. Using resize() and writing via a plain random-access iterator allows the compiler to emit SIMD instructions, giving ~5% faster inference on VGG19.
1 parent 1a404d1 commit 7b813c5

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

include/fdeep/tensor.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ namespace internal {
190190
template <typename F>
191191
tensor transform_tensor(F f, const tensor& m)
192192
{
193-
return tensor(m.shape(), fplus::transform_convert<float_vec>(f, *m.as_vector()));
193+
const auto& src = *m.as_vector();
194+
float_vec result(src.size());
195+
std::transform(src.begin(), src.end(), result.begin(), f);
196+
return tensor(m.shape(), std::move(result));
194197
}
195198

196199
inline std::vector<tensor> tensor_to_depth_slices(const tensor& m)

0 commit comments

Comments
 (0)