Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Tue, 20 Sep 2022 14:19:05 -0400
From: Rich Felker <>
To: Quentin Rameau <>
Cc:, Florian Weimer <>
Subject: Re: The heap memory performance (malloc/free/realloc) is
 significantly degraded in musl 1.2 (compared to 1.1)

On Tue, Sep 20, 2022 at 08:12:44PM +0200, Quentin Rameau wrote:
> > This is a good thing, we noticed it before. But we actually need a realloc that **can specify the copy range**.
> > 
> > As in our previous example, suppose we "void* p malloc(200KB)" and
> > "malloc_usable_size(p)" returns 256KB. For allocators such as
> > tcmalloc, if we do "realloc(p, 300KB)", it will actually execute
> > "malloc(300KB)+memcpy(**256KB**)+free(256KB)". But at this time,
> > the actual business of the application layer often only needs to
> > copy a small amount of content, such as the first 2KB of data.
> So what you're actually trying to do is more clear now.
> And this is something that you should do on the “application layer”,
> not expect the libc to magically taking care of this.

Exactly. This can be done entirely at the application layer just by
keeping track of the size you allocated. In the above example, the
number 256 kB is a red herring. Yes the
"malloc(300KB)+memcpy(256KB)+free(256KB)" is wasteful, but the
"malloc(300KB)+memcpy(200KB)+free(200KB)" would be comparably wasteful
when you only want to preserve the first 2K, and you can make the
decision that it would be wasteful, and that you instead just want to
allocate a new buffer yourself and memcpy 2K, just by knowing the
original 200KB, without any knowledge of malloc_usable_size.

As a bonus, this will be even faster than the fastest possible
malloc_usable_size implementation.


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.