高性能流媒体服务器-nebula之数据结构(4)--动态数组NBAArray

本文介绍了NBAArray,一种不使用realloc且在数据push操作时提供高效率的动态数组。相比STL中的vector,NBAArray在push操作上展现出近5倍的性能优势,尽管在get操作上稍慢3倍,但在服务器系统特别是流媒体服务器中,由于其内存分配策略,能提供更稳定的性能。
摘要由CSDN通过智能技术生成

NBAArray是一个不使用realloc分配内存的动态数组,当数组动态增长时,我们使用bucket指数级的分配内存,将动态数组的内存分配分散到各个bucket,避免了数组内存分配时的数据复制,使用场景:数据push很频繁的时候。经与stl中的vector比较测试,push时nbaarray比vector高出近5倍的性能,get时比vector慢3倍左右。在服务器系统中,有着非常均衡的稳定性。

//
//  NBAArray.h
//  nebula
//
//  Created by yi.cheng on 16/5/10.
//  Copyright © 2016年 kanshansoft. All rights reserved.
//

#ifndef NBAArray_h
#define NBAArray_h
#include 
   
   
    
    
#include 
    
    
     
     
#include 
     
     
      
      

/******************************************************************************/
/* array */

/**
 * Initial and minimal size of the array expressed as a power of 2.
 * The initial size is 2^NBA_ARRAYOF_BIT.
 */
#define NBA_ARRAYOF_BIT     6

/** \internal
 * Max number of elements as a power of 2.
 */
#define NBA_ARRAYOF_BIT_MAX 32

/**
 * Array container type.
 * \note Don't use internal fields directly, but access the container only using functions.
 */
template
      
      
       
        class NBAArray {
    NBAArray(const NBAArray&);
    NBAArray& operator = (const NBAArray&);
public:
    NBAArray(lfalloctor& alloctor);
    
    const T& operator [](uint32_t index) const;
    T& editItemAt(uint32_t index);
    void push(const T& item);
    void push(T&& item);
    const T& begin() const;
    const T& last() const;
    INLINE uint32_t size() const  {
        return mCount;
    }
    INLINE bool isEmpty() const {
        return (mCount == 0);
    }
    void clear();
    ~NBAArray();
private:
    void*           mBucket[NBA_ARRAYOF_BIT_MAX]; /**< Dynamic array of buckets. */
    size_t          mElementSize;                 /**< Size of the stored element in bytes. */
    uint32_t        mBucketBit;                   /**<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值