11

I am trying to speed up the compile time of the GLM(OpenGL Mathematics). GLM makes heavy usages of C++ templates.

This is what I have tried so far.

math.h
#pragma once

#include <glm\glm.hpp>
extern template struct glm::tvec3<float, glm::highp>;

math.cpp
#include "math.h"
template struct glm::tvec3<float, glm::highp>;

And then I have three files that are using the glm::vec3 template, glm::vec3 is a typedef of glm::tvec3<float, glm::highp>. The three files a,b,c looks almost the same:

a.cpp, b.cpp, c.cpp
#include "math.h"
glm::vec3 func() {
    glm::vec3 a = glm::vec3{1,1,1};
    glm::vec3 b = glm::vec3{1,1,1};
    return a + b;
}

I am using both explicit instantiation definition and explicit instantiation declaration. So the files a,b,c should not cause implicit instantiation. But the compile time is the same as if I don´t do it.

hidayat
  • 9,493
  • 13
  • 51
  • 66
  • 2
    AFAIK, explicit instantiation can only help speeding up compilation time if you move template declarations into separate .cpp file and explicitly instantiate only required templates. Which is not the case with header-only libraries. Even worse: explicitly instantiating class will generate all its symbols, including unused ones – Andrei R. Jan 26 '17 at 11:53
  • 1
    @AndreiR. You **don't have to hide** template implementation in order to avoid implicit instantion and thus force compiler to search for it in other translation unit. That's the whole point of `extern template` -> to force compiler to search for implementation in other translation unit rather than instantianting it in current one. – PcAF Jan 28 '17 at 00:35
  • @PcAF but then this example should work, should it not? – hidayat Jan 30 '17 at 09:17
  • I guess even though @PcAF is correct, the problem here is that the compiler still has to parse the entire header file. Since in your example you are only using the (presumably simple) constructor, you won't save much time on compiling that. – chtz Mar 31 '19 at 11:03
  • This question got my attention, I don't have an answer, but other users report your same problem when trying to use external templates with glm: http://cpc110.blogspot.com/2020/04/using-extern-template-with-third-party.html [ from that page: "I suspect that this is because #include does actually end up including the template definition, and at that point the subsequent extern template declarations are just redundant."] – crsn Jun 16 '20 at 08:27
  • I know then the time of asking this question wasn't exist, but it has some measurements. https://stackoverflow.com/q/45370039/14583599 – zerocukor287 Mar 16 '22 at 11:27

1 Answers1

0

Your math.h still causes users to include <glm\glm.hpp>
That's the thing you want to avoid to speed things up. To speed things up, make your own class whose implementation (inside math.cpp) might use glm.hpp, but the users of that class do not need to include glm.hpp themselves.

This is an example left to the student, but you want something like:

math.h
struct vec3{ double x1,x2,x3};
vec3 plus(const vec3& a, const vec3& b);

Then when a.cpp includes math.h, it provides the functions you need, but does not make all your compilation units include glm.hpp.

CHKingsley
  • 321
  • 2
  • 9