deprecated delete and manual memory management

Timon Gehr timon.gehr at gmx.ch
Thu Apr 28 13:14:48 PDT 2011


Steven Schveighoffer wrote:
> I would need to see what your benchmark is before I'd say it was
> conclusive.

Ok. Version with delete is GC_hints (delete does not give many semantic
guarantees, ergo it is a hint), version without delete is GC. It is also attached
for convenience.

This benchmark tests quite high loads on the GC heap, so for usual applications
the GC won't perform all that bad. But then again, usual applications don't come
with a memory allocation bottleneck.

@Andrei: The benchmark also includes a comparison to malloc/free (version c_heap).

Timon


/* Benchmark GC vs GC & hints vs freelist vs C Heap
 * My measurements on Intel Core Duo 2x2.4 GhZ on Ubuntu 10.04 (lucid) Amd64
 * version=GC:       1m45s
 * version=GC_hints: ~6.8s
 * //using custom allocators:
 * version=freelist: ~3.7s
 * version=c_heap:   ~4.5s
 */


import std.stdio;
import std.c.stdlib;
import std.c.string;
import std.algorithm:swap;


version=GC;//default
//version=GC_hints;
//version=freelist;
//version=c_heap;


version(freelist) version=calldel;
version(GC_hints) version=calldel;
version(c_heap) version=calldel;

class foo{
	char data[1020]=void;
	static foo freelist;
	foo next;
	version(freelist){
		new(size_t s){
			if(freelist !is null){
				void *r=cast(void*)freelist;
				freelist=freelist.next;
				return r;
			}
			return cast(void*)new char[s];//could use malloc(s); effects are similar
		}
		delete(void *p){
			if(p){
				foo oldfl=freelist;
				freelist=cast(foo)p;
				freelist.next=oldfl;
			}
		}
	}
	version(c_heap){
		new(size_t s){return malloc(s);}
		delete(void *p){if(p) free(p);}
	}
};

foo[100000] a;

void main(){
	srand(100);
	int top;
	int dir;//0: expected growing, 1: expected shrinking
	foreach(t;0..10000000){
		if(!top){
			a[top++]=new foo;
			dir=0;
		}else if(top==100000){
			top--;
			version(calldel) delete a[top];
			dir=1;
		}else if(dir^!(rand()%3)){
			top--;
			version(calldel) delete a[top];
		}else a[top++]=new foo;
		if(!rand()%100) for(int i=0;i<100;i++) swap(a[0],a[rand()%top]);//mess around
	}
}
begin 644 benchmark.d
M+RH at 0F5N8VAM87)K($=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O8RAS
M*3L at 969F96-T<R!A<F4@<VEM:6QA<@H)"7T*"0ED96QE=&4H=F]I9"`J<"E[
M"@D)"6EF*'`I>PH)"0D)9F]O(&]L9&9L/69R965L:7-T.PH)"0D)9G)E96QI
M<W0]8V%S="AF;V\I<#L*"0D)"69R965L:7-T+FYE>'0];VQD9FP["@D)"7T*
M"0E]"@E]"@EV97)S:6]N*&-?:&5A<"E["@D);F5W*'-I>F5?="!S*7MR971U
M<FX@;6%L;&]C*',I.WT*"0ED96QE=&4H=F]I9"`J<"E[:68H<"D at 9G)E92AP
M*3M]"@E]"GT["@IF;V];,3`P,#`P72!A.PH*=F]I9"!M86EN*"E["@ES<F%N
M9"@Q,#`I.PH):6YT('1O<#L*"6EN="!D:7([+R\P.B!E>'!E8W1E9"!G<F]W
M:6YG+"`Q.B!E>'!E8W1E9"!S:')I;FMI;F<*"69O<F5A8V at H=#LP+BXQ,#`P
M,#`P,"E["@D):68H(71O<"E["@D)"6%;=&]P*RM=/6YE=R!F;V\["@D)"61I
M<CTP.PH)"7UE;'-E(&EF*'1O<#T],3`P,#`P*7L*"0D)=&]P+2T["@D)"79E
M<G-I;VXH8V%L;&1E;"D at 9&5L971E(&%;=&]P73L*"0D)9&ER/3$["@D)?65L
M<V4@:68H9&ER7B$H<F%N9"@I)3,I*7L*"0D)=&]P+2T["@D)"79E<G-I;VXH
M8V%L;&1E;"D at 9&5L971E(&%;=&]P73L*"0E]96QS92!A6W1O<"LK73UN97<@
M9F]O.PH)"6EF*"%R86YD*"DE,3`P*2!F;W(H:6YT(&D],#MI/#$P,#MI*RLI
M('-W87`H85LP72QA6W)A;F0H*25T;W!=*3LO+VUE<W, at 87)O=6YD"@E]"GT*
`
end


More information about the Digitalmars-d mailing list