From: Ken Thompson Date: Sat, 20 Nov 2010 23:58:28 +0000 (-0800) Subject: more on dynamic hash in compound literals. X-Git-Tag: weekly.2010-11-23~6 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=8cb8ba14a5c2d196105ba2c47c788610ad8d1971;p=gostls13.git more on dynamic hash in compound literals. thanks to vskrap, andrey mirtchovski, and Eoghan Sherry. R=rsc CC=golang-dev https://golang.org/cl/3245041 --- diff --git a/src/cmd/gc/typecheck.c b/src/cmd/gc/typecheck.c index ec73aebfa5..919d99ecf7 100644 --- a/src/cmd/gc/typecheck.c +++ b/src/cmd/gc/typecheck.c @@ -1809,14 +1809,11 @@ indexdup(Node *n, Node *hash[], ulong nhash) } static int -prime(ulong h) +prime(ulong h, ulong sr) { - ulong n, sr; + ulong n; - sr = h; - for(n=0; n<3; n++) - sr = (sr + h/sr)/2; - for(n=3; nlist; ll; ll=ll->next) h++; - h = 9*h/8; + + // if the auto hash table is + // large enough use it. if(h <= nautohash) { *hash = autohash; memset(*hash, 0, nautohash * sizeof(**hash)); return nautohash; } - while(!prime(h)) - h++; + + // make hash size odd and 12% larger than entries + h += h/8; + h |= 1; + + // calculate sqrt of h + sr = h/2; + for(i=0; i<5; i++) + sr = (sr + h/sr)/2; + + // check for primeality + while(!prime(h, sr)) + h += 2; + + // build and return a throw-away hash table *hash = mal(h * sizeof(**hash)); memset(*hash, 0, h * sizeof(**hash)); return h;