Change way to move into vector to faster one

Regarding to BenchMark
https://quick-bench.com/q/RVoDNywC4ybLpW8KkHLDj5WDIMg
This commit is contained in:
Filip Sykala 2022-03-25 08:49:43 +01:00
parent 0487113f8b
commit bdb8c3729f
3 changed files with 36 additions and 31 deletions

View File

@ -246,8 +246,9 @@ inline Polygons to_polygons(ExPolygon &&src)
Polygons polygons; Polygons polygons;
polygons.reserve(src.holes.size() + 1); polygons.reserve(src.holes.size() + 1);
polygons.push_back(std::move(src.contour)); polygons.push_back(std::move(src.contour));
std::move(std::begin(src.holes), std::end(src.holes), std::back_inserter(polygons)); polygons.insert(polygons.end(),
src.holes.clear(); std::make_move_iterator(src.holes.begin()),
std::make_move_iterator(src.holes.end()));
return polygons; return polygons;
} }
@ -255,10 +256,11 @@ inline Polygons to_polygons(ExPolygons &&src)
{ {
Polygons polygons; Polygons polygons;
polygons.reserve(number_polygons(src)); polygons.reserve(number_polygons(src));
for (ExPolygons::iterator it = src.begin(); it != src.end(); ++it) { for (ExPolygon& expoly: src) {
polygons.push_back(std::move(it->contour)); polygons.push_back(std::move(expoly.contour));
std::move(std::begin(it->holes), std::end(it->holes), std::back_inserter(polygons)); polygons.insert(polygons.end(),
it->holes.clear(); std::make_move_iterator(expoly.holes.begin()),
std::make_move_iterator(expoly.holes.end()));
} }
return polygons; return polygons;
} }
@ -301,17 +303,19 @@ inline void polygons_append(Polygons &dst, ExPolygon &&src)
{ {
dst.reserve(dst.size() + src.holes.size() + 1); dst.reserve(dst.size() + src.holes.size() + 1);
dst.push_back(std::move(src.contour)); dst.push_back(std::move(src.contour));
std::move(std::begin(src.holes), std::end(src.holes), std::back_inserter(dst)); dst.insert(dst.end(),
src.holes.clear(); std::make_move_iterator(src.holes.begin()),
std::make_move_iterator(src.holes.end()));
} }
inline void polygons_append(Polygons &dst, ExPolygons &&src) inline void polygons_append(Polygons &dst, ExPolygons &&src)
{ {
dst.reserve(dst.size() + number_polygons(src)); dst.reserve(dst.size() + number_polygons(src));
for (ExPolygons::iterator it = src.begin(); it != src.end(); ++ it) { for (ExPolygon& expoly: src) {
dst.push_back(std::move(it->contour)); dst.push_back(std::move(expoly.contour));
std::move(std::begin(it->holes), std::end(it->holes), std::back_inserter(dst)); dst.insert(dst.end(),
it->holes.clear(); std::make_move_iterator(expoly.holes.begin()),
std::make_move_iterator(expoly.holes.end()));
} }
} }
@ -325,8 +329,9 @@ inline void expolygons_append(ExPolygons &dst, ExPolygons &&src)
if (dst.empty()) { if (dst.empty()) {
dst = std::move(src); dst = std::move(src);
} else { } else {
std::move(std::begin(src), std::end(src), std::back_inserter(dst)); dst.insert(dst.end(),
src.clear(); std::make_move_iterator(src.begin()),
std::make_move_iterator(src.end()));
} }
} }

View File

@ -65,8 +65,9 @@ public:
if (entities.empty()) if (entities.empty())
entities = std::move(src); entities = std::move(src);
else { else {
std::move(std::begin(src), std::end(src), std::back_inserter(entities)); entities.insert(entities.end(),
src.clear(); std::make_move_iterator(src.begin()),
std::make_move_iterator(src.end()));
} }
} }
void append(const ExtrusionPaths &paths) { void append(const ExtrusionPaths &paths) {

View File

@ -93,9 +93,10 @@ template <typename T>
inline void append(std::vector<T>& dest, const std::vector<T>& src) inline void append(std::vector<T>& dest, const std::vector<T>& src)
{ {
if (dest.empty()) if (dest.empty())
dest = src; dest = src; // copy
else else
dest.insert(dest.end(), src.begin(), src.end()); dest.insert(dest.end(), src.begin(), src.end());
// NOTE: insert reserve space when needed
} }
template <typename T> template <typename T>
@ -104,11 +105,10 @@ inline void append(std::vector<T>& dest, std::vector<T>&& src)
if (dest.empty()) if (dest.empty())
dest = std::move(src); dest = std::move(src);
else { else {
dest.reserve(dest.size() + src.size()); dest.insert(dest.end(),
std::move(std::begin(src), std::end(src), std::back_inserter(dest)); std::make_move_iterator(src.begin()),
std::make_move_iterator(src.end()));
} }
src.clear();
src.shrink_to_fit();
} }
// Append the source in reverse. // Append the source in reverse.
@ -116,7 +116,7 @@ template <typename T>
inline void append_reversed(std::vector<T>& dest, const std::vector<T>& src) inline void append_reversed(std::vector<T>& dest, const std::vector<T>& src)
{ {
if (dest.empty()) if (dest.empty())
dest = src; dst = {src.rbegin(), src.rend()};
else else
dest.insert(dest.end(), src.rbegin(), src.rend()); dest.insert(dest.end(), src.rbegin(), src.rend());
} }
@ -126,13 +126,12 @@ template <typename T>
inline void append_reversed(std::vector<T>& dest, std::vector<T>&& src) inline void append_reversed(std::vector<T>& dest, std::vector<T>&& src)
{ {
if (dest.empty()) if (dest.empty())
dest = std::move(src); dest = {std::make_move_iterator(src.rbegin),
else { std::make_move_iterator(src.rend)};
dest.reserve(dest.size() + src.size()); else
std::move(std::rbegin(src), std::rend(src), std::back_inserter(dest)); dest.insert(dest.end(),
} std::make_move_iterator(src.rbegin()),
src.clear(); std::make_move_iterator(src.rend()));
src.shrink_to_fit();
} }
// Casting an std::vector<> from one type to another type without warnings about a loss of accuracy. // Casting an std::vector<> from one type to another type without warnings about a loss of accuracy.