Optimising Template Classes for Size

Programming, for all ages and all languages.
Post Reply
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Optimising Template Classes for Size

Post by AJ »

Hi,

I have created a custom sorted linked-list class which I use in my kernel for keeping track of various things and have just tried to use that same class in my boot loader. The kernel is optimised for speed, and the bootloader is optimised for size (-Os). I am using the standard 32 bit wiki GCC Cross-Compiler (GCC 4.3.0).

The problem is that this template class uses private member functions for some tasks. Let me give you a summarised example. The linked list class uses the concept of a node (with a pointer to the next node) to create the linked list of type T:

Code: Select all

template <class T>

class SList
{
   public:
      T *Add(T *x)
      {
         /* Add data (x) to a new Node and add that new node to the linked list.*/
         ...
         InsertBefore(NodeToInsertBefore, NewNode);
         ...
      }
   private:
      Node *InsertBefore(Node *PrevNode, Node *NewNode)
      {
         ...
      }
}
So hopefully anyone who can help me gets the kind of idea...

Now - the problem is that I get the following compile-time error:

Code: Select all

error: inlining failed in call to '...::InsertBefore(Node*, Node*)...' : optimizing for size and code size would grow.
OK - so a fairly plain message and it obviously implies that InsertBefore is being inlined by default. But is there a sensible workaround? Fine - in this case I could just insert the code in to the 'Add' function and all would be well, but there are other circumstances where I want to use such private member functions.

It also surprises me that this is an error rather than a warning (and yes, I do always use -Werror and am not happy letting warnigns through the net).

Thanks in advance for any advice,
Cheers,
Adam
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re: Optimising Template Classes for Size

Post by Candy »

What compiler is that? What platform?

Got some compilable freestanding code that we can use to test?
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: Optimising Template Classes for Size

Post by jnc100 »

Gcc 4.3 seems to have a new option, -finline-small-functions, which is enabled by default with -O1 (and therefore -O2, -O3 and -Os), which wasn't present in previous versions. I'd imagine it tries to automatically inline small functions. When this option is combined with optimizing for size, then a warning is produced if inlining a function would cause an increase in code size (which you turn into an error with -Werror). I think the warning is -Winline, I'm not sure of the syntax but -Wno-inline may turn it off. Alternatively, I presume you could disable the option with -Os -fno-inline-small-functions for your optimize-for-space build.

Regards,
John.
User avatar
AJ
Member
Member
Posts: 2646
Joined: Sun Oct 22, 2006 7:01 am
Location: Devon, UK
Contact:

Re: Optimising Template Classes for Size

Post by AJ »

Hi,

Thanks for the help. The bit about it being an error is my bad - I always use -Werror but was still expecting to see the text "Warning:" rather than "Error". Removing -Werror does indeed allow a compilation with warnings.
Gcc 4.3 seems to have a new option, -finline-small-functions
This was very helpful and allowed me to look up some more information on the problem. It appears thad GCC decides heuristically which functions to inline, and still uses the same criteria with -Os. There is no -fno-inline-small-functions option that I can find (ICC seems to have this option, though!) and trying this at the command line makes no difference. There is the -fno-inline option, but that disables all inlines indiscriminitely.

Here's the solution which I should have seen before: I use a makefile based on Solar's excelent tutorial. This by default has the switches -Werror and -Winline. Of course, removing -Winline stops the warnings from happening, so I have done this.

Thanks for your help,
Adam
Post Reply