-
-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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 pcl::make_shared
that automatically handles aligned allocations
#3163
Add pcl::make_shared
that automatically handles aligned allocations
#3163
Conversation
Hi, sorry for not joining the discussion earlier. Generally, 👍 for doing this. It's not a part of this PR and is left for discussion in future, but I can imagine eventually arriving at code like this, which I believe looks nice: auto cloud_in = pcl::make_shared_cloud<pcl::PointXYZ>(); But back to the PR. Would it be too much if I ask you to regroup commits such that there are only two left:
As it is now the find/replace produces a lot of noise and it's hard to concentrate on the meaningful changes. Regarding |
a053c55
to
09f63cf
Compare
Thanks. First, I wanted to propose a shorter implementation of template<typename...> using void_t = void; // part of std in c++17
template <typename, typename = void_t<>> struct has_custom_allocator : std::false_type { };
template <typename T> struct has_custom_allocator<T, void_t<typename T::custom_allocator>> : std::true_type { }; What's your opinion? Second, I think we should add a simple unit test. I have thought about this, but don't have any good ideas how to verify that Third, we'll need to add documentation to both |
If this mechanism is going to be available in C++17 then it is definitely best if we mimic it. Did you test if this particular implementation works? I'm asking because from quickly looking at it I'm seeing a potential ambiguous call error (which was what forced to adopt the other "sink function" mechanism) in case you pass to it a type to it which has a template <typename, typename = void_t<>> struct has_custom_allocator : std::false_type { };
template <typename T> struct has_custom_allocator<T, void_t<typename T::custom_allocator>> : std::true_type { };
Agreed. Supposedly, when using make shared, the control block and actual object being held are allocated in a single call. Ultimately, we only care if the held object is allocated on a multiple of X (16?) bits address. There's also the challenge of know what exactly is X. I believe this is configurable and platform dependent. We probably should look into Eigen's own unit tests for some guidance. |
This answer explains it thoroughly, but basically if Foo::custom_allocator(_type?) is well-formed there's no Substitution Failure and the specialized non-primary definition gets chosen unequivocally. |
I was convinced I had tried something along these lines before, but I clearly failed some of the underlying constructs. Nevertheless, this new proposal is substantially more elegant than mine and hopefully, it will allows us to perform a quick find replace to remove the custom version once we adopt C++17, so I'm completely in favor. Edit: @aPonza - rebase on top of master instead of merging master to your own branch. You will still need to go through the same merge conflicts, but you won't have the extra merge commits at the end. |
Not sure why it shows the same merge commit twice, but I'll be sure to rebase next time... Anyways, I don't understand what I'm missing: why is this not working?
which outputs
My knowledge in unit/integration testing is nil, I tried to look up examples in eigen like you suggested but couldn't yet find the relevant ones. I have found the gcc tests though, I suppose the difference between allocate_shared and make_shared is what you're after. |
I'd avoid getting deep into this. Eigen's own tests of aligned allocators are complex and hard to understand. I think we should just assume they work. On our side, we only need to check whether our dispatching between aligned and normal |
There should exists no error in this situation. You're simply allocating a non-aligned Foo object. On its own this won't cause any errors. Now if you have a fixed size Eigen attribute inside e.g.,
|
No clue what I did wrong, after I saw the mess I ended up doing it the other way, I'll read up on it. Regardless, I don't understand if this is what you were expecting for the check, it seems simple, if anything. I couldn't understand how to use googlemock's EDIT: took a while but I fixed it. |
4542146
to
7d9a7db
Compare
Both failures are from #3194 as far as I understand. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. Besides from a single comment that I left this PR looks complete in terms of code. Before we merge it would be nice to add better docstrings though. Since we are dealing with macros and "overloaded" template functions, we'd need to double-check empirically that Doxygen generates appropriate HTML. I'll try to have a look at this tomorrow and get back with details on how to format docstrings.
I figured out how add documentation to #ifdef DOXYGEN_ONLY
/// Documentation
template<typename T, typename ... Args>
boost::shared_ptr<T> make_shared(Args&&... args);
#else
template<typename T, typename ... Args>
std::enable_if_t<has_custom_allocator<T>::value, boost::shared_ptr<T>> make_shared(Args&&... args)
{
return boost::allocate_shared<T>(Eigen::aligned_allocator<T>(), std::forward<Args> (args)...);
}
template<typename T, typename ... Args>
std::enable_if_t<!has_custom_allocator<T>::value, boost::shared_ptr<T>> make_shared(Args&&... args)
{
return boost::make_shared<T>(std::forward<Args> (args)...);
}
#endif To make this work we need to add Now as for the macro documentation, I'm struggling to make it appear in HTML. If someone has experience with this, please let me know. |
Thanks. Apart from the question with the name, LGTM. @SergioRAgostinho do you have suggestions? |
By the way, #3194 was solved, please rebase so that we can see Windows status as well. |
edd83ea
to
32cbdc2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I just have minor comments.
@@ -27,7 +27,7 @@ | |||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |||
* BUT: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keyboard gone wild?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The colon is still here. You mark this as resolved. Was it intentional or is this a bug of my browser cache?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, it's still there.
25238c6
to
a4c809b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's regroup commits before merging. I propose to have three:
- Add
make_shared
and trait - Add unit test
- Replace
EIGEN_MAKE...
withPCL_MAKE...
1 and 2 may be joined if you prefer.
a4c809b
to
4cb7de3
Compare
4cb7de3
to
e485ca0
Compare
e485ca0
to
fde22eb
Compare
@@ -27,7 +27,7 @@ | |||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |||
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |||
* BUT: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The colon is still here. You mark this as resolved. Was it intentional or is this a bug of my browser cache?
fde22eb
to
bbbbd77
Compare
pcl::make_shared
that automatically handles aligned allocations
Cherry-picked the relevant commits from #3146 to only add the function.
I would argue that this PR (plus the automated clang-tidy changes from the previous PR) eases the job of transitioning into C++14 and from boost::shared_ptr to std::shared_ptr in particular.
There is also left to discuss this comment from Sergio here (apart from the whole "do you want this").