メモリプールの速度テスト

動機については前回の日記にて(d:id:studiokingyo:20041210
メモリプールを作ったので、速度をテストしてみた。

1 / MemoryPool deallocate / 1313951.000000
2 / MemoryPool allocate / 7741790.000000
3 / object_pool deallocate / 15908204.000000
4 / object_pool allocate / 61968823.000000

めっさ遅いんですけど...(´З`)チェッ
ヽ(`ε´)ノ ブーブー!!
MemoryPoolはhttp://shinh.skr.jp/loki/SmallObj.htmlより持ってきたソースだ。
ちなみにcrtdbg.hを通してコンパイルするとメモリ開放時にエラーが起こる*1ので
以下のようにした。


/// from Efficient c++ section 6
/**
@note
http://shinh.skr.jp/loki/SmallObj.html
より

*/
#ifndef MEMORY_POOL_H_
#define MEMORY_POOL_H_

#include <memory>
#include <malloc.h>

const int DEFAULT_EXPAND_SIZE = 32;

template <class T_, size_t size_ =DEFAULT_EXPAND_SIZE>
class MemoryPool {
public:
typedef MemoryPool<T_,size_> self_type;
MemoryPool() {
next_ = NULL;
expandTheFreeList(size_);
}
~MemoryPool() {
self_type* t;

for (t = next_; t != 0; t = next_) {
next_ = next_->next_;
free( t );
}
}
void *allocate(){
if (!next_) expandTheFreeList(size_);

self_type* head = next_;
next_ = head->next_;

return head;
}
void deallocate(void* doomed) {
self_type* head =
static_cast<self_type*>(doomed);
head->next_ = next_;
next_ = head;
}

private:
void expandTheFreeList(int howMany);

private:
self_type* next_;
};
template <class T_, size_t size_>
void MemoryPool<T_, size_>::expandTheFreeList(int howMany) {

size_t size = (sizeof(T_) > sizeof(self_type*))
? sizeof(T_) : sizeof(self_type*);

self_type* runner =
(self_type*)malloc(size);

next_ = runner;
for (int i = 0; i < howMany; i++) {
runner->next_ =
(self_type*) malloc(size);
runner = runner->next_;
}
runner->next_ = 0;
}

template <class T_, size_t size_ =DEFAULT_EXPAND_SIZE>
class UseMemoryPool {
public:
void* operator new(size_t size) { return pool_->allocate(size); }
void operator delete(void* doomed, size_t) { pool_->deallocate(doomed); }

public:
static void initMemPool() {
pool_.reset(new MemoryPool<T_, size_>);
}

private:
static std::auto_ptr<MemoryPool<T_, size_> > pool_;

};

template <class T_, size_t size_>
std::auto_ptr<MemoryPool<T_, size_> > UseMemoryPool<T_, size_>::pool_;

#endif // ! MEMORY_POOL_H_


やはり、本家にはかなわぬか・・・ムゥ
どうにかしなければ・・・

残念な結果だがdkutil_cのReleaseはRelease(まだβ版だけど^^;)

とりあえず、私の作ったメモリプールは破棄する事になるだろう・・・。

何故MemoryPoolは速いか?

  • 普通なら数珠繋ぎの所に専用のポインタを使うが、それを使っていないのでメモリを少し節約。かつ、変な分岐をしなくてOKになる?
  • 単純な1方向リスト

何故 私のライブラリが遅いか?

  • 私のメモリプールライブラリはこの数珠繋ぎの所を再利用と証して複雑な処理をしている。
  • その他モロモロの条件処理がある。
  • inline展開にはかなわんのか?

と、言う事でけっこうがんばって作ったメモリ管理ライブラリよ〜
http://dkingyoutility.sourceforge.jp/studiokingyonet/reference/dkutil_c/v0194/dkcMemoryPool_8h.ja.sjis.html
さよ〜なら〜

*1:delete[]なのにdeleteをリンクしてる。完璧にコンパイラ側のミスだと思うのだが・・・