N
N
Nikita Gusakov2014-06-24 03:02:48
Search Engine Optimization
Nikita Gusakov, 2014-06-24 03:02:48

Does gcc optimize getters/setters without inline?

Actually the whole question is in the title. Optimization means automatic inline, without declaring a keyword.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
R
RPG, 2014-06-24
@hell0w0rd

If the body of the getter/setter is in the scope of its use, then it completely substitutes: goo.gl/zry8bv. The -O2 flag is sufficient for this. However, the example is strained and far from reality.
However, in the case of complex code (more than 1 file), the developer will have a choice: leave the body of the function in the header file or compile the implementation separately in another cpp file. If there is no function body in the header file, the compiler will be forced to flatten the object code of the function instead of the source code, and you need to include the -flto flag when compiling.
Further, it all depends on what you are doing - a library or your own program. In a library, methods are usually hidden and not made inline, so as not to force client programs to recompile when changing class methods.
My advice is to leave this question until you find your getter or setter at the top of the profiler.
UPD. Experimenting with the new compiler (gcc 4.9) shows that the -O2 -flto flag does do interprocedural inline optimization automatically if possible. The second compiler available to me (gcc 4.4) cannot do this - there is no LTO support yet. Accordingly, when using new compilers, there really is no point in inline. Here are the files for testing: https://gist.github.com/scriptum/57ae4d2524d42fccb494

T
tsarevfs, 2014-06-24
@tsarevfs

Depends on compiler options. With -O3 they should. There was an interesting bug in connection with automatic inline .

M
Misha Krinkin, 2014-06-24
@kmu1990

If the definition is available, then of course it can optimize, depending on whether -O2 or O3 is used, it inlines with varying degrees of activity. Otherwise, you can specify the -lto flag when compiling and linking (it seems to have appeared since version 4.6), but I don’t know how actively it will inline and how to manage it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question