Skip to content

Commit

Permalink
b-allocator: nd_allocator is x30 faster
Browse files Browse the repository at this point in the history
*********************  N= 500000       nd-cap= 200000000
nd_allocator:     12421475
seq new:          108782520
seq delete:       40186652
random new+del:   316008715
  • Loading branch information
lvv committed Feb 13, 2012
1 parent d218cdd commit 3e8a6a5
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ stdc++.h.gch:

CLEAN_LIST += t-print t-regex t-meta

CXXFLAGS= -std=gnu++11 -Wall -I/home/lvv/p/
CXXFLAGS += -std=gnu++11 -Wall -I/home/lvv/p/

t-buf-r: t-buf

######################################################## SCCPP
Expand Down
2 changes: 1 addition & 1 deletion allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct trace_allocator : std::allocator<T> {

template<typename T> // NO DELETE ALLOCATOR
struct nd_allocator : std::allocator<T> {
static const size_t capacity = 500*1000*1000; // 500MB
static const size_t capacity = 200*1000*1000; // 500MB
static char *data;
static char *b; // free begin
static char *e; // end of data buffer;
Expand Down
61 changes: 53 additions & 8 deletions b-allocator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

int main() {


const size_t N = 20000;
#ifdef NDEBUG
const size_t N = 500000;
#else
const size_t N = 100;
#endif
char *P[N] = { nullptr };

nd_allocator<char> Al;
Expand All @@ -19,19 +22,61 @@ int main() {
///// ND_ALLOCATOR
*Al.data = 'a';
tm.reset();
pALL(P) { *p = Al.allocate((p-P)%27); **p='a'; }
pALL(P) { *p = Al.allocate((p-P)%27+1); **p='a'; }
__ "nd_allocator: ", tm.interval_ticks(), flush;

//pALL(P) delete[] *p;
//__ "delete:\t", tm.interval_ticks();


///// NEW
///// BUILDINT NEW sequential
tm.reset();
pALL(P) { *p = new char[(p-P)%27+1]; **p='a'; }
__ "seq new: ", tm.interval_ticks(), flush;
pALL(P) { delete[] *p; *p = nullptr; }
__ "seq delete: ", tm.interval_ticks(), flush;

///// BUILDINT NEW random

// GENERATE LOG
// log of allocation/deallocation
// x=0: uninitilized
// x>0: index of subsequent delete (where pointer will be stored)
// x<0: negative of index of predeceasing allocate (for assert) + 1

int L[2*N]{};
int l=0, ll; // log index to allocate and de-allocate
iFOR(N) {
while(ll = l + rand()%(2*N-1-l) + 1, L[ll]); // find free log entry for de-allocate
while(L[l]) ++l; // find free log entry for allocation
assert(!L[l] && !L[ll]); //__ i,l,ll;
L[l++] = i; // rec alloc
L[ll]=-i-1; // rec de-alloc
};
//__ L;



///// PLAY LOG

tm.reset();
pALL(P) { *p = new char[(p-P)%27]; **p='a'; }
__ "new: ", tm.interval_ticks(), flush;
pALL(P) delete[] *p;
__ "delete: ", tm.interval_ticks(), flush;
lALL(L) {
int i=L[l];
if (i>=0) { assert(!P[i]);
P[i] = new char[rand()%27 + 1];
*P[i] = 'a';
} else {
int j= -i-1; assert(*P[j]=='a');
delete[] P[j];
P[j] = nullptr;
}
}

pALL(P) assert(p==nullptr);

__ "random new+del: ", tm.interval_ticks(), flush;


///// ND cleanup
atexit( []{ delete[] nd_allocator<char>::data; });
}
7 changes: 7 additions & 0 deletions cj.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
///////////////////////////////////////////////////////////////////// SHORTCUTS

///// types
typedef const int cint;
typedef std::vector<int> vint;
typedef std::vector<unsigned int> vuint;
typedef std::vector<long> vlong;
Expand Down Expand Up @@ -67,8 +68,14 @@ typedef std::list<std::string> lstr;
#define LLA(IT, C) for (auto IT=end(C)-1; IT >= begin(C); IT--)
#define itALL(C) ALL(it,C)
#define pALL(C) ALL(p,C)
#define qALL(C) ALL(q,C)
#define pLLA(C) LLA(p,C)
#define iALL(C) for (long i=0; i<(long)(end(C)-begin(C)); i++)
#define jALL(C) for (long j=0; j<(long)(end(C)-begin(C)); j++)
#define kALL(C) for (long k=0; k<(long)(end(C)-begin(C)); k++)
#define lALL(C) for (long l=0; l<(long)(end(C)-begin(C)); l++)
#define mALL(C) for (long m=0; m<(long)(end(C)-begin(C)); m++)
#define nALL(C) for (long n=0; n<(long)(end(C)-begin(C)); n++)
#define cALL(C) for (auto c:C) if(c == '\0') break; else
#define xALL(C) for (auto x:C)

Expand Down
1 change: 1 addition & 0 deletions index.txt
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ FORMATS
http://www.kirit.com/Thread:/1723074
http://stackoverflow.com/questions/245973/whats-the-best-c-json-parser
http://stackoverflow.com/a/8560856
https://github.com/udp/json-parser

CSV
http://csvkit.readthedocs.org/en/latest/index.html
Expand Down

0 comments on commit 3e8a6a5

Please sign in to comment.