• <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>

            SGI STL中默認Allocator為何變為new_allocator?

            peakflys原創作品,轉載請保留原作者和源鏈接
               項目中和自己代碼中大量使用了STL的容器,平時也沒怎么關注alloc的具體實現細節,主觀認識上還停留在侯捷大師的《STL源碼剖析》中的講解。
               以下為書中摘錄截圖:詳見書中2.2.4節內容

            前段時間項目中出了一個內存問題,在追查問題的過程中查看了對應的源碼(版本為libstdc++-devel-4.1.2)
            源碼文件c++allocator.h中定義了默認的Alloc:#ifndef _CXX_ALLOCATOR_H
            #define _CXX_ALLOCATOR_H 1

            // Define new_allocator as the base class to std::allocator.
            #include <ext/new_allocator.h>
            #define __glibcxx_base_allocator  __gnu_cxx::new_allocator

            #endif
            查看new_allocator.h文件,發現new_allocator僅僅是對operator new和operator delete的簡單封裝(感興趣的朋友可自行查看)。
            眾所周知libstdc++中STL的大部分實現是取自SGI的STL,而《STL源碼剖析》的源碼是Cygnus C++ 2.91則是SGI STL的早期版本,下載源碼看了一下allocator的實現確實如書中所言。
            不知道從哪個版本起,SGI的STL把默認的Alloc替換成了new_allocator,有興趣的同學可以查一下。
            知道結果后,可能很多人和我一樣都不禁要問:Why?
            以下是兩個版本的源碼實現:
            1、new_allocator
                  // NB: __n is permitted to be 0.  The C++ standard says nothing
                  
            // about what the return value is when __n == 0.
                  pointer
                  allocate(size_type __n, const void* = 0)
                  {
                if (__builtin_expect(__n > this->max_size(), false))
                  std::__throw_bad_alloc();

                return static_cast<_Tp*>(::operator new(__n * sizeof(_Tp)));
                  }

                  // __p is not permitted to be a null pointer.
                  void
                  deallocate(pointer __p, size_type)
                  { ::operator delete(__p); }
            2、__pool_alloc
              template<typename _Tp>
                _Tp*
                __pool_alloc<_Tp>::allocate(size_type __n, const void*)
                {
                  pointer __ret = 0;
                  if (__builtin_expect(__n != 0, true))
                {
                  if (__builtin_expect(__n > this->max_size(), false))
                    std::__throw_bad_alloc();

                  // If there is a race through here, assume answer from getenv
                  
            // will resolve in same direction.  Inspired by techniques
                  
            // to efficiently support threading found in basic_string.h.
                  if (_S_force_new == 0)
                    {
                      if (getenv("GLIBCXX_FORCE_NEW"))
                    __atomic_add(&_S_force_new, 1);
                      else
                    __atomic_add(&_S_force_new, -1);
                    }

                  const size_t __bytes = __n * sizeof(_Tp);
                  if (__bytes > size_t(_S_max_bytes) || _S_force_new == 1)
                    __ret = static_cast<_Tp*>(::operator new(__bytes));
                  else
                    {
                      _Obj* volatile* __free_list = _M_get_free_list(__bytes);

                      lock sentry(_M_get_mutex());
                      _Obj* __restrict__ __result = *__free_list;
                      if (__builtin_expect(__result == 0, 0))
                    __ret = static_cast<_Tp*>(_M_refill(_M_round_up(__bytes)));
                      else
                    {
                      *__free_list = __result->_M_free_list_link;
                      __ret = reinterpret_cast<_Tp*>(__result);
                    }
                      if (__builtin_expect(__ret == 0, 0))
                    std::__throw_bad_alloc();
                    }
                }
                  return __ret;
                }
              template<typename _Tp>
                void
                __pool_alloc<_Tp>::deallocate(pointer __p, size_type __n)
                {
                  if (__builtin_expect(__n != 0 && __p != 0, true))
                {
                  const size_t __bytes = __n * sizeof(_Tp);
                  if (__bytes > static_cast<size_t>(_S_max_bytes) || _S_force_new == 1)
                    ::operator delete(__p);
                  else
                    {
                      _Obj* volatile* __free_list = _M_get_free_list(__bytes);
                      _Obj* __q = reinterpret_cast<_Obj*>(__p);

                      lock sentry(_M_get_mutex());
                      __q ->_M_free_list_link = *__free_list;
                      *__free_list = __q;
                    }
                }
                }
            從源碼中可以看出new_allocator基本就沒有什么實現,僅僅是對operator new和operator delete的封裝,而__pool_alloc的實現基本和《STL源碼剖析》中一樣,所不同的是加入了多線程的支持和強制operator new的判斷。
            無論從源碼來看,還是實際的測試(后續會附上我的測試版本),都可以看出__pool_alloc比new_allocator更勝一籌。

            同很多人討論都不得其解,網上也很少有關注這個問題的文章和討論,倒是libstdc++的官網文檔有這么一段:
            (peakflys注:文檔地址:https://gcc.gnu.org/onlinedocs/libstdc++/manual/memory.html#allocator.default)
            從文檔的意思來看,選擇new_allocator是基于大量測試,不幸的是文檔中鏈接的測試例子均無法訪問到……不過既然他們說基于測試得出的結果,我就隨手寫了一個自己的例子:
            #include <map>
            #include <vector>
            #ifdef _POOL
            #include <ext/pool_allocator.h>
            #endif

            static const unsigned int Count = 1000000;

            using namespace std;

            struct Data
            {
                int a;
                double b;
            };

            int main()
            {
            #ifdef _POOL
                map<int, Data, less<int>, __gnu_cxx::__pool_alloc<pair<int, Data> > > mi;
                vector<Data, __gnu_cxx::__pool_alloc<Data> > vi;
            #else
                map<int, Data> mi;
                vector<Data> vi;
            #endif

                for(int i = 0; i < Count; ++i)
                {
                    Data d;
                    d.a = i;
                    d.b = i * i;
                    mi[i] = d;
                    vi.push_back(d);
                }
                mi.clear();
            #ifdef _POOL
                vector<Data, __gnu_cxx::__pool_alloc<Data> >().swap(vi);
            #else
                vector<Data>().swap(vi);
            #endif
                for(int i = 0; i < Count; ++i)
                {
                    Data d;
                    d.a = i;
                    d.b = i * i;
                    mi[i] = d;
                    vi.push_back(d);
                }
                return 0;
            }
            因為當數據大于128K時,__pool_alloc同new_allocator一樣直接調用operator,所以例子中構造出的Data小于128K,來模擬兩個分配器的不同。同時如libstdc++官網中說的,我們同時使用了sequence容器vector和associate容器map。
            例子中模擬了兩種類型容器的插入-刪除-插入的過程,同時里面包含了元素的構造、析構以及內存的分配和回收。
            以下是在我本地機器上運行的結果:
            1、-O0的版本:

            2、-O2的版本:

            多線程的測試例子我就不貼了,測試結果大致和上面相同,大家可以自行測試。
            從多次運行的結果來看__pool_alloc的性能始終是優于new_allocator的。
            又回到那個問題,為什么SGI STL的官方把默認的Alloc從__pool_alloc變為new_allocator。
            本篇文章不能給大家一個答案,官方網站上也未看到解釋,自己唯一可能的猜測是
            1、__pool_alloc不利于使用者自定義operator new和operator delete(其實這條理由又被我自己推翻了,因為通過源碼可以知道置位_S_force_new即可解決)
            2、malloc性能的提升以及硬件的更新導致使用默認的operator new即可。

            如果大家有更好,更權威的答案請告訴我(peakflys@gmail.com)或者留言,謝謝。
                                                                              by peakflys 16:49:49 Wednesday, January 14, 2015

            posted on 2015-01-14 16:50 peakflys 閱讀(4397) 評論(8)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: SGI STL中默認Allocator為何變為new_allocator? 2015-01-15 00:12 egmkang

            跟硬件沒關系,malloc性能提升.因為malloc本身在thread上面就有一個pool.
            而且高性能的分配器越來越多,比如tcmalloc/jemalloc,已經沒有任何必要再提供一個pool allocator  回復  更多評論   

            # re: SGI STL中默認Allocator為何變為new_allocator? 2015-01-15 09:12 蘋果汁

            樓主好學 贊一個  回復  更多評論   

            # re: SGI STL中默認Allocator為何變為new_allocator?[未登錄] 2015-01-15 13:01 chipset

            pool不拼接內存,一塊大內存被分割成很多相等的小塊,此時調用分配一塊較大內存可能失敗,不是因為內存不夠用,而是因為全是分割成小碎塊不拼接導致的。這是Pool的致命傷。再者Pool的效率也高不到哪里,以不拼接小塊換速度的做法不值得提倡。  回復  更多評論   

            # re: SGI STL中默認Allocator為何變為new_allocator? 2015-01-15 17:42 xxoo

            樓上的,內存池的作用不就是這個嗎?你申請一塊內存,可以做到物理不連續?  回復  更多評論   

            # re: SGI STL中默認Allocator為何變為new_allocator? 2015-01-15 19:23 peakflys

            如果我沒記錯的話malloc自始至終都有自己的一套pool策略,所以我所說的malloc性能提升并非指的這個。
            不過第三方穩定高效的allocator實現可能是標準庫作者放棄pool的一個原因@egmkang
              回復  更多評論   

            # re: SGI STL中默認Allocator為何變為new_allocator? 2015-01-15 19:31 peakflys

            說的不錯,pool的方式存在的問題是挺多。不過對于pool的效率高不到哪去的觀點我不敢認同,針對大量小數據的分配,有時候還必須得使用pool的方式,不然ptmalloc、tcmalloc、jemalloc等不會維護復雜的內存分配方式@chipset  回復  更多評論   

            # re: SGI STL中默認Allocator為何變為new_allocator? 2015-01-20 15:24 juegoskizi

            我很喜歡,很不錯的職位。  回復  更多評論   

            # re: SGI STL中默認Allocator為何變為new_allocator?[未登錄] 2016-03-07 14:09 JAKE

            樓主,有個問題請教下:
            我有個應用場景,即線程A使用obj_pool分配了一個對象obj,線程B用完obj后歸還給pool。 這里肯定會有線程同步隱患。

            目前我使用兩個lockfree隊列完美解決這個問題,但很繁瑣。

            我想問的是
            使用tcmalloc的話,能解決我上邊所說的問題嗎?  回復  更多評論   

            <2015年1月>
            28293031123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            導航

            統計

            公告

            人不淡定的時候,就愛表現出來,敲代碼如此,偶爾的靈感亦如此……

            常用鏈接

            留言簿(4)

            隨筆分類

            隨筆檔案

            文章檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产亚洲美女精品久久久| 欧美久久亚洲精品| 国产精品无码久久综合| 久久WWW免费人成—看片| 日韩十八禁一区二区久久| 久久久久国产精品人妻| 久久人人爽人爽人人爽av| 国产成人久久激情91| 亚洲午夜久久久影院伊人| yy6080久久| 日本五月天婷久久网站| 99精品国产在热久久无毒不卡| 久久精品亚洲乱码伦伦中文| 18岁日韩内射颜射午夜久久成人| 久久久国产精品亚洲一区| 日韩精品久久久肉伦网站| 久久婷婷国产剧情内射白浆| 国产欧美久久一区二区| 国产精品久久成人影院| 日韩欧美亚洲综合久久| 久久久99精品一区二区| 国内精品伊人久久久久av一坑| 伊人久久大香线蕉精品不卡 | 中文字幕精品久久久久人妻| 久久久精品国产sm调教网站| 东方aⅴ免费观看久久av| 久久强奷乱码老熟女| 国产亚州精品女人久久久久久 | 久久精品国产99久久久| 国产精品久久久久久久人人看| 精品久久久久国产免费| 国产ww久久久久久久久久| 久久精品无码免费不卡| 91久久精品91久久性色| 青草国产精品久久久久久| 少妇无套内谢久久久久| 伊人久久大香线蕉成人| 久久精品国产精品亚洲毛片| 69久久精品无码一区二区| 久久不见久久见免费视频7| 色综合久久久久综合体桃花网 |