日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

bullet HashMap 内存紧密的哈希表

發(fā)布時(shí)間:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 bullet HashMap 内存紧密的哈希表 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

last modified time:2014-11-9 14:07:00

bullet 是一款開源物理引擎,它提供了碰撞檢測(cè)、重力模擬等功能,非常多3D游戲、3D設(shè)計(jì)軟件(如3D Mark)使用它作為物理引擎。

作為物理引擎,對(duì)性能的要求是非常苛刻的;bullet項(xiàng)目之所以可以發(fā)展到今天,非常大程度取決于它在性能上優(yōu)異的表現(xiàn)。

翻閱bullet的源代碼就能看到非常多源代碼級(jí)別的優(yōu)化。本文將介紹的HashMap就是一個(gè)典例。

bullet項(xiàng)目首頁(yè):http://bulletphysics.org/

注:bullet非常多函數(shù)定義了Debug版和Release版兩個(gè)版本號(hào)。本文僅以Release版為例。


btAlignedAllocator的接口定義

btAlignedAllocator是bullet定義的一個(gè)內(nèi)存分配器接口,bullet的其它數(shù)據(jù)結(jié)構(gòu)都使用它來(lái)管理內(nèi)存。btAlignedAllocator的定義和STL的allocator(下面稱std::allocator)類似: ///The btAlignedAllocator is a portable class for aligned memory allocations. ///Default implementations for unaligned and aligned allocations can be overridden by a custom allocator // using btAlignedAllocSetCustom and btAlignedAllocSetCustomAligned. template < typename T , unsigned Alignment > class btAlignedAllocator {typedef btAlignedAllocator< T , Alignment > self_type;public://just going down a list:btAlignedAllocator() {}/*btAlignedAllocator( const self_type & ) {}*/template < typename Other >btAlignedAllocator( const btAlignedAllocator< Other , Alignment > & ) {}typedef const T* const_pointer;typedef const T& const_reference;typedef T* pointer;typedef T& reference;typedef T value_type;pointer address ( reference ref ) const { return &ref; }const_pointer address ( const_reference ref ) const { return &ref; }pointer allocate ( size_type n , const_pointer * hint = 0 ) {(void)hint;return reinterpret_cast< pointer >(btAlignedAlloc( sizeof(value_type) * n , Alignment ));}void construct ( pointer ptr , const value_type & value ) { new (ptr) value_type( value ); }void deallocate( pointer ptr ) {btAlignedFree( reinterpret_cast< void * >( ptr ) );}void destroy ( pointer ptr ) { ptr->~value_type(); }template < typename O > struct rebind {typedef btAlignedAllocator< O , Alignment > other;};template < typename O >self_type & operator=( const btAlignedAllocator< O , Alignment > & ) { return *this; }friend bool operator==( const self_type & , const self_type & ) { return true; } }; 與std::allocator類似。btAlignedAllocator的allocate和deallocate分別負(fù)責(zé)申請(qǐng)和釋放內(nèi)存空間,以release版編譯的btAlignedAlloc/btAlignedFree分別為: void* btAlignedAllocInternal (size_t size, int alignment);void btAlignedFreeInternal (void* ptr);#define btAlignedAlloc(size,alignment) btAlignedAllocInternal(size,alignment)#define btAlignedFree(ptr) btAlignedFreeInternal(ptr)而btAlignedAllocInternal/btAlignedFreeInternal及其定制化的實(shí)現(xiàn)為: static btAlignedAllocFunc *sAlignedAllocFunc = btAlignedAllocDefault; static btAlignedFreeFunc *sAlignedFreeFunc = btAlignedFreeDefault;void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc) {sAlignedAllocFunc = allocFunc ? allocFunc : btAlignedAllocDefault;sAlignedFreeFunc = freeFunc ?

