-
Notifications
You must be signed in to change notification settings - Fork 421
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
Explicit template declaration and instantiation for scalar type #165
Conversation
…pattern # Conflicts: # CMakeModules/FCLMacros.cmake # include/fcl/math/motion/tbv_motion_bound_visitor.h
AppleClang 6.0 has a bug that it does not generate symbols for explicitly instantiated constexpr member function of template class (ref: http://stackoverflow.com/questions/27425943/undefined-symbols-with-extern-templates-in-a-static-library?noredirect=1#comment43293703_27425943), which is reported as a bug (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64293).
# Conflicts: # include/fcl/broadphase/broadphase_SaP.h
I'm not sure if I misunderstand something here. |
Like @chhtz, I am also confused by this. |
You are correct. It's technically the same with having the implementation directly in the Edit: For clarification, these are the main changes of this pull request:
|
Ok, as it is, it does add to code readability and I did not intend to suggest to move the implementation back into the |
Could I ask what would be the benefits of doing so? I think it could reduce compilation time but don't think that's significant. Am I missing something here? |
@jslee02: The idea is to take advantage of explicit instantiation for (a) compile time speedup for end users, (b) reduced likelihood of indecipherable compile errors, and (c) information hiding. Assuming there are only a small number of likely combinations of template parameters, we can instantiate those in compiled library code. Normal user code then compiles against the To make that work the |
I agree with that the goal of #160 and this pull request is what you pointed out, but I don't see why |
Oh, is that true? I would have thought making the compiler eat those definitions would be time consuming. |
You are right, sorry for the noise. I was not aware of the |
Yeah, I learned that while working on this.
It might not be as fast as we completely don't include the implementation headers, but they're pretty close in build time because of the |
@jwnimmer-tri, possible have-cake-and-eat-it-too option re |
I believe this PR is ready to merge. |
@jslee02: looks like build failed for 64 bit Windows Release build. Is that expected? |
It seems AppVeyor just failed for some reason. It passed before the last commit that I don't believe it caused the build error. It would be good to retrigger the test for sure, but I don't have access for it. |
Merging would trigger and could be repaired afterwards if there is a problem. (Or you could push a trivial commit.) |
Okay. I'll choose the first option. Merging now then. |
This pull request adds explicit template instantiations in order to reduce the increased build time due to the templatizing (#154) as proposed by #160.
The basic idea is to instantiate template objects (class or function) in source files for scalar types that are most likely used by the user (e.g.,
double
andfloat
) so that the compiler create binaries for the template objects for the scalar types. This reduces compilation times because the compiler doesn't need to generate code for those instantiated template objects.For this work, I followed the nice guide, which was written for Drake, but ran into one issue. In the example of the guide, all the object in the header is templatized for single parameter
T
, but many of FCL header have objects templatized for the mix of scalar type, shape type, narrowphase solver type. In this case, we should include the-inl.h
file at the end of the header so that the compiler can see the definitions in-inl.h
for objects are not explicitly instantiated in the source file.However, the problem of doing this was that the compiler generates codes even for
MyClassA<double>
. Thanks to C++11, we have a simple solution for this issue: explicit instantiation declaration ((2) in Explicit instantiation section of here). Now the-inl.h
file is modified as:It seems the explicit template declaration can be placed at the end of the header (the header includes
-inl.h
file anyways), but I prefer to hide from header because I think it's unnecessary info in reading the header to understand the API.One bonus of using explicit template declaration is that we don't need to include
-inl.h
even when we instantiate the template objects for own types (again, because the headers include-inl.h
files anyways).In this PR, the template objects are instantiated only for
double
. One open question is if it's worth to instantiate forfloat
type as well. I'm inclined not to instantiate forfloat
. A downside of having both would be the increased size of binary. We could consider having two binaries fordouble
andfloat
, but it would be an overkill.Note that this pull request should be merged after #163.