From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.org/gmane.linux.lib.musl.general/525 Path: news.gmane.org!not-for-mail From: Szabolcs Nagy Newsgroups: gmane.linux.lib.musl.general Subject: unneeded mremap calls in realloc Date: Wed, 16 Nov 2011 01:45:37 +0100 Message-ID: <20111116004536.GV24939@port70.net> Reply-To: musl@lists.openwall.com NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1321404355 3588 80.91.229.12 (16 Nov 2011 00:45:55 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 16 Nov 2011 00:45:55 +0000 (UTC) To: musl@lists.openwall.com Original-X-From: musl-return-526-gllmg-musl=m.gmane.org@lists.openwall.com Wed Nov 16 01:45:51 2011 Return-path: Envelope-to: gllmg-musl@lo.gmane.org Original-Received: from mother.openwall.net ([195.42.179.200]) by lo.gmane.org with smtp (Exim 4.69) (envelope-from ) id 1RQTdO-0007Qt-Uh for gllmg-musl@lo.gmane.org; Wed, 16 Nov 2011 01:45:51 +0100 Original-Received: (qmail 5480 invoked by uid 550); 16 Nov 2011 00:45:49 -0000 Mailing-List: contact musl-help@lists.openwall.com; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Original-Received: (qmail 5469 invoked from network); 16 Nov 2011 00:45:49 -0000 Content-Disposition: inline User-Agent: Mutt/1.5.20 (2009-06-14) Xref: news.gmane.org gmane.linux.lib.musl.general:525 Archived-At: as discussed on irc, in realloc there is a mremap where newlen is pagesize adjusted but oldlen is not so oldlen==newlen almost always fails run this simple test case with strace to see the issue: #include int main(){ char *p = 0; int n; for (n = 0; n < 500000; n++) p = realloc(p, n); free(p); return 0; } the fix that significantly speeds up the above code: (there might be better fix, eg why oldlen is not a multiple of pagesize in the first place?) diff --git a/src/malloc/malloc.c b/src/malloc/malloc.c index abf3e8f..8e9b022 100644 --- a/src/malloc/malloc.c +++ b/src/malloc/malloc.c @@ -401,7 +401,7 @@ void *realloc(void *p, size_t n) return new; } newlen = (newlen + PAGE_SIZE-1) & -PAGE_SIZE; - if (oldlen == newlen) return p; + if (((oldlen + PAGE_SIZE-1) & -PAGE_SIZE) == newlen) return p; base = __mremap(base, oldlen, newlen, MREMAP_MAYMOVE); if (base == (void *)-1) return newlen < oldlen ? p : 0;