freeFunc : btAlignedFreeDefault; } void* btAlignedAllocInternal (size_t size, int alignment) { gNumAlignedAllocs++; // 和gNumAlignedFree結(jié)合用來(lái)檢查內(nèi)存泄露 void* ptr; ptr = sAlignedAllocFunc(size, alignment); // printf("btAlignedAllocInternal %d, %x\n",size,ptr); return ptr; } void btAlignedFreeInternal (void* ptr) { if (!ptr) { return; } gNumAlignedFree++; // 和gNumAlignedAllocs 結(jié)合用來(lái)檢查內(nèi)存泄露 // printf("btAlignedFreeInternal %x\n",ptr); sAlignedFreeFunc(ptr); }


如上,bullet內(nèi)存分配的定制操作并不復(fù)雜。僅僅需調(diào)用下面兩個(gè)函數(shù)就可以: // The developer can let all Bullet memory allocations go through a custom memory allocator, using btAlignedAllocSetCustom void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc);// If the developer has already an custom aligned allocator, then btAlignedAllocSetCustomAligned can be used. // The default aligned allocator pre-allocates extra memory using the non-aligned allocator, and instruments it. void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc);

不管是否定制自己的Alloc/Free(或AllignedAlloc/AlignedFree),bullet內(nèi)的其它數(shù)據(jù)結(jié)構(gòu)都使用btAlignedAllocator作為內(nèi)存分配(回收)的接口。隨后將會(huì)看到。btAlignedAllocator的定制化設(shè)計(jì)與std::allocator的不同。文末具體討論。


btAlignedAllocator的內(nèi)存對(duì)齊

btAlignedAllocator除了定制化與std::allocator不同外,還添加了內(nèi)存對(duì)齊功能(從它的名字也能看得出來(lái))。

繼續(xù)查看btAlignedAllocDefault/btAlignedFreeDefault的定義(btAlignedAllocator.{h|cpp})能夠看到:

#if defined (BT_HAS_ALIGNED_ALLOCATOR) #include <malloc.h> static void *btAlignedAllocDefault(size_t size, int alignment) {return _aligned_malloc(size, (size_t)alignment); // gcc 提供了 }static void btAlignedFreeDefault(void *ptr) {_aligned_free(ptr); } #elif defined(__CELLOS_LV2__) #include <stdlib.h>static inline void *btAlignedAllocDefault(size_t size, int alignment) {return memalign(alignment, size); }static inline void btAlignedFreeDefault(void *ptr) {free(ptr); } #else // 當(dāng)前編譯環(huán)境沒(méi)有 對(duì)齊的(aligned)內(nèi)存分配函數(shù) static inline void *btAlignedAllocDefault(size_t size, int alignment) {void *ret;char *real;real = (char *)sAllocFunc(size + sizeof(void *) + (alignment-1)); // 1. 多分配一點(diǎn)內(nèi)存if (real) {ret = btAlignPointer(real + sizeof(void *),alignment); // 2. 指針調(diào)整*((void **)(ret)-1) = (void *)(real); // 3. 登記實(shí)際地址} else {ret = (void *)(real);}return (ret); }static inline void btAlignedFreeDefault(void *ptr) {void* real;if (ptr) {real = *((void **)(ptr)-1); // 取出實(shí)際內(nèi)存塊 地址sFreeFunc(real);} } #endif

bullet本身也實(shí)現(xiàn)了一個(gè)對(duì)齊的(aligned)內(nèi)存分配函數(shù)。在系統(tǒng)沒(méi)有對(duì)齊的內(nèi)存分配函數(shù)的情況下,也能保證btAlignedAllocator::acllocate返回的地址是按特定字節(jié)對(duì)齊的。

以下就來(lái)分析btAlignedAllocDefault / btAlignedFreeDefault是怎樣實(shí)現(xiàn)aligned allocation / free的。sAllocFunc/sFreeFunc的定義及初始化:

static void *btAllocDefault(size_t size) {return malloc(size); }static void btFreeDefault(void *ptr) {free(ptr); }static btAllocFunc *sAllocFunc = btAllocDefault; static btFreeFunc *sFreeFunc = btFreeDefault;

bullet同一時(shí)候提供了,AllocFunc/FreeFunc的定制化:

void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc) {sAllocFunc = allocFunc ? allocFunc : btAllocDefault;sFreeFunc = freeFunc ? freeFunc : btFreeDefault; } 默認(rèn)情況下sAllocFunc/sFreeFunc就是malloc/free,btAlignedAllocDefault中可能令人疑惑的是——為什么要多分配一點(diǎn)內(nèi)存?后面的btAlignPointer有什么用?

再來(lái)看看bullet是怎樣實(shí)現(xiàn)指針對(duì)齊的(btScalar.h):

