c - How to write wrapper function so that compiler efficiently optimize it? -
i have function basic error checking before returning next node of link list:
node *next_node(node *n) { switch(type(n)){ case a: case b: . . case n: return n->next; default: exit(1); //this representation of code handling error scenario. } }
there more wrapper functions on top of things depending upon return value of above mentioned function.
these wrapper functions being used extensively in whole code base. when run profiler, found them time consuming routines. make sense me because there thousands of function calls different code area these functions , such number of calls take time due function call overheading and/or instruction cache misses.
i know compilers optimization around these, can inlined while generating assembly code. seems not working because of current way of implementation.
so, questions are:
- what common way of writing such wrapper functions compiler can optimize them low runtime?
- how other companies handle such scenario in code base?
note1: code above representation, there lot of such wrapper function in whole code base. therefore, if has idea on improving upon runtime issue due wrapper functions, should share idea.
note2: i'm using gcc compiler , code base entirely in c.
so seem in correct case low level optimization because have profiled code , found time consuming function.
assuming use higher optimization level of compiler, bad part there no general answer such question. can give hints here:
- are redundant operations somewhere? if yes can try make sure them once
- are there complex loops unwinded? not gain tests @ price of longer code
- is assembly code option? if yes can try write or part of function in assembly language
but (should?) wonder whether:
- the general structure of application correct - if can manage less call function spend less time in it...
- you need tests in production code. if assertions more tests, should conditionally included
#ifdef
in tests , debug versions.
Comments
Post a Comment