Wednesday, April 6, 2011

Qualcomm Onsite 之三

一个Interviewer问我以前的工作中code base有多大,我说大概有几万个文件,总共的size超过1GB。然后他又问我文件的大小是多少,我说从几百个字节到几千个字节不等。

接着,他又问我如果一个function超过一百行,会不会有问题?我说还行,我还见过更长的。话刚说完,我就有点知道他问的意思了。马上再补充说,这样的函数往往集合了太多的功能,developer最好应把它分解成几个sub-function,每个sub-function完成某项特定的任务,这样就可以实现较好的readibility。不过也有代价,就是stack的开销增加了,因为call每个sub-function都要把它们push进stack,执行完了再pop it up,也许会对performance有影响。Developers must balance between readibility and performance.

Interviewer接过话茬说,有没有一种方法,可以让我们enjoy the best of both worlds?我说很难。他提示说其实gcc的compiler提供了一种优化的方法可以做到不至于让多个sub-function影响performance, without using the keyword "inline". For example, gcc -O1/O2/O3

查了一下gcc的document,还真是如此:
http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

-O2 turns on all optimization flags specified by -O. It also turns on the following optimization flags:

        -fthread-jumps
        -falign-functions -falign-jumps
        -falign-loops -falign-labels
        -fcaller-saves
        -fcrossjumping
        -fcse-follow-jumps -fcse-skip-blocks
        -fdelete-null-pointer-checks
        -fdevirtualize
        -fexpensive-optimizations
        -fgcse -fgcse-lm
        -finline-small-functions
        -findirect-inlining
        -fipa-sra
        -foptimize-sibling-calls
        -fpartial-inlining
        -fpeephole2
        -fregmove
        -freorder-blocks -freorder-functions
        -frerun-cse-after-loop
        -fsched-interblock -fsched-spec
        -fschedule-insns -fschedule-insns2
        -fstrict-aliasing -fstrict-overflow
        -ftree-switch-conversion
        -ftree-pre
        -ftree-vrp

看样子,就是其中的-finline-small-functions这个开关起的作用了。以前在施乐写程序的时候,没太关注Makefile,看来付出代价了。

No comments:

Post a Comment