高性能流媒体服务器-nebula之数据结构(1)--hash table介绍

本文介绍了Nebula流媒体服务器中使用Hash Table作为高速缓存的场景,通过采用Murmur3哈希函数,实现的Hash Table在插入、删除速度上比STL的unordered_map快约2倍,查找效率更高。为了优化内存分配,还自定义了内存池,并确保了该Hash Table的全平台兼容性和自动扩展能力。

hash table在nebula中的使用场景主要用于string的高速缓存,hash函数我们采用了murmur3,经测试,该hash table的插入和删除效率是stl中unordered_map的2倍左右,查找的效率比unordered_map略高。考虑堆内存分配的性能问题,我们自定义了一个内存池以快速分配容器所需的内存,该hash表是自动扩展,是全完全跨平台的, 以下为源代码:

//
//  hashtable.h
//  nebula
//
//  Created by yi.cheng on 15/12/31.
//  Copyright © 2015 kanshansoft. All rights reserved.
//

#ifndef hashtable_h
#define hashtable_h
#include 
#include 
#include 
#include 

namespace anysee {
    template inline uint32_t hashfunc(const K& key) {
        return key.hash();
    }
    template<> inline uint32_t hashfunc(const char* const& key) {
        return hash_murmur2((uint8_t*)key, strlen(key));
    }
    template<> inline uint32_t hashfunc(char* const& key) {
        return hash_murmur2((uint8_t*)key, strlen(key));
    }
    template<> inline uint32_t hashfunc(const uint32_t& key) {
        return nba_inthash_u32(key);
    }
    template<> inline uint32_t hashfunc(const int& key) {
        return nba_inthash_u32((uint32_t)key);
    }
    template<> inline uint32_t hashfunc(const uint64_t& key) {
        return nba_inthash_u6432(key);
    }
    template<> inline uint32_t hashfunc(const int64_t& key) {
        return nba_inthash_u6432((uint64_t)key);
    }
    template  struct HashKeyType {
        typedef K  OrignType;
        typedef K  Type;
    };
    template <> struct  HashKeyType {
        typedef const char*     OrignType;
        typedef anysee::String8 Type;
    };
    template <> struct  HashKeyType {
        typedef char*      OrignType;
        typedef anysee::String8 Type;
    };
    template <> struct HashKeyType {
        typedef IOString        orignType;
        typedef anysee::String8 Type;
    };
    
    template   class  HashTable  {
        HashTable(const HashTable&);
        HashTable& operator = (const HashTable&);
        /*
         * One element in the list.
         */
        class BNode {
        public:
            inline BNode(const K& key, const V& val, uint32_t hkey) : mKey(key), mVal(val), mHKey(hkey), mpNext(NULL) {}
            ~BNode() {}
            inline V& getValue() { return mVal; }
            inline typename HashKeyType::Type& getKey() { return mKey; }
            inline const V& getValue() const { return mVal; }
            inline const typename HashKeyType::Type& getKey() const { return mKey; }
            inline BNode* getNext() const { return mpNext; }
            inline void setVal(const V& val) { mVal = val; }
            inline void setNext(BNode* ptr) { mpNext = ptr; }
        private:
            friend class HashTable;
            typename HashKeyType::Type           mKey;
            V                                 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值