5

I have a C application that is heavily network I/O bound. It is currently compiled with -O2 on gcc. Building the application with -Os shows gives a 20% size reduction. Some basic testing showed no measurable decrease (or increase) in performance.

Is this a good case for building with -Os? Is there a reason not to do this? I've never actually seen a program that has been compiled for size no matter how much time it spends on I/O.

Remus
  • 53
  • 2
  • 3
    Does the size matter to you? – Iharob Al Asimi Apr 25 '15 at 22:03
  • Often, only 1% of the code is responsible for 99% of the CPU time spend. Ideally you'd compile that 1% for speed and the rest for size. The problem is that the compiler has trouble figuring this out. Try profile guided optimization. – usr Apr 25 '15 at 22:42

1 Answers1

4

Optimization shouldn't affect the program's operations. Therefore, no type of optimization should affect network I\O used by the program, and anything else for that matter. If your program sends 10 kilobytes, it will send the same even after optimization.

Optimization may affect the way structs are aligned among other things (like the size of the code, memory usage and more) but won't affect the logic at all (if programmed correctly).

Generally, as binary files tend to be relatively small (a 1MB binary file is an extremely large one) it is more common to optimize for speed rather than size. However, it is up to you.

Mark Segal
  • 5,427
  • 4
  • 31
  • 69
  • Wouldn't the smaller size of the program mean better use of the icache? – Remus Apr 25 '15 at 22:19
  • It really depends on the specific usage of the program. However, in my laptop, I've got a total of 256KB of L1 cache, 1MB of L2 cache and 6MB of L3 cache. It's highly unlikely that your program is much larger than 6MB, and even so, vast parts of it are probably mostly unused. Trust your compiler for optimization. – Mark Segal Apr 25 '15 at 22:21
  • Please refer to this question for more information regarding optimization - http://stackoverflow.com/questions/19470873/why-does-gcc-generate-15-20-faster-code-if-i-optimize-for-size-instead-of-speed?rq=1 – Mark Segal Apr 25 '15 at 22:24
  • I was thinking that any space savings in the instruction cache means that _other_ programs running on the system can make use of it. It is probably overkill but does that make sense? – Remus Apr 25 '15 at 22:40
  • It varies depending on usage. How many programs running? How much of your programs code is actually commonly used? Which OS and platform are you using? Usually only a small part of a program is used commonly. Provide more details for a datailed answerm – Mark Segal Apr 26 '15 at 05:05