From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.0 required=5.0 tests=MAILING_LIST_MULTI, RCVD_IN_MSPIKE_H2 autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 5340 invoked from network); 20 Sep 2022 18:19:24 -0000 Received: from second.openwall.net (193.110.157.125) by inbox.vuxu.org with ESMTPUTF8; 20 Sep 2022 18:19:24 -0000 Received: (qmail 25828 invoked by uid 550); 20 Sep 2022 18:19:21 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-ID: Reply-To: musl@lists.openwall.com Received: (qmail 25808 invoked from network); 20 Sep 2022 18:19:20 -0000 Date: Tue, 20 Sep 2022 14:19:05 -0400 From: Rich Felker To: Quentin Rameau Cc: musl@lists.openwall.com, Florian Weimer Message-ID: <20220920181905.GR9709@brightrain.aerifal.cx> References: <2022091915532777412615@gmail.com> <20220919110829.GA2158779@port70.net> <874jx3h76u.fsf@oldenburg.str.redhat.com> <20220919134659.GO9709@brightrain.aerifal.cx> <874jx2phqm.fsf@oldenburg.str.redhat.com> <2022092101393822582117@gmail.com> <20220920201244.4f40362e.quinq@fifth.space> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20220920201244.4f40362e.quinq@fifth.space> User-Agent: Mutt/1.5.21 (2010-09-15) 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 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. Rich