This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Templates - What potential problems are associated with their usage?



If a template class requires a template argument to determine the size of a member array, will that mean the methods are "duplicated" each time a different template parameter is used to instantiate the class? Worried about code bloat.

Are all template functions inlined or are they the same as functions using the inline keyword where the compiler can decide when to inline them? Any experience with how well the compiler makes these decisions?

What is the best method for using templates on a DSP with limited memory?

Thanks

Chris

  • c0l0jar0 said:
    If a template class requires a template argument to determine the size of a member array, will that mean the methods are "duplicated" each time a different template parameter is used to instantiate the class? Worried about code bloat.

    Most of this code bloat can be avoided by compiling with optimization.

    c0l0jar0 said:
    Are all template functions inlined or are they the same as functions using the inline keyword where the compiler can decide when to inline them?

    Strictly speaking, inlining of template functions is no different than other functions.  However, template functions are often very small, and thus are more likely to be inlined.  By building with --opt_level=2 or higher, the compiler automatically inlines most small functions.

    When you say DSP, I presume you mean a C6000 DSP.  To get the full performance entitlement of C6000, you must allow the compiler to perform an optimization on your loops called software pipelining.  This software pipelining optimization is only performed if you build with --opt_level=2 or higher.  Thus, I presume you build with at least --opt_level=2, and thus most of your concerns about code bloat and inlining get addressed.

    Thanks and regards,

    -George

  • So since I am attempting to reduce code bloat I am trying to determine if using a template class with two different template parameters will cause duplication in memory of the function. For instance, I believe if the method uses the template argument heavily then the code will be compiled more than once and memory will be required for each "version" of that function. On the other hand, if the argument is not used in the function but the method is part of a template class will it duplicate the function each time a different template arg is used?

    The second question was really asking the following. I have heard that all template functions are inline. Can the compiler choose to not inline these functions but instead even have a symbol in the MAP file for these functions if it chooses? Maybe when the function is very long?

    What can I do to avoid bloating the code with template usage. We do use optimization levels higher than 2. The compiler does inline many functions that did not use that keyword. My question is, what does the compiler do to avoid inlining or what should i do to prevent problems.

    Thanks

    Chris 

  • c0l0jar0 said:
    So since I am attempting to reduce code bloat I am trying to determine if using a template class with two different template parameters will cause duplication in memory of the function. For instance, I believe if the method uses the template argument heavily then the code will be compiled more than once and memory will be required for each "version" of that function. On the other hand, if the argument is not used in the function but the method is part of a template class will it duplicate the function each time a different template arg is used?

    I need an example to say anything concrete.  For now, I'll simplify things by saying we are talking only about a template function.  In theory, a new function is produced for each distinct type used when the template is instantiated.  Supposing one instantiation uses "int" and the other "double", that makes sense.  However, if one uses "int" and the the other uses "unsigned int", then only one function is produced.  

    c0l0jar0 said:
    I have heard that all template functions are inline.

    That's very common in practice, but only because template functions are, on average, much smaller than other functions. 

    c0l0jar0 said:
    Can the compiler choose to not inline these functions

    Yes.

    c0l0jar0 said:
    We do use optimization levels higher than 2.

    That is the single best thing you can do to avoid code bloat.

    Keep in mind that the particulars of this issue are not unique to the TI C++ compiler.  So any online discussion you find about this issue is relevant.

    Thanks and regards,

    -George