Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <2022092101393822582117@gmail.com>
Date: Wed, 21 Sep 2022 01:39:39 +0800
From: baiyang <baiyang@...il.com>
To: "James Y Knight" <jyknight@...gle.com>, 
	musl <musl@...ts.openwall.com>
Cc: "Florian Weimer" <fweimer@...hat.com>, 
	"Rich Felker" <dalias@...c.org>
Subject: Re: Re: The heap memory performance (malloc/free/realloc) is significantly degraded in musl 1.2 (compared to 1.1)

> tcmalloc implements similar functionality, as well, with family of functions named "tcmalloc_size_returning_operator_new"

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.

Therefore, it would be great if there were additional parameters to inform realloc to just execute malloc(300KB)+memcpy(**2KB**)+free(256KB) when degenerating back to malloc+memcpy+free.
 
--

   Best Regards
  BaiYang
  baiyang@...il.com
  http://i.baiy.cn
**** < END OF EMAIL > **** 
 
 
From: James Y Knight
Date: 2022-09-21 00:59
To: musl
CC: Florian Weimer; Rich Felker; baiyang
Subject: Re: [musl] The heap memory performance (malloc/free/realloc) is significantly degraded in musl 1.2 (compared to 1.1)
On Tue, Sep 20, 2022 at 9:58 AM Siddhesh Poyarekar <siddhesh@...hat.com> wrote:
Adding support for something that's already declared as bad
programming practice seems like a step backwards.  Instead, I hope we
find a way to discourage active use of malloc_usable_size more
strongly.  

BTW, if folks aren't aware, there is already work on the C++ side to expose an API which lets you request a heap allocation of _at least_ the given size, which rounds the actual size up in whatever way the allocator likes, and returns the pointer and actual size allocated. With this API, you declare an explicit intent that all of the memory -- up to the returned size -- is valid to use without needing to go back to the allocator to ask for more.

The proposal is still making its way through the standardization process, but hopefully it'll make it into the next version of C++ after C++23.  (Of course, that's not a sure thing until it happens.) Here's the doc, with more rationale/etc:
  https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p0901r9.html

Also, as noted in the doc, jemalloc experimentally implemented this functionality in its non-standard API, via a function it called "smallocx" -- though jemalloc hides the API so it can't be used by default. The API is effectively:
  typedef struct { void *ptr; size_t size; } smallocx_return_t;
  smallocx_return_t smallocx(size_t size, int flags);
https://github.com/jemalloc/jemalloc/blob/a0734fd6ee326cd2059edbe4bca7092988a63684/src/jemalloc.c#L3414
(That's consistent with jemalloc's other non-standard APIs, which stick alignment/etc into a "flags" argument, but probably not suitable for a more-standardized cross-implementation API)

tcmalloc implements similar functionality, as well, with family of functions named "tcmalloc_size_returning_operator_new":
  https://github.com/google/tcmalloc/blob/267aa2ec2817ab9d09b3fbb65ecb90193dd4348e/tcmalloc/malloc_extension.h#L549
which of course also isn't a suitable API to support cross-implementation.

If someone wants to push forward this area, IMO, it would be really great to have an API exposing this functionality designed to be implemented in a common way across libc malloc implementations -- and eventually added to POSIX or C.



Content of type "text/html" skipped

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.