From patchwork Fri Nov 25 20:09:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Siddhesh Poyarekar X-Patchwork-Id: 61114 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E4E553839D98 for ; Fri, 25 Nov 2022 20:10:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E4E553839D98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1669407020; bh=7kPxN8aSEQoxpm0ma3EYWAPkdLLtgxlkNGs3w2JcnnE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Uojx5SST6/ffNyfYI/XX8Wi1m55Bsfvj50LUlwBUSP5yq4f+f/oqqi/jx8ba11mg6 DehQuYptcXGal+b4tR4GYfmPjdcR4l3uSv4KrcD+tAQH4rS7xqCkjU/tUi0pADSqjY URGhK4ud3J39rYuEsdbi7treK4/kfEcel1T5VhDI= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from hedgehog.birch.relay.mailchannels.net (hedgehog.birch.relay.mailchannels.net [23.83.209.81]) by sourceware.org (Postfix) with ESMTPS id AB7C738381C1 for ; Fri, 25 Nov 2022 20:09:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AB7C738381C1 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 1E815761A60 for ; Fri, 25 Nov 2022 20:09:57 +0000 (UTC) Received: from pdx1-sub0-mail-a304.dreamhost.com (unknown [127.0.0.6]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id 4C0267617DD for ; Fri, 25 Nov 2022 20:09:56 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1669406996; a=rsa-sha256; cv=none; b=L8DnwXlAYceA7kGCn/tK8pXPhfaT031WCcQrl/c6yV1wldfGyRgZjQrqF7TSWFhbpUaHjh BqWHPAeGipyeyokABwzxhWrXadWHDmayOLVvZQ3hsZoYBHZ0OMhfs6d9QTf6v+T23Gwfxj cypfnGU6zqUnr2isDeqw8DnuVwc01aab5Yb6pUhqaJ+mGi6My83vxl/VupOlR/RBckfG1u xezVAfEzVpfaJmQRDfYNgYCXOd+YVjV2i0ETdt6rRVYSVKh56AlTeDK255mj+BfG28qOgl AMABW3ZYZ2kj4qc+eBtSY8VbOSEj26EV+8guSRXS6GJK4MCPquiwRDmS7ZEZhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1669406996; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=7kPxN8aSEQoxpm0ma3EYWAPkdLLtgxlkNGs3w2JcnnE=; b=2iOXD+OpfhDpDljQ7yPSmQDw8N/XU7cIwF8EZ24fflSwThoAHXDAabrVg4BgtPJtYWTBXY IJqoGMo3up5EFZUU6ZfYycBZB8fVJq6+vH4hqqNJ04ClM3KJKBphVjyA9CbOneoZwgX2KP n3ULODjdb2ihuJPy/Fz1Ic0blWrpV6awWfvcEWhXWKg1ip1teZK9bz1Yo02fXhBT5GpUhc 30Zqmnus8+WHuSzOMSG5ILWgDrrKobyZYmWxD7caQbB8LmV8f9Nh4cZKk+UQOhiU24FElM VUACX32lGCIchZviRU4eUJSzSo4zRHZmCrwzL/Hxr676Qtd4sa8wH3EAHrfmDQ== ARC-Authentication-Results: i=1; rspamd-77fc559df8-mqrgz; auth=pass smtp.auth=dreamhost smtp.mailfrom=siddhesh@sourceware.org X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Absorbed-Absorbed: 10e57cc2562c08ab_1669406996527_2806299989 X-MC-Loop-Signature: 1669406996527:685925887 X-MC-Ingress-Time: 1669406996527 Received: from pdx1-sub0-mail-a304.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.103.24.54 (trex/6.7.1); Fri, 25 Nov 2022 20:09:56 +0000 Received: from fedora.redhat.com (bras-base-toroon4834w-grc-23-76-68-24-147.dsl.bell.ca [76.68.24.147]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a304.dreamhost.com (Postfix) with ESMTPSA id 4NJmDg6L3KzHm for ; Fri, 25 Nov 2022 12:09:55 -0800 (PST) To: libc-alpha@sourceware.org Subject: [PATCH] realloc: Return unchanged if request is within usable size Date: Fri, 25 Nov 2022 15:09:48 -0500 Message-Id: <20221125200948.2399996-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.38.1 MIME-Version: 1.0 X-Spam-Status: No, score=-1173.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Siddhesh Poyarekar via Libc-alpha From: Siddhesh Poyarekar Reply-To: Siddhesh Poyarekar Errors-To: libc-alpha-bounces+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" If there is enough space in the chunk to satisfy the new size, return the old pointer as is, thus avoiding any locks or reallocations. The only real place this has a benefit is in large chunks that tend to get satisfied with mmap, since there is a large enough spare size (up to a page) for it to matter. For allocations on heap, the extra size is typically barely a few bytes (up to 15) and it's unlikely that it would make much difference in performance. Also added a smoke test to ensure that the old pointer is returned unchanged if the new size to realloc is within usable size of the old pointer. Signed-off-by: Siddhesh Poyarekar --- malloc/malloc.c | 6 ++++++ malloc/tst-realloc.c | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/malloc/malloc.c b/malloc/malloc.c index 2a61c8b5ee..3ef61bef34 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -1100,6 +1100,8 @@ static void munmap_chunk(mchunkptr p); static mchunkptr mremap_chunk(mchunkptr p, size_t new_size); #endif +static size_t musable (void *mem); + /* ------------------ MMAP support ------------------ */ @@ -3396,6 +3398,10 @@ __libc_realloc (void *oldmem, size_t bytes) if (__glibc_unlikely (mtag_enabled)) *(volatile char*) oldmem; + /* If there's usable space in the current chunk, return as is. */ + if (bytes <= musable (oldmem)) + return oldmem; + /* chunk corresponding to oldmem */ const mchunkptr oldp = mem2chunk (oldmem); /* its size */ diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c index 5eb62a770f..54dd0dd0b3 100644 --- a/malloc/tst-realloc.c +++ b/malloc/tst-realloc.c @@ -142,6 +142,27 @@ do_test (void) free (p); + /* Smoke test to make sure that allocations do not move if they have enough + space to expand in the chunk. */ + for (size_t sz = 3; sz < 256 * 1024; sz += 2048) + { + p = realloc (NULL, sz); + if (p == NULL) + FAIL_EXIT1 ("realloc (NULL, 31) returned NULL."); + size_t newsz = malloc_usable_size (p); + printf ("size: %zu, usable size: %zu, extra: %zu\n", + sz, newsz, newsz - sz); + void *new_p = realloc (p, newsz); + if (new_p != p) + FAIL_EXIT1 ("Expanding (%zu bytes) to usable size (%zu) moved block", + sz, newsz); + free (new_p); + + /* We encountered a large enough extra size at least once. */ + if (newsz - sz > 1024) + break; + } + return 0; }