Anton Lindqvist wrote:
> Hi,
> If the elements argument passed to hashinit() is a power of 2 there's no
> need to find the closest power of 2 that can fit all elements since
> elements == hashsize will always be true. During boot of a stock amd64
> kernel running inside vmd 80% of the calls to hashinit() includes a
> power of 2 size.
>
> Comments? OK?
Hi,
Dunno how much a win it is. But anyhow this will blow in hashfree as
hashsize won't be the same.
Mathieu.
>
> Index: kern/kern_subr.c
> ===================================================================
> RCS file: /cvs/src/sys/kern/kern_subr.c,v
> retrieving revision 1.49
> diff -u -p -r1.49 kern_subr.c
> --- kern/kern_subr.c 14 Feb 2017 10:31:15 -0000 1.49
> +++ kern/kern_subr.c 28 Apr 2018 20:21:40 -0000
> @@ -163,8 +163,11 @@ hashinit(int elements, int type, int fla
>
> if (elements <= 0)
> panic("hashinit: bad cnt");
> - for (hashsize = 1; hashsize < elements; hashsize <<= 1)
> - continue;
> + if ((elements & (elements - 1)) == 0)
> + hashsize = elements;
> + else
> + for (hashsize = 1; hashsize < elements; hashsize <<= 1)
> + continue;
> hashtbl = mallocarray(hashsize, sizeof(*hashtbl), type, flags);
> if (hashtbl == NULL)
> return NULL;
>