Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Date: Tue, 7 Apr 2020 22:32:29 -0400
From: Rich Felker <dalias@...c.org>
To: musl@...ts.openwall.com
Subject: "Expected behavior" for mallocng @ low usage

I figured as I tune and prepare to integrate the new malloc, it would
be helpful to have a description of what users should see in programs
that make only small use of malloc, since this is the "hard case" to
get right and since any reports of unexpected behavior would be really
useful. For simplicity I'm going to assume 4k pages.

If the program makes at least one "very small" (<= 108 bytes)
allocation, or at least one allocation of certain other sizes smaller
than the page size, you should expect to see a single-page mmap that's
divided up something like a buddy allocator. At the top level it
consists of 2 2032-byte slots, one of which will be broken into two
1008-byte slots, one of which will be broken into 2 496-byte slots.
For "very small" sizes, one of these will in turn be broken up into N
equal-sized slots for the requested size class (N=30, 15, 10, 7, 6, 5,
or 4).

If the page is fully broken up into pairs of 496-byte slots, there are
8 such slots, and only 7 "very small" size classes, so under "very low
usage", all such objects should fit in the single page, even if you
use a multitude of different sizes.

For the next 8 size classes (2 doublings) up to 492 bytes, and
depending on divisibility, a group of 2, 3, 5, or 7 slots will be
created in a slot of size 496, 1008, or 2032. These can use the same
page as the above smaller sizes if there's room available.

Above this size, coarse size classing is used at first (until usage
reaches a threshold) to avoid allocating a large number of many-slot
groups of slightly different sizes that might never be filled. The
next doubling consists only of ranges [493,668] and [669,1004],
allocated in slots of size 2032 in groups of 3 and 2, respectively;
these can use any existing free slot of size 2032. (Once usage has
reached a threshold such that adding a group of 5 or 7 slots doesn't
cause a dramatic relative increase in total usage, finer-grained size
classes will be used.)

At higher sizes, groups of slots are not allocated inside a larger
slot, but as mmaps consisting of a power-of-two number of pages, which
will be split N ways, initially with N=7, 3, 5, or 2 depending on
divisibility. As usage increases, so does the N (doubling the number
of pages used) which reduces potential for vm space fragmentation and
increases the number of slots that can be allocated/freed with fast
paths manipulating free masks.

At present, coarse size classing is used for all these at first, which
can result in significant "waste" but avoids preallocating large (5 or
7) counts of slots that might not ever be used. This is what I'm
presently working to improve by allowing direct individual mmaps in
cases where they can be efficient.

Changes in this area are likely coming soon. Main thing I'm trying to
solve still is getting eagar allocation down even further so that
small programs don't grow significantly when switching to mallocng.

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.