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

Add some perfect forwarding in the Kernel #7373

Merged
merged 34 commits into from
May 15, 2024

Conversation

sloriot
Copy link
Member

@sloriot sloriot commented Apr 7, 2023

No description provided.

Copy link
Member

@mglisse mglisse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to replace the copy in Cartesian_converter with a move, that's good. Getting rid of the move should be possible (instead of directly calling the converter on a double, use an object that has a conversion operator to the exact FT and lazily calls the converter in this operator), but probably not worth it.

Cartesian_kernel/include/CGAL/Cartesian/Vector_3.h Outdated Show resolved Hide resolved
Cartesian_kernel/include/CGAL/Cartesian/Vector_3.h Outdated Show resolved Hide resolved
Cartesian_kernel/include/CGAL/Cartesian/Vector_3.h Outdated Show resolved Hide resolved
Kernel_23/include/CGAL/Point_3.h Outdated Show resolved Hide resolved
@sloriot sloriot added Batch_1 First Batch of PRs under testing Batch_2 Second Batch of PRs under testing and removed Batch_1 First Batch of PRs under testing labels Apr 10, 2023
@sloriot sloriot marked this pull request as ready for review April 12, 2023 16:00
@sloriot sloriot added this to the 5.6-beta milestone Apr 12, 2023
@sloriot sloriot removed the Batch_2 Second Batch of PRs under testing label Apr 14, 2023
@sloriot sloriot added Batch_2 Second Batch of PRs under testing Batch_1 First Batch of PRs under testing and removed Batch_2 Second Batch of PRs under testing labels Apr 28, 2023
@sloriot
Copy link
Member Author

sloriot commented May 4, 2023

Successfully tested in CGAL-5.6-Ic-241. 2D version can be added too now.

@sloriot sloriot added TODO Tested and removed Batch_1 First Batch of PRs under testing labels May 4, 2023
@mglisse
Copy link
Member

mglisse commented May 5, 2023

This seems to replace the copy in Cartesian_converter with a move, that's good. Getting rid of the move should be possible (instead of directly calling the converter on a double, use an object that has a conversion operator to the exact FT and lazily calls the converter in this operator), but probably not worth it.

Just a proof of concept hack

--- a/Cartesian_kernel/include/CGAL/Cartesian_converter.h
+++ b/Cartesian_kernel/include/CGAL/Cartesian_converter.h
@@ -83,6 +83,15 @@ struct Converting_visitor : boost::static_visitor<> {
 
 } // namespace internal
 
+template<class T2, class C, class T1> struct Marc {
+  CGAL_NO_UNIQUE_ADDRESS C c;
+  T1 const* in;
+  operator T2() const { return c(*in); }
+};
+template<class T2, class C, class T1> Marc<T2,C,T1> marc(C const&c, T1 const&x) {
+  return { c, &x };
+}
+
 template < class K1, class K2,
            class Converter /*= typename internal::Default_converter<K1, K2>::Type*/>
 class Cartesian_converter : public Enum_converter
@@ -292,7 +301,8 @@ public:
     operator()(const typename K1::Point_3 &a) const
     {
         typedef typename K2::Point_3 Point_3;
-        return Point_3(c(a.x()), c(a.y()), c(a.z()));
+        typedef typename K2::FT FT;
+        return Point_3(marc<FT>(c, a.x()), marc<FT>(c, a.y()), marc<FT>(c, a.z()));
     }
 
     typename K2::Weighted_point_3

When using Cartesian_converter on Point_3 from double to mpq_class, this removes some allocations. I expect it should also help with Gmpq, but not with boost types (I think they don't allocate on move).

(NewKernel_d uses a transform_iterator on the coordinates, for a similar effect)

@lrineau
Copy link
Member

lrineau commented Apr 23, 2024

Several red lines in CGAL-6.0-Ic-227

/mnt/testsuite/include/CGAL/array.h:66:13: error: non-constant-expression cannot be narrowed from type 'int' to 'double' in initializer list [-Wc++11-narrowing]
   66 |   return {{ std::forward<Args>(args)... }};
      |             ^~~~~~~~~~~~~~~~~~~~~~~~

😢

@mglisse
Copy link
Member

mglisse commented Apr 23, 2024

/mnt/testsuite/include/CGAL/array.h:66:13: error: non-constant-expression cannot be narrowed from type 'int' to 'double' in initializer list [-Wc++11-narrowing]
   66 |   return {{ std::forward<Args>(args)... }};
      |             ^~~~~~~~~~~~~~~~~~~~~~~~

#7373 (comment)

@lrineau
Copy link
Member

lrineau commented Apr 23, 2024

@mglisse Can you please review ca1f11d and 940a7eb?

@afabri
Copy link
Member

afabri commented Apr 25, 2024

Compilation errors in CGAL-6.0-Ic-229

@lrineau
Copy link
Member

lrineau commented Apr 29, 2024

@sloriot This PR can be tested again, after 15349f0. It should compile now with MSVC 2017 (but with that compiler the move/forwarding might be not optimal).

@afabri
Copy link
Member

afabri commented Apr 30, 2024

Compilation error 6.0-Ic-232 VC2017

@sloriot sloriot added the Batch_1 First Batch of PRs under testing label Apr 30, 2024
@sloriot
Copy link
Member Author

sloriot commented May 2, 2024

The error is gone in CGAL-6.0-Ic-233

@lrineau
Copy link
Member

lrineau commented May 2, 2024

The error is gone in CGAL-6.0-Ic-233

Does it mean "fully tested"/"merge-able", or something else?

@sloriot sloriot removed the Batch_1 First Batch of PRs under testing label May 2, 2024
@sloriot
Copy link
Member Author

sloriot commented May 2, 2024

It means that MSVC 2017 no longer crashes. It will be tested again tonight.

@sloriot
Copy link
Member Author

sloriot commented May 6, 2024

Successfully tested in CGAL-6.0-Ic-235

Comment on lines 52 to +56
VectorC2(const FT &x, const FT &y)
: base(CGAL::make_array(x, y)) {}
: base{x, y} {}

VectorC2(FT&& x, FT&& y)
: base{std::move(x), std::move(y)} {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIUC we use 2 overloads instead of perfect forwarding + make_array because make_array doesn't work well enough?

@lrineau lrineau added the rm only: ready for master For the release team only: that indicates that a PR is about to be merged in 'master' label May 13, 2024
@lrineau lrineau merged commit e9fb019 into CGAL:master May 15, 2024
9 checks passed
@lrineau lrineau removed the rm only: ready for master For the release team only: that indicates that a PR is about to be merged in 'master' label May 16, 2024
@lrineau lrineau deleted the Kernel-Perfect_forwarding branch May 16, 2024 10:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants