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.reserve(src.holes.size() + 1);
polygons.push_back(std::move(src.contour));
std::move(std::begin(src.holes), std::end(src.holes), std::back_inserter(polygons));
src.holes.clear();
polygons.insert(polygons.end(),
std::make_move_iterator(src.holes.begin()),
std::make_move_iterator(src.holes.end()));
return polygons;
}
@ -255,10 +256,11 @@ inline Polygons to_polygons(ExPolygons &&src)
{
Polygons polygons;
polygons.reserve(number_polygons(src));
for (ExPolygons::iterator it = src.begin(); it != src.end(); ++it) {
polygons.push_back(std::move(it->contour));
std::move(std::begin(it->holes), std::end(it->holes), std::back_inserter(polygons));
it->holes.clear();
for (ExPolygon& expoly: src) {
polygons.push_back(std::move(expoly.contour));
polygons.insert(polygons.end(),
std::make_move_iterator(expoly.holes.begin()),
std::make_move_iterator(expoly.holes.end()));
}
return polygons;
}
@ -300,18 +302,20 @@ inline void polygons_append(Polygons &dst, const ExPolygons &src)
inline void polygons_append(Polygons &dst, ExPolygon &&src)
{
dst.reserve(dst.size() + src.holes.size() + 1);
dst.push_back(std::move(src.contour));
std::move(std::begin(src.holes), std::end(src.holes), std::back_inserter(dst));
src.holes.clear();
dst.push_back(std::move(src.contour));
dst.insert(dst.end(),
std::make_move_iterator(src.holes.begin()),
std::make_move_iterator(src.holes.end()));
}
inline void polygons_append(Polygons &dst, ExPolygons &&src)
{
dst.reserve(dst.size() + number_polygons(src));
for (ExPolygons::iterator it = src.begin(); it != src.end(); ++ it) {
dst.push_back(std::move(it->contour));
std::move(std::begin(it->holes), std::end(it->holes), std::back_inserter(dst));
it->holes.clear();
for (ExPolygon& expoly: src) {
dst.push_back(std::move(expoly.contour));
dst.insert(dst.end(),
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()) {
dst = std::move(src);
} else {
std::move(std::begin(src), std::end(src), std::back_inserter(dst));
src.clear();
dst.insert(dst.end(),
std::make_move_iterator(src.begin()),
std::make_move_iterator(src.end()));
}
}

View File

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