Hi,
This diff implements a tradeoff to gain speed at the cost of reducing
the randomness of chunk allocation in malloc slightly.
The idea is only to randomize the first half of chunks in a page. The
second half of chunks will fill in the gaps in-order. The
effectiveness of the current randomization decreases already with the
number of free slots diminishing in a page.
In one test case, this diff reduced the runtime from 31s to 25s. I'm
not completely sure if the reduced randomness is acceptable. But if
not, I like to commit the alternative mentioned in the comment since
"i" will always be 0 in that case. That alone already gives me a
measurable performance gain.
-Otto
Index: malloc.c
===================================================================
RCS file: /cvs/src/lib/libc/stdlib/malloc.c,v
retrieving revision 1.127
diff -u -p -r1.127 malloc.c
--- malloc.c 16 Dec 2010 18:47:01 -0000 1.127
+++ malloc.c 26 Apr 2011 13:24:17 -0000
@@ -1025,23 +1025,27 @@ malloc_bytes(struct dir_info *d, size_t
k++;
}
- /* advance a random # of positions */
- i = getrnibble() % bp->free;
- while (i > 0) {
- u += u;
- k++;
- if (k >= MALLOC_BITS) {
- lp++;
- u = 1;
- k = 0;
+ /* advance a random # of positions if we didn't fill much yet */
+ /* alternatively if (bp->free > 1) { */
+ if (bp->free > 1 && (mopts.malloc_guard || bp->free > bp->total / 2)) {
+ i = getrnibble() % bp->free;
+ while (i > 0) {
+ u += u;
+ k++;
+ if (k >= MALLOC_BITS) {
+ lp++;
+ u = 1;
+ k = 0;
+ if (lp - bp->bits > (bp->total - 1) /
+ MALLOC_BITS) {
+ wrterror("chunk overflow", NULL);
+ errno = EFAULT;
+ return (NULL);
+ }
+ }
+ if (*lp & u)
+ i--;
}
- if (lp - bp->bits > (bp->total - 1) / MALLOC_BITS) {
- wrterror("chunk overflow", NULL);
- errno = EFAULT;
- return (NULL);
- }
- if (*lp & u)
- i--;
}
*lp ^= u;