tune 那種被呼叫百萬次以上的小函式

這可能要較深的計算機組織知識。但運氣好時瞄一下它的組語,指定只作用於該函式的 optimization flags[1] 或用 profile guided optimization 也可能得逞。另外正統的作法是如改變資料結構在記憶體中的 layout 以減少 cache miss。

[1]: See "optimize" attribute in http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

For C:
1. Use an "allocation context object" that supports user specified destructors:
2. Write allocation functions for files, locks, reference counting etc that takes an allocation context and registers the correct destructor.
3. Add one call to free the allocation context where you'd have ended the block.
You'd still need to make sure your control flow goes through that one free call.

I'll save my argument for why this is better then "_attribute_((cleanup))" for now. Would make a nice discussion topic for our next meetup ;)
(On a separate note, this feel like dynamic versus lexical scoping)

My preferred solution to this problem in other high level languages:

I think the way Go handles this is less elegant then Python and C#'s solutions above.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License