Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std::vector: Using emplace_back to replace push_back to improve performance #939

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions CPP/Clipper2Lib/include/clipper2/clipper.core.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,10 @@ namespace Clipper2Lib
{
Path<T> result;
result.reserve(4);
result.push_back(Point<T>(left, top));
result.push_back(Point<T>(right, top));
result.push_back(Point<T>(right, bottom));
result.push_back(Point<T>(left, bottom));
result.emplace_back(left, top);
result.emplace_back(right, top);
result.emplace_back(right, bottom);
result.emplace_back(left, bottom);
return result;
}

Expand Down Expand Up @@ -618,13 +618,13 @@ namespace Clipper2Lib
result.reserve(path.size());
typename Path<T>::const_iterator path_iter = path.cbegin();
Point<T> first_pt = *path_iter++, last_pt = first_pt;
result.push_back(first_pt);
result.emplace_back(first_pt);
for (; path_iter != path.cend(); ++path_iter)
{
if (!NearEqual(*path_iter, last_pt, max_dist_sqrd))
{
last_pt = *path_iter;
result.push_back(last_pt);
result.emplace_back(last_pt);
}
}
if (!is_closed_path) return result;
Expand All @@ -642,7 +642,7 @@ namespace Clipper2Lib
for (typename Paths<T>::const_iterator paths_citer = paths.cbegin();
paths_citer != paths.cend(); ++paths_citer)
{
result.push_back(StripNearEqual(*paths_citer, max_dist_sqrd, is_closed_path));
result.emplace_back(std::move(StripNearEqual(*paths_citer, max_dist_sqrd, is_closed_path)));
}
return result;
}
Expand Down
20 changes: 10 additions & 10 deletions CPP/Clipper2Lib/include/clipper2/clipper.export.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ static Path<T> ConvertCPathToPathT(T* path)
T x = *v++, y = *v++;
#ifdef USINGZ
z_type z = Reinterpret<z_type>(*v++);
result.push_back(Point<T>(x, y, z));
result.emplace_back(x, y, z);
#else
result.push_back(Point<T>(x, y));
result.emplace_back(x, y);
#endif
}
return result;
Expand All @@ -419,12 +419,12 @@ static Paths<T> ConvertCPathsToPathsT(T* paths)
T x = *v++, y = *v++;
#ifdef USINGZ
z_type z = Reinterpret<z_type>(*v++);
path.push_back(Point<T>(x, y, z));
path.emplace_back(x, y, z);
#else
path.push_back(Point<T>(x, y));
path.emplace_back(x, y);
#endif
}
result.push_back(path);
result.emplace_back(std::move(path));
}
return result;
}
Expand All @@ -443,9 +443,9 @@ static Path64 ConvertCPathDToPath64WithScale(const CPathD path, double scale)
double y = *v++ * scale;
#ifdef USINGZ
z_type z = Reinterpret<z_type>(*v++);
result.push_back(Point64(x, y, z));
result.emplace_back(x, y, z);
#else
result.push_back(Point64(x, y));
result.emplace_back(x, y);
#endif
}
return result;
Expand All @@ -471,12 +471,12 @@ static Paths64 ConvertCPathsDToPaths64(const CPathsD paths, double scale)
double y = *v++ * scale;
#ifdef USINGZ
z_type z = Reinterpret<z_type>(*v++);
path.push_back(Point64(x, y, z));
path.emplace_back(x, y, z);
#else
path.push_back(Point64(x, y));
path.emplace_back(x, y);
#endif
}
result.push_back(path);
result.emplace_back(std::move(path));
}
return result;
}
Expand Down
26 changes: 13 additions & 13 deletions CPP/Clipper2Lib/include/clipper2/clipper.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,14 @@ namespace Clipper2Lib {

inline void PolyPathToPaths64(const PolyPath64& polypath, Paths64& paths)
{
paths.push_back(polypath.Polygon());
paths.emplace_back(polypath.Polygon());
for (const auto& child : polypath)
PolyPathToPaths64(*child, paths);
}

inline void PolyPathToPathsD(const PolyPathD& polypath, PathsD& paths)
{
paths.push_back(polypath.Polygon());
paths.emplace_back(polypath.Polygon());
for (const auto& child : polypath)
PolyPathToPathsD(*child, paths);
}
Expand Down Expand Up @@ -348,9 +348,9 @@ namespace Clipper2Lib {
result.reserve(array_size / 2);
for (size_t i = 0; i < array_size; i +=2)
#ifdef USINGZ
result.push_back( U{ an_array[i], an_array[i + 1], 0} );
result.emplace_back( an_array[i], an_array[i + 1], 0 );
#else
result.push_back( U{ an_array[i], an_array[i + 1]} );
result.emplace_back( an_array[i], an_array[i + 1] );
#endif
}

Expand Down Expand Up @@ -518,20 +518,20 @@ namespace Clipper2Lib {
}

prevIt = srcIt++;
dst.push_back(*prevIt);
dst.emplace_back(*prevIt);
for (; srcIt != stop; ++srcIt)
{
if (!IsCollinear(*prevIt, *srcIt, *(srcIt + 1)))
{
prevIt = srcIt;
dst.push_back(*prevIt);
dst.emplace_back(*prevIt);
}
}

if (is_open_path)
dst.push_back(*srcIt);
dst.emplace_back(*srcIt);
else if (!IsCollinear(*prevIt, *stop, dst[0]))
dst.push_back(*stop);
dst.emplace_back(*stop);
else
{
while (dst.size() > 2 &&
Expand Down Expand Up @@ -603,10 +603,10 @@ namespace Clipper2Lib {
double dx = co, dy = si;
Path<T> result;
result.reserve(steps);
result.push_back(Point<T>(center.x + radiusX, static_cast<double>(center.y)));
result.emplace_back(center.x + radiusX, static_cast<double>(center.y));
for (size_t i = 1; i < steps; ++i)
{
result.push_back(Point<T>(center.x + radiusX * dx, center.y + radiusY * dy));
result.emplace_back(center.x + radiusX * dx, center.y + radiusY * dy);
double x = dx * co - dy * si;
dy = dy * co + dx * si;
dx = x;
Expand Down Expand Up @@ -700,7 +700,7 @@ namespace Clipper2Lib {
Path<T> result;
result.reserve(len);
for (typename Path<T>::size_type i = 0; i < len; ++i)
if (!flags[i]) result.push_back(path[i]);
if (!flags[i]) result.emplace_back(path[i]);
return result;
}

Expand All @@ -711,7 +711,7 @@ namespace Clipper2Lib {
Paths<T> result;
result.reserve(paths.size());
for (const auto& path : paths)
result.push_back(SimplifyPath(path, epsilon, isClosedPath));
result.emplace_back(std::move(SimplifyPath(path, epsilon, isClosedPath)));
return result;
}

Expand Down Expand Up @@ -749,7 +749,7 @@ namespace Clipper2Lib {
result.reserve(len);
for (typename Path<T>::size_type i = 0; i < len; ++i)
if (flags[i])
result.push_back(path[i]);
result.emplace_back(path[i]);
return result;
}

Expand Down
14 changes: 7 additions & 7 deletions CPP/Clipper2Lib/include/clipper2/clipper.minkowski.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Clipper2Lib
Path64 path2(pattern.size());
std::transform(pattern.cbegin(), pattern.cend(),
path2.begin(), [p](const Point64& pt2) {return p + pt2; });
tmp.push_back(path2);
tmp.emplace_back(std::move(path2));
}
}
else
Expand All @@ -45,7 +45,7 @@ namespace Clipper2Lib
Path64 path2(pattern.size());
std::transform(pattern.cbegin(), pattern.cend(),
path2.begin(), [p](const Point64& pt2) {return p - pt2; });
tmp.push_back(path2);
tmp.emplace_back(std::move(path2));
}
}

Expand All @@ -59,14 +59,14 @@ namespace Clipper2Lib
Path64 quad;
quad.reserve(4);
{
quad.push_back(tmp[g][h]);
quad.push_back(tmp[i][h]);
quad.push_back(tmp[i][j]);
quad.push_back(tmp[g][j]);
quad.emplace_back(tmp[g][h]);
quad.emplace_back(tmp[i][h]);
quad.emplace_back(tmp[i][j]);
quad.emplace_back(tmp[g][j]);
};
if (!IsPositive(quad))
std::reverse(quad.begin(), quad.end());
result.push_back(quad);
result.emplace_back(std::move(quad));
h = j;
}
g = i;
Expand Down
Loading
Loading