///align a pointer to the provided alignment, upwards template <typename T>T* btAlignPointer(T* unalignedPtr, size_t alignment) {struct btConvertPointerSizeT{union {T* ptr;size_t integer;};};btConvertPointerSizeT converter;const size_t bit_mask = ~(alignment - 1);converter.ptr = unalignedPtr;converter.integer += alignment-1;converter.integer &= bit_mask;return converter.ptr; }

接下來(lái)分析btAlignPointer是怎樣調(diào)整指針的?

實(shí)際調(diào)用btAlignPointer時(shí),使用的alignment都是2的指數(shù)。如btAlignedObjectArray使用的是16,以下就以16進(jìn)行分析。

先如果unalignedPtr是alignment(16)的倍數(shù),則converter.integer += alignment-1; 再 converter.integer &= bit_mask之后,unalignedPtr的值不變。還是alignment(16)的倍數(shù)。

再如果unalignedPtr不是alignment(16)的倍數(shù),則converter.integer += alignment-1; 再converter.integer &= bit_mask之后。unalignedPtr的值將被上調(diào)到alignment(16)的倍數(shù)。

所以btAlignPointer可以將unalignedPtr對(duì)齊到alignment倍數(shù)。】


明確了btAlignPointer的作用。自然可以明確btAlignedAllocDefault中為什么多申請(qǐng)一點(diǎn)內(nèi)存,申請(qǐng)的大小是size + sizeof(void *) + (alignment-1):

假設(shè)sAllocFunc返回的地址已經(jīng)依照alignment對(duì)齊,則sizeof(void*)和sizeof(alignment-1)及btAlignedAllocDefault的返回值關(guān)系例如以下圖所看到的:


void*前面的alignment-sizeof(void*)字節(jié)和尾部的sizeof(size)-1字節(jié)的內(nèi)存會(huì)被浪費(fèi),只是非常小(相對(duì)內(nèi)存條而言)管他呢。


假設(shè)sAllocFunc返回的地址沒(méi)能按alignment對(duì)齊,則sizeof(void*)和sizeof(alignment-1)及btAlignedAllocDefault的返回值關(guān)系例如以下圖所看到的:



PS: 順便一提,為什么須要內(nèi)存對(duì)齊?簡(jiǎn)單地說(shuō)。依照機(jī)器字長(zhǎng)倍數(shù)對(duì)齊的內(nèi)存。CPU訪問(wèn)的速度更快;詳細(xì)來(lái)說(shuō),則要依據(jù)詳細(xì)CPU和總線控制器的廠商文檔來(lái)討論的,那將涉及非常多平臺(tái)、硬件細(xì)節(jié),所以本文不正確該話題著墨太多。


btAlignedObjectArray——bullet的動(dòng)態(tài)數(shù)組

btAlignedObjectArray的作用與STL的vector類似(下面稱std::vector),都是動(dòng)態(tài)數(shù)組。btAlignedObjectArray的數(shù)據(jù)成員(data member)聲明例如以下:

template <typename T> class btAlignedObjectArray {btAlignedAllocator<T , 16> m_allocator; // 沒(méi)有data member。不會(huì)添加內(nèi)存int m_size;int m_capacity;T* m_data;//PCK: added this linebool m_ownsMemory; // ... 省略 };

btAlignedObjectArray同一時(shí)候封裝了QuickSort。HeapSort。BinarySearch,LinearSearch函數(shù),可用于排序、查找,btAlignedObjectArray的全部成員函數(shù)(member function)定義例如以下:

template <typename T> //template <class T> class btAlignedObjectArray {btAlignedAllocator<T , 16> m_allocator;int m_size;int m_capacity;T* m_data;//PCK: added this linebool m_ownsMemory;#ifdef BT_ALLOW_ARRAY_COPY_OPERATOR public:SIMD_FORCE_INLINE btAlignedObjectArray<T>& operator=(const btAlignedObjectArray<T> &other); #else//BT_ALLOW_ARRAY_COPY_OPERATOR private:SIMD_FORCE_INLINE btAlignedObjectArray<T>& operator=(const btAlignedObjectArray<T> &other); #endif//BT_ALLOW_ARRAY_COPY_OPERATORprotected:SIMD_FORCE_INLINE int allocSize(int size);SIMD_FORCE_INLINE void copy(int start,int end, T* dest) const;SIMD_FORCE_INLINE void init();SIMD_FORCE_INLINE void destroy(int first,int last);SIMD_FORCE_INLINE void* allocate(int size);SIMD_FORCE_INLINE void deallocate();public: btAlignedObjectArray();~btAlignedObjectArray();///Generally it is best to avoid using the copy constructor of an btAlignedObjectArray,// and use a (const) reference to the array instead.btAlignedObjectArray(const btAlignedObjectArray& otherArray); /// return the number of elements in the arraySIMD_FORCE_INLINE int size() const;SIMD_FORCE_INLINE const T& at(int n) const;SIMD_FORCE_INLINE T& at(int n);SIMD_FORCE_INLINE const T& operator[](int n) const;SIMD_FORCE_INLINE T& operator[](int n);///clear the array, deallocated memory. Generally it is better to use array.resize(0), // to reduce performance overhead of run-time memory (de)allocations.SIMD_FORCE_INLINE void clear();SIMD_FORCE_INLINE void pop_back();///resize changes the number of elements in the array. If the new size is larger, // the new elements will be constructed using the optional second argument.///when the new number of elements is smaller, the destructor will be called,// but memory will not be freed, to reduce performance overhead of run-time memory (de)allocations.SIMD_FORCE_INLINE void resizeNoInitialize(int newsize);SIMD_FORCE_INLINE void resize(int newsize, const T& fillData=T());SIMD_FORCE_INLINE T& expandNonInitializing( );SIMD_FORCE_INLINE T& expand( const T& fillValue=T());SIMD_FORCE_INLINE void push_back(const T& _Val);/// return the pre-allocated (reserved) elements, this is at least // as large as the total number of elements,see size() and reserve()SIMD_FORCE_INLINE int capacity() const;SIMD_FORCE_INLINE void reserve(int _Count);class less{ public:bool operator() ( const T& a, const T& b ) { return ( a < b ); }};template <typename L>void quickSortInternal(const L& CompareFunc,int lo, int hi);template <typename L>void quickSort(const L& CompareFunc);///heap sort from http://www.csse.monash.edu.au/~lloyd/tildeAlgDS/Sort/Heap/template <typename L>void downHeap(T *pArr, int k, int n, const L& CompareFunc);void swap(int index0,int index1);template <typename L>void heapSort(const L& CompareFunc);///non-recursive binary search, assumes sorted arrayint findBinarySearch(const T& key) const;int findLinearSearch(const T& key) const;void remove(const T& key);//PCK: whole functionvoid initializeFromBuffer(void *buffer, int size, int capacity);void copyFromArray(const btAlignedObjectArray& otherArray); };

btAlignedObjectArray和std::vector類似。各成員函數(shù)的詳細(xì)實(shí)現(xiàn)這里不再列出。


std::unordered_map的內(nèi)存布局

btHashMap的內(nèi)存布局與我們常見(jiàn)的HashMap的內(nèi)存布局截然不同。為了和btHashMap的內(nèi)存布局對(duì)照,這里先介紹一下std::unordered_map的內(nèi)存布局。

GCC中std::unordered_map僅是對(duì)_Hahstable的簡(jiǎn)單包裝。_Hashtable的數(shù)據(jù)成員定義例如以下:

__bucket_type* _M_buckets;size_type _M_bucket_count;__before_begin _M_bbegin;size_type _M_element_count;_RehashPolicy _M_rehash_policy;當(dāng)中。size_type為std::size_t的typedef;而_RehashPlolicy是詳細(xì)的策略類,僅僅有成員函數(shù)定義,沒(méi)有數(shù)據(jù)成員(這是一種被稱作Policy Based的設(shè)計(jì)范式。詳細(xì)可參閱《Modern C++ Design》,中譯本名為《C++設(shè)計(jì)新思維》。由侯捷先生翻譯)。

繼續(xù)跟蹤_bucket_type,能夠看到(_Hashtable):

using __bucket_type = typename __hashtable_base::__bucket_type;和(__hashtable_base):

using __node_base = __detail::_Hash_node_base;using __bucket_type = __node_base*;

至此。才知道_M_buckets的類型為:_Hash_node_base**

繼續(xù)追蹤。能夠看到_Hash_node_base的定義: /*** struct _Hash_node_base** Nodes, used to wrap elements stored in the hash table. A policy* template parameter of class template _Hashtable controls whether* nodes also store a hash code. In some cases (e.g. strings) this* may be a performance win.*/struct _Hash_node_base{_Hash_node_base* _M_nxt;_Hash_node_base() : _M_nxt() { }_Hash_node_base(_Hash_node_base* __next) : _M_nxt(__next) { }};

從_Hashtable::_M_buckets(二維指針)和_Hash_node_base的_M_nxt的類型(指針)。能夠推測(cè)Hashtable的內(nèi)存布局——buckets數(shù)組存放hash值同樣的node鏈表的頭指針,每一個(gè)bucket上掛著一個(gè)鏈表。

繼續(xù)看__before_begin的類型(_Hashtable):

using __before_begin = __detail::_Before_begin<_Node_allocator_type>;繼續(xù)跟蹤: /*** This type is to combine a _Hash_node_base instance with an allocator* instance through inheritance to benefit from EBO when possible.*/template<typename _NodeAlloc>struct _Before_begin : public _NodeAlloc{_Hash_node_base _M_node;_Before_begin(const _Before_begin&) = default;_Before_begin(_Before_begin&&) = default;template<typename _Alloc>_Before_begin(_Alloc&& __a): _NodeAlloc(std::forward<_Alloc>(__a)){ }};依據(jù)對(duì)STL雙鏈表std::list的了解,能夠推測(cè)Berfore_begin的作用,非常可能和雙鏈表的“頭部的多余的一個(gè)節(jié)點(diǎn)”類似,僅僅是為了方便迭代器(iterator)迭代,通過(guò)_Hashtable::begin()能夠得到驗(yàn)證: iteratorbegin() noexcept{ return iterator(_M_begin()); }__node_type*_M_begin() const{ return static_cast<__node_type*>(_M_before_begin()._M_nxt); }const __node_base&_M_before_begin() const{ return _M_bbegin._M_node; }


實(shí)際存放Value的node類型為以下兩種的當(dāng)中一種(按Hash_node_base的凝視,Key為string時(shí)可能會(huì)用第一種,以提升性能):

/*** Specialization for nodes with caches, struct _Hash_node.** Base class is __detail::_Hash_node_base.*/template<typename _Value>struct _Hash_node<_Value, true> : _Hash_node_base{_Value _M_v;std::size_t _M_hash_code;template<typename... _Args>_Hash_node(_Args&&... __args): _M_v(std::forward<_Args>(__args)...), _M_hash_code() { }_Hash_node*_M_next() const { return static_cast<_Hash_node*>(_M_nxt); }};/*** Specialization for nodes without caches, struct _Hash_node.** Base class is __detail::_Hash_node_base.*/template<typename _Value>struct _Hash_node<_Value, false> : _Hash_node_base{_Value _M_v;template<typename... _Args>_Hash_node(_Args&&... __args): _M_v(std::forward<_Args>(__args)...) { }_Hash_node*_M_next() const { return static_cast<_Hash_node*>(_M_nxt); }};

以下通過(guò)insert源代碼的追蹤,證實(shí)我們對(duì)hashtable內(nèi)存布局的猜想:

_Hashtable::insert:

template<typename _Pair, typename = _IFconsp<_Pair>>__ireturn_typeinsert(_Pair&& __v){__hashtable& __h = this->_M_conjure_hashtable();return __h._M_emplace(__unique_keys(), std::forward<_Pair>(__v));}

_Hashtable::_M_emplace(返回值類型寫得太復(fù)雜,已刪除):

_M_emplace(std::true_type, _Args&&... __args){// First build the node to get access to the hash code__node_type* __node = _M_allocate_node(std::forward<_Args>(__args)...); // 申請(qǐng)鏈表節(jié)點(diǎn) __args為 pair<Key, Value> 類型const key_type& __k = this->_M_extract()(__node->_M_v); // 從節(jié)點(diǎn)中抽取 key__hash_code __code; __try{__code = this->_M_hash_code(__k);}__catch(...){_M_deallocate_node(__node);__throw_exception_again;}size_type __bkt = _M_bucket_index(__k, __code); // 尋找buckets上的相應(yīng)hash code相應(yīng)的indexif (__node_type* __p = _M_find_node(__bkt, __k, __code)) // 在bucket所指鏈表上找到實(shí)際節(jié)點(diǎn){// There is already an equivalent node, no insertion_M_deallocate_node(__node);return std::make_pair(iterator(__p), false);}// Insert the nodereturn std::make_pair(_M_insert_unique_node(__bkt, __code, __node),true);}
_Hashtable::_M_find_node: __node_type*_M_find_node(size_type __bkt, const key_type& __key,__hash_code __c) const{__node_base* __before_n = _M_find_before_node(__bkt, __key, __c);if (__before_n)return static_cast<__node_type*>(__before_n->_M_nxt);return nullptr;}
_Hashtable::_M_find_before_node(返回值類型寫得太復(fù)雜,已刪除): _M_find_before_node(size_type __n, const key_type& __k,__hash_code __code) const{__node_base* __prev_p = _M_buckets[__n]; // 取出頭指針if (!__prev_p)return nullptr;__node_type* __p = static_cast<__node_type*>(__prev_p->_M_nxt);for (;; __p = __p->_M_next()) // 遍歷鏈表{if (this->_M_equals(__k, __code, __p)) // key匹配?return __prev_p;if (!__p->_M_nxt || _M_bucket_index(__p->_M_next()) != __n)break;__prev_p = __p;}return nullptr;}

看到_Hashtable::_M_find_before_node的代碼,就驗(yàn)證了此前我們對(duì)于Hashtable內(nèi)存布局的猜想:這和SGI hash_map的實(shí)現(xiàn)體hashtable的內(nèi)存布局同樣(詳情可參考《STL源代碼剖析》,侯捷先生著)。

(PS:追蹤起來(lái)并不輕松,能夠借助Eclipse等集成開發(fā)環(huán)境進(jìn)行)

比如,std::unordered_map<int, int*>背后的Hashtable的一種可能的內(nèi)存布局例如以下:


std::unordered_map的內(nèi)存布局是大多數(shù)<數(shù)據(jù)結(jié)構(gòu)>、<算法>類教材給出的“標(biāo)準(zhǔn)做法”,也是比較常見(jiàn)的實(shí)現(xiàn)方法。


btHashMap

btHashMap的內(nèi)存布局。與“標(biāo)準(zhǔn)做法”截然不同。例如以下可見(jiàn)btHashMap的數(shù)據(jù)成員(data member)定義:

template <class Key, class Value> class btHashMap {protected:btAlignedObjectArray<int> m_hashTable;btAlignedObjectArray<int> m_next;btAlignedObjectArray<Value> m_valueArray;btAlignedObjectArray<Key> m_keyArray; // ... 省略 };能夠看到,btHashMap的將buckets和key, value全放在一起。它的內(nèi)存布局可能例如以下:

依據(jù)命名。能夠推測(cè):

m_keyArray和m_valueArray分別存放key和value;

m_next的作用應(yīng)該是存放k/v array的index,以此形成鏈表。

m_hashTable的作用應(yīng)該和前面_M_bukets所指向的數(shù)組類似,作為表頭;

以下通過(guò)分析btHashMap的幾個(gè)方法,來(lái)對(duì)這幾個(gè)推測(cè)一一驗(yàn)證。


btHashMap::findIndex

以下來(lái)看看btHashMap::findIndex的實(shí)現(xiàn):

int findIndex(const Key& key) const{unsigned int hash = key.getHash() & (m_valueArray.capacity()-1); // 依賴 Key::getHash()if (hash >= (unsigned int)m_hashTable.size()){return BT_HASH_NULL;}int index = m_hashTable[hash]; // index相當(dāng)于unordered_map的buckets[hash]的鏈表頭指針while ((index != BT_HASH_NULL) && key.equals(m_keyArray[index]) == false) // 遍歷鏈表。直到匹配,依賴 Key::equals(Key){index = m_next[index]; }return index;}

btHashMap::findIndex用到了m_hashTable。m_keyArray,m_next,能夠看出:

m_hashTable的作用確實(shí)類似于unordered_map的buckets數(shù)組;

m_keyArray確實(shí)是存放了key;

m_next[i]確實(shí)類似于unordered_map鏈表節(jié)點(diǎn)的next指針。


btHashMap::insert

接下來(lái)看看btHashMap::insert:

void insert(const Key& key, const Value& value) {int hash = key.getHash() & (m_valueArray.capacity()-1);//replace value if the key is already thereint index = findIndex(key); // 找到了<Key, Value>節(jié)點(diǎn)if (index != BT_HASH_NULL){m_valueArray[index]=value; // 找到了,更行valuereturn;}int count = m_valueArray.size(); // 當(dāng)前已填充數(shù)目int oldCapacity = m_valueArray.capacity();m_valueArray.push_back(value); // value壓入m_valueArray的尾部,capacity可能增長(zhǎng)m_keyArray.push_back(key); // key壓入m_keyArray的尾部int newCapacity = m_valueArray.capacity();if (oldCapacity < newCapacity) {growTables(key); // 假設(shè)增長(zhǎng)。調(diào)整其余兩個(gè)數(shù)組的大小。并調(diào)整頭指針?biāo)谖恢?/hash with new capacityhash = key.getHash() & (m_valueArray.capacity()-1);}m_next[count] = m_hashTable[hash]; // 連同下一行,將新節(jié)點(diǎn)插入 m_hashTable[hash]鏈表頭部m_hashTable[hash] = count;}

這里驗(yàn)證了:m_valueArray存放的確實(shí)是value。


btHashMap::remove

btHashMap與普通Hash表的差別在于,它可能要自己管理節(jié)點(diǎn)內(nèi)存;比方,中間節(jié)點(diǎn)remove掉之后。怎樣保證下次insert可以復(fù)用節(jié)點(diǎn)內(nèi)存?通過(guò)btHashMap::remove可以知道bullet是怎樣實(shí)現(xiàn)的:

void remove(const Key& key) {int hash = key.getHash() & (m_valueArray.capacity()-1);int pairIndex = findIndex(key); // 找到<Key, Value>的 indexif (pairIndex ==BT_HASH_NULL){return;}// Remove the pair from the hash table.int index = m_hashTable[hash]; // 取出頭指針btAssert(index != BT_HASH_NULL);int previous = BT_HASH_NULL;while (index != pairIndex) // 找index的前驅(qū){previous = index;index = m_next[index];}if (previous != BT_HASH_NULL) // 將當(dāng)前節(jié)點(diǎn)從鏈表上刪除{btAssert(m_next[previous] == pairIndex);m_next[previous] = m_next[pairIndex]; // 當(dāng)前節(jié)點(diǎn)位于鏈表中間}else {m_hashTable[hash] = m_next[pairIndex]; // 當(dāng)前節(jié)點(diǎn)是鏈表第一個(gè)節(jié)點(diǎn)}// We now move the last pair into spot of the// pair being removed. We need to fix the hash// table indices to support the move.int lastPairIndex = m_valueArray.size() - 1; // If the removed pair is the last pair, we are done.if (lastPairIndex == pairIndex) // 假設(shè)<Key, Value>已經(jīng)是array的最后一個(gè)元素。則pop_back將減小size(capacity不變){m_valueArray.pop_back();m_keyArray.pop_back();return;}// Remove the last pair from the hash table. 將最后一個(gè)<Key, Value>對(duì)從array上移除int lastHash = m_keyArray[lastPairIndex].getHash() & (m_valueArray.capacity()-1);index = m_hashTable[lastHash];btAssert(index != BT_HASH_NULL);previous = BT_HASH_NULL;while (index != lastPairIndex){previous = index;index = m_next[index];}if (previous != BT_HASH_NULL){btAssert(m_next[previous] == lastPairIndex);m_next[previous] = m_next[lastPairIndex];}else{m_hashTable[lastHash] = m_next[lastPairIndex];}// Copy the last pair into the remove pair's spot. 將最后一個(gè)<Key, Value>復(fù)制到移除pair的空當(dāng)處m_valueArray[pairIndex] = m_valueArray[lastPairIndex];m_keyArray[pairIndex] = m_keyArray[lastPairIndex];// Insert the last pair into the hash table , 將移除節(jié)點(diǎn)插入到m_hashTable[lastHash]鏈表的頭部m_next[pairIndex] = m_hashTable[lastHash];m_hashTable[lastHash] = pairIndex;m_valueArray.pop_back();m_keyArray.pop_back();}

內(nèi)存緊密(連續(xù))的優(yōu)點(diǎn)

btHashMap的這樣的設(shè)計(jì)。可以保證整個(gè)Hash表內(nèi)存的緊密(連續(xù))性。而這樣的連續(xù)性的優(yōu)點(diǎn)主要在于:

第一,能與數(shù)組(指針)式API兼容。比方非常多OpenGL API。由于存在btHashMap內(nèi)的Value和Key在內(nèi)存上都是連續(xù)的。所以這一點(diǎn)非常好理解;

第二,保證了cache命中率(表元素較少時(shí))。因?yàn)槠胀ㄦ湵淼墓?jié)點(diǎn)內(nèi)存是在每次須要時(shí)才申請(qǐng)的,所以基本上不會(huì)連續(xù)。通常不在同樣內(nèi)存頁(yè)。所以,即便是短時(shí)間內(nèi)多次訪問(wèn)鏈表節(jié)點(diǎn),也可能因?yàn)楣?jié)點(diǎn)內(nèi)存分散造成不能將全部節(jié)點(diǎn)放入cache。從而導(dǎo)致訪問(wèn)速度的下降;而btHashMap的節(jié)點(diǎn)內(nèi)存始終連續(xù),因而保證較高的cache命中率,能帶來(lái)一定程度的性能提升。


btAlignedAllocator點(diǎn)評(píng)

btAlignedAllocator定制化接口與std::allocator全然不同。std::allocator的思路是:首先實(shí)現(xiàn)allocator,然后將allocator作為模板參數(shù)寫入詳細(xì)數(shù)據(jù)結(jié)構(gòu)上,如vector<int, allocator<int> >;

這樣的方法盡管能夠?qū)崿F(xiàn)“定制化”,但存在著一定的問(wèn)題:

第一,因?yàn)槿繕?biāo)準(zhǔn)庫(kù)的allcoator用的都是std::allocator。假設(shè)你使用了第二種allocator,程序中就可能存在不止一種類型的內(nèi)存管理方法一起工作的局面;特別是當(dāng)標(biāo)準(zhǔn)庫(kù)使用的是SGI 當(dāng)年實(shí)現(xiàn)的“程序退出時(shí)才歸還全部?jī)?nèi)存的”allocator(詳細(xì)可參閱《STL源代碼剖析》)時(shí),內(nèi)存爭(zhēng)用是不可避免的。

第二。這樣的設(shè)計(jì)無(wú)形中添加了編碼和調(diào)試的復(fù)雜性。相信調(diào)試過(guò)gcc STL代碼的人深有體會(huì)。

而btAlignedAllocator則全然不存在這種問(wèn)題:

第一。它的allocate/deallocate行為通過(guò)全局的函數(shù)指針代理實(shí)現(xiàn),不可能存在同一時(shí)候有兩個(gè)以上的類型底層管理內(nèi)存的方法。

第二。使用btAlignedAllocator的數(shù)據(jù)結(jié)構(gòu),其模板參數(shù)相對(duì)簡(jiǎn)單。編碼、調(diào)試的復(fù)雜性自然也減少了。

本人拙見(jiàn),STL有點(diǎn)過(guò)度設(shè)計(jì)了。盡管Policy Based的設(shè)計(jì)可以帶來(lái)靈活性,但代碼的可讀性下降了非常多(也許開發(fā)glibc++的那群人沒(méi)打算讓別人看他們的代碼?)。


擴(kuò)展閱讀

文中提到了兩本書:

《Modern C++ Design》(中譯本名為《C++設(shè)計(jì)新思維》。侯捷先生譯)。該書仔細(xì)描寫敘述了Policy Based Design。

《STL源代碼剖析》(侯捷先生著)。該書具體剖析了SGI hashtable的實(shí)現(xiàn)。

本文所討論的源代碼版本號(hào):

bullet 2.81

gcc 4.6.1(MinGW)


歡迎評(píng)論或email(xusiwei1236@163.com)交流觀點(diǎn),轉(zhuǎn)載注明出處。勿做商用。

轉(zhuǎn)載于:https://www.cnblogs.com/yxwkf/p/5276686.html

總結(jié)

以上是生活随笔為你收集整理的bullet HashMap 内存紧密的哈希表的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。