• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Zero Lee的專欄

            Template and Inheritance

            As Meyers noted in Item 24 of Effective C++,the inability to inline a virtual function is its biggest performace penalty.
            Virtual functions seems to inflict a performace cost in several ways:
            [1] The vptr must be initialized in the constructor.
            [2] A virtual function is invoked via pointer indirection. We must fetch the pointer to the function table and then access the correct function offset.
            [3] Inlining is a compile-time decision. The compiler cannot inline virtual functions whose resolution takes place at run-time.
            The true cost of virtual functions then boils down to the third item only.

            -------------------------------------------------------------------------------------------
            Virtual function calls that can be resolved only at rum-time will inhibit inling. At times, that may pose a performace problem that we must solve. Dynamic binding of?a function call is a consequence of inheritance. One way to eliminate dyanamic binding is to replace inheritance with a template-based design. Templates are more performance-friendly in the sense that they push the resolution step from run-time to compile-time. Compile-time, as far as we are concerned, is free.

            The desing space for inheritance and templates has some overlap. We will discuss one such example.

            Suppose you wanted to develop a thread-safe string class that may be manipulated safely by concurrent threads in a Win32 environment. In that environment you have a choice of multiple synchronization schemes such ascriticalsection, mutex, and semanphores, just to name a few. You would like your thread-safe string to offer the flexibility to use any of those schemes, and at different times you may have a reason to prefer one scheme over another. Inheritance would be a reasonable choice to capture the commonality among synchronization mechanisms.

            The Locker abstract base class will declare the common interface:

            ?1 class ?Locker
            ?2 {
            ?3 public :
            ?4 ????Locker()? {?}
            ?5 ???? virtual ? ~ Locker()? {?}
            ?6 ???? virtual ? void ? lock ()? = ? 0 ;
            ?7 ???? virtual ? void ?unlock()? = ? 0 ;
            ?8 }
            ;
            ?9
            10 class ?CriticalSectionLock?:? public ?Locker
            11 {?
            12
            13 }
            ;
            14 class ?MutexLock?:? public ?Locker
            15 {
            16 ?
            17 }
            ;
            Because you prefer not to re-invent the wheel, you made the choice to derive the thread-safe string from the existing standard string. The remaining design choices are:

            [1] Hard coding. You could derive three distinct classes from string::CriticalSectionString, MutexString, and SemaphoreString, each class implementing its implied synchronization mechanism.
            [2] Inheritance. You could derive a single ThreadSafeString class that contains a pointer to a Locker object. Use polynorphism to select the particular synchronization mechanism at run-time.
            [3] Templates. Create a template-based string class parameterized by the Locker type.
            ////////////////////////////////////////////////////////////////////////////////////////////
            Here we only talk about the Template implementation.

            The templates-based design combines the best of both worlds-reuse and efficiency. The ThreadSafeString is implemented as a?template parameterized by the Locker template argument:
            ?1template?<class?LOCKER>
            ?2class?ThreadSafeString?:?public?string
            ?3{
            ?4public:
            ?5???ThreadSafeString(const?char*?s)?
            ?6???:?string(s)?{?}
            ?7???
            ?8???int?length();
            ?9private:
            10???LOCKER?lock;
            11}
            ;
            12
            The length method implementation is similar to the previous ones:
            ?1template?<class?LOCKER>
            ?2inline
            ?3int?ThreadSafeString<LOCKER>?::?length()
            ?4{
            ?5??lock.lock();
            ?6??int?len?=?string::length();
            ?7??lock.unlock();
            ?8
            ?9??return?len;
            10}
            If you want critical section protection, you will instantiate the template with a CriticalSectionLock:
            {
            ?? ThreadSafeString<CriticalSectionLock> csString = "Hello";
            ?? ...
            }
            or you may go with a mutex:
            {
            ?? ThreadSafeString<MutexLock> mtxString = "Hello";
            ?? ...
            }

            This design also provides a relief from the virtual function calls to lock() and unlock(). The declaration of a ThreadSafeString selects a particular type of synchronization upon template instantiation time. Just like hard coding, this enables the compiler to resolve the virtual calls and inline them.

            As you?can see, templates can make a positive performace contribution by pushing computations out of the excution-time and into compile-time, enabling inling in the process.

            posted on 2006-11-13 13:37 Zero Lee 閱讀(278) 評論(0)  編輯 收藏 引用 所屬分類: C++ Performance

            久久亚洲精品人成综合网| 久久精品蜜芽亚洲国产AV| 久久99国产精品久久99果冻传媒| 日韩乱码人妻无码中文字幕久久 | 韩国三级大全久久网站| 97久久婷婷五月综合色d啪蜜芽| 久久人人爽人人爽人人AV东京热| 亚洲国产成人久久综合一区77| 一本色道久久HEZYO无码| 久久国产精品无| 久久久久久免费视频| 亚洲欧洲久久av| 狠狠色丁香久久婷婷综合| 日韩精品久久久肉伦网站| 久久综合88熟人妻| 色综合久久久久无码专区| 欧美成a人片免费看久久| 久久精品无码一区二区无码| 精品久久久久久成人AV| 香蕉久久夜色精品升级完成| 久久精品国产亚洲av日韩| 久久久精品午夜免费不卡| 丰满少妇人妻久久久久久 | 久久99国产精品久久99果冻传媒 | 日日狠狠久久偷偷色综合0 | 久久99国产综合精品女同| 久久婷婷综合中文字幕| 欧美激情精品久久久久久| 亚洲AV无码1区2区久久| 亚洲狠狠久久综合一区77777| 亚洲成色WWW久久网站| 蜜桃麻豆www久久| 免费精品久久天干天干| 久久狠狠色狠狠色综合| 久久这里只有精品首页| 日韩精品久久久久久| 亚洲午夜久久久久久噜噜噜| 狠狠色丁香婷婷综合久久来来去| 久久精品中文字幕久久| 久久中文字幕人妻熟av女| 99久久精品免费国产大片|