Thread: Optimizing by eliminating translation units

  1. #1
    Registered User
    Join Date
    Feb 2011

    Optimizing by eliminating translation units

    Hi everyone,

    I know that MSVC is capable of looking into other translation units while compiling one translation unit, to enable optimization. I'm told that GCC can only inline code and not more than this. Is it a valid optimization technique, then, for a program that requires it, that right before you create the release executable, you merge all your translation units into one big ugly .c file, so the compiler can have as much information as possible about all other functions while compiling?


  2. #2
    Registered User
    Join Date
    Sep 2006
    I believe it should be tested, rather than having an opinion rendered.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Generally, I wouldn't bother. While, technically, the compiler might have more information that it can call on in order to optimise, the gains will rarely be significant - particularly compared with the effort of subsequently maintaining your code (only trivial programs will never require modification after release).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Feb 2011


    Wait, I don't mean that you would keep your code like that. I mean, let's say you're developing a game and your source is in 1000 *.c files and 1000 *.h files. Right when it's time to make the executable that is going on the installation DVD, you create a temporary .c file, and put everything into one big file, send it through your compiler (will take ages), and then the resulting executable file is lean and mean.

    Then you go back to your 2000 files and keep squashing bugs during the life cycle of the program. See what I mean? For some modern games, they try very hard to get an extra few frames per second. I wonder if this would work.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    The chance that the compiler will discover some massive meaningful optimisation of the scale you're imagining is slim.

    While it is trying to analyse such a large amount of code, chances are that it will simply run out of memory.

    You get a real chance from the compiler by making as many functions as possible "static" within a single translation unit. The compiler can then see ALL instances of where the static function is called from, and make some pretty meaningful optimisations as a result.

    And no, I've never seen this "cat *.c > eatthis.c" as an optimisation deployed in practice.

    REAL optimisation comes from the skill of the programmer choosing the best algorithms and data structures for the problem at hand.

    > they try very hard to get an extra few frames per second.
    I once managed a 10x improvement in frame rate for an application.
    I read the manual on how to use the system properly!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Mar 2009
    Recent versions of GCC can do whole-program optimization just like MSVC. Look into -flto and -fwhopr : LinkTimeOptimization - GCC Wiki. It's worth something like 5% in SPEC, which is actually pretty decent for such a well optimized-for benchmark suite.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. how to use one .c file variable in another .c file
    By nicoeschpiko in forum C Programming
    Replies: 31
    Last Post: 03-15-2010, 09:08 AM
  3. Help me please.
    By yann in forum C Programming
    Replies: 15
    Last Post: 09-29-2009, 09:04 PM
  4. Integration problem
    By HAssan in forum C Programming
    Replies: 4
    Last Post: 01-23-2006, 03:07 AM
  5. Big problem how to calculate units
    By codebrain in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2003, 01:19 PM

Tags for this Thread