2

Though it may not be a good idea to inline such a large amount of codes, I hope that there is a way to deliberately inline all designated STL codes. For example,

#include <list>
#include <iostream>

std::list<int> list;
/* SKIPPED : INSERT RANDOM NUMBERS */
list.sort();   // forcibly inlined

for (int &num : list)
    std::cout << num << std::endl;

The goal is that I have to eliminate every possible STL function invocation overhead no matter how ridiculous it is and how small or large the member functions they are. All of the STL source codes is able to be modified. How to successfully achieve this either in gcc 6.2.0 or clang 3.0?

Kevin Dong
  • 5,001
  • 9
  • 29
  • 62
  • 5
    Is there any specific reason why you want this? Because this looks like a [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) to me. – Rakete1111 Jun 16 '17 at 16:41
  • 4
    `11001`, `0xdeadbeef` and `hdytrvivcewxtr` are _codes_, but what you're talking about is _code_. Also, there's no STL anymore, there is only the C++ Standard Library (aka _stdlib_). – ForceBru Jun 16 '17 at 16:42
  • @Rakete1111 I am doing a research on benchmarking the effect of calling overhead. – Kevin Dong Jun 16 '17 at 16:42
  • @ForceBru Yes. This is why I provide the version of my compilers, since STL is fully intergated into the C++ standard. – Kevin Dong Jun 16 '17 at 16:43
  • There are also _certain_ employers that make us write code without use of STL for C++98. It's an entirely legitimate question in my opinion. –  Jun 16 '17 at 16:44
  • 2
    This is a half/quarter dupe IMO: https://stackoverflow.com/questions/8381293/how-do-i-force-gcc-to-inline-a-function – Rakete1111 Jun 16 '17 at 16:44
  • @Rakete1111 I tried that before, but C++ compilers tend to ignore those attributes, since I can still see those callings occur in profilers such as Linux Perf. – Kevin Dong Jun 16 '17 at 16:45
  • @KevinDong Are you sure that you marked *every* function called with that attribute? Because I doubt that the docs are lying :) – Rakete1111 Jun 16 '17 at 16:46
  • @Rakete1111 I am sure. I traced down into every single member function as deep as possible. – Kevin Dong Jun 16 '17 at 16:47
  • @KevinDong If it doesn't work, then it may just not be possible. There's no requirement that compilers provide a way to inline everything. – Barmar Jun 16 '17 at 16:47
  • The linked solution might only work for ordinary C functions, not C++ templated functions. That adds a level of complexity that may prevent it from inlining. – Barmar Jun 16 '17 at 16:49
  • have you tried `--param inline-unit-growth=500` or similar? – Walter Jun 16 '17 at 16:53
  • @Walter I will try this. ;-) – Kevin Dong Jun 16 '17 at 17:01
  • @KevinDong "I can still see those callings occur in profilers such as Linux Perf" make **sure** that the calls aren't actually inlined by looking at the assembly. I'm​ not sure, but it *could* be that the profiler is able to recognize inlined functions, isn't it? – Daniel Jour Jun 16 '17 at 17:35

1 Answers1

1

By default gcc limits the overall growth if any compilation unit caused by inlining to 20% (factor 1.2). This can be changed with the compiler option

--param inline-unit-growth=500

when the growth allowed would be 500%, i.e. a factor 6. In fact, there are many more parameters controlling the behaviour of inlining. You should really study the documentation (or man pages) and set these parameters appropriately. Others are

--param max-inline-insns-single=number
--param max-inline-insns-auto=number
Walter
  • 44,150
  • 20
  • 113
  • 196
  • Thanks a lot. It works. [GCC 6.2.0 Options Here.](https://gcc.gnu.org/onlinedocs/gcc-6.2.0/gcc/Optimize-Options.html) – Kevin Dong Jun 16 '17 at 17:06