From nobody Sun Aug 2 12:10:50 2026 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4hCdtf4XSbz6nQHs for ; Sun, 02 Aug 2026 12:10:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (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 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "YR1" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 4hCdtf3Kjtz3kLF for ; Sun, 02 Aug 2026 12:10:50 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1785672650; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hJIIqkRw9ypNS+HuDX8WU330LDWDHjhczL/7UQ+eNRc=; b=oa3mQ/j0yZYwuvQSIeP4/n+Z6NSACedG8XRgxUwNPgIVg1J4ZB77HyW4/BOqdV+69mf+Fy 7c0evAgCj6AoSWOOzI0FKmtAKg4M1DY8YAoevdfA0/d9xsl0nsAmVd6ij48mekPNGUhi2Y N5Yw4cD8oqZyc5gRd2k3ldBFrPM6nrvWcErhCtAlAwoVwaWZZmeSvNSIVfDzAlf6RT1a1Z Ve8jQm6koanbhVuoyVT6z13ogJFaUxIl9rE9KtcphE6dYccSe8PoN2IUAPYMxoxKFgSaTe Fz0Kz/3A1snJtraY58RX6Qg+6iYyGIigXWp0UX7BkmyUVLnmmjvTJr1CbzdaKA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1785672650; a=rsa-sha256; cv=none; b=avK5awu4zF9UDE5PPjQ1EJMH5IEZn7xIm7PVUWQQTP11NakBgR8oCCHV2L1ifpGODuBWYm ARbCHU8WsQZJBvBHHp4ioX/YS++HuztRBfIwjzCCqXIyqIRSdavJlGRrSdZi07nSXiMJe4 H9KD87c+EuLjtGZpiNq+VjFSOg4dGtQ1gh83qlIZzgv0oy7cFcAHnymDbIR8ddm6x4reAo C6daUv7y/t+/sOxUpOjP1jL45cbac/CFFitiYVV31mt4P/cbEuZH/Obo13djLmRvib5iRo jr3001cvbocIGpIhzEo5jtMHUbP/5efDzo7p9CTro+6MvHApXokyjLEINi1UZA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1785672650; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=hJIIqkRw9ypNS+HuDX8WU330LDWDHjhczL/7UQ+eNRc=; b=H1DsjjaCsQz7xBSV3tTAbppdm1ERVMorgfVta1izVLO8B1mMkq2fw80tHMULnO2Cdp//7H 4U16WbeM0/VyCeqTCqgYjP1laapXrnhc1NtOgpSfq/nDcNcJXenT6YOPrwq+1uC8q4uFNI 2Gq3OgIDPzLSiVfBRo38leljjxyMVrxGdLkEVWEYqpXAnkYp5V9POY6KT7TzniRSkkYen9 NltEMHssQpprkzhROcD/J84Iw5+LamwVrS8TmelFLWJH/cTkbgnyYvDYXWBmuUD7jQn+Eu /ruiR1QG1jJe7Wfm7XljT0urfuhcMlyr0df6oWl7n0v8jAKglKeK9IQxODQG5Q== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hCdtf1HV7z1SF7 for ; Sun, 02 Aug 2026 12:10:50 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 1f0f3 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sun, 02 Aug 2026 12:10:50 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org Cc: Minsoo Choo From: Robert Clausecker Subject: git: 00a79975c062 - main - libc/merge.c: use memcpy() for copying List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: fuz X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 00a79975c062650ba15e432e30776d42fc44fbaa Auto-Submitted: auto-generated Date: Sun, 02 Aug 2026 12:10:50 +0000 Message-Id: <6a6f33ca.1f0f3.c2ca741@gitrepo.freebsd.org> The branch main has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=00a79975c062650ba15e432e30776d42fc44fbaa commit 00a79975c062650ba15e432e30776d42fc44fbaa Author: Minsoo Choo AuthorDate: 2026-07-02 19:47:33 +0000 Commit: Robert Clausecker CommitDate: 2026-08-02 11:17:45 +0000 libc/merge.c: use memcpy() for copying Currently mergesort() uses ICOPY_*() to copy data as four byte blocks instead of one byte. However, this is only achievable when both size and base arguments are aligned to four bytes. Use of memcpy() is ideal as 1) it is cleaner and 2) the library will use SIMD for copying when the hardware supports it. Compared to ICOPY_*(), SIMD can support up to 64 bytes. When the SIMD-backed memcpy() find the address is unaligned, it can first copy data up to the nearest aligned address, and then use SIMD operations for faster transfer. Thus memcpy() can give better performance than mergesort()'s own implementation. This is benchmarked on amd64 where there isn't a SIMD-backed implementation yet. However, the baseline implementation in assembly already delivers better performance in unaligned cases although there is some performance drops in aligned cases. The benchmark results and script is available in the Phabricator review. Ideally, more performance improvements will come when amd64 gets SIMD implementation of memcpy(). Signed-off-by: Minsoo Choo Reviewed by: fuz MFC after: 1 week Differential Revision: https://reviews.freebsd.org/D58002 --- lib/libc/stdlib/merge.c | 69 +++++++++++++++---------------------------------- 1 file changed, 21 insertions(+), 48 deletions(-) diff --git a/lib/libc/stdlib/merge.c b/lib/libc/stdlib/merge.c index e07a3947e741..7c15fa7953f9 100644 --- a/lib/libc/stdlib/merge.c +++ b/lib/libc/stdlib/merge.c @@ -66,25 +66,19 @@ typedef int (*cmp_t)(const void *, const void *); static void setup(u_char *, u_char *, size_t, size_t, cmp_t); static void insertionsort(u_char *, size_t, size_t, cmp_t); -#define ISIZE sizeof(int) #define PSIZE sizeof(u_char *) -#define ICOPY_LIST(src, dst, last) \ - do \ - *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \ - while(src < last) -#define ICOPY_ELT(src, dst, i) \ - do \ - *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \ - while (i -= ISIZE) - -#define CCOPY_LIST(src, dst, last) \ - do \ - *dst++ = *src++; \ - while (src < last) -#define CCOPY_ELT(src, dst, i) \ - do \ - *dst++ = *src++; \ - while (i -= 1) +#define COPY_LIST(src, dst, last) \ + do { \ + memcpy(dst, src, last - src); \ + dst += last - src; \ + src += last - src; \ + } while (0) +#define COPY_ELT(src, dst, i) \ + do { \ + memcpy(dst, src, i); \ + src += i; \ + dst += i; \ + } while (0) /* * Find the next possible pointer head. (Trickery for forcing an array @@ -112,7 +106,7 @@ mergesort(void *base, size_t nmemb, size_t size, cmp_t cmp) { size_t i, nbytes, asize; int sense; - int big, iflag; + int big; u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2; u_char *list2, *list1, *p2, *p, *last, **p1; @@ -129,10 +123,6 @@ mergesort(void *base, size_t nmemb, size_t size, cmp_t cmp) return (-1); } - iflag = 0; - if (__is_aligned(size, ISIZE) && __is_aligned(base, ISIZE)) - iflag = 1; - if ((list2 = malloc(asize)) == NULL) return (-1); @@ -200,34 +190,17 @@ COPY: b = t; } i = size; if (q == f1) { - if (iflag) { - ICOPY_LIST(f2, tp2, b); - ICOPY_ELT(f1, tp2, i); - } else { - CCOPY_LIST(f2, tp2, b); - CCOPY_ELT(f1, tp2, i); - } + COPY_LIST(f2, tp2, b); + COPY_ELT(f1, tp2, i); } else { - if (iflag) { - ICOPY_LIST(f1, tp2, b); - ICOPY_ELT(f2, tp2, i); - } else { - CCOPY_LIST(f1, tp2, b); - CCOPY_ELT(f2, tp2, i); - } + COPY_LIST(f1, tp2, b); + COPY_ELT(f2, tp2, i); } } - if (f2 < l2) { - if (iflag) - ICOPY_LIST(f2, tp2, l2); - else - CCOPY_LIST(f2, tp2, l2); - } else if (f1 < l1) { - if (iflag) - ICOPY_LIST(f1, tp2, l1); - else - CCOPY_LIST(f1, tp2, l1); - } + if (f2 < l2) + COPY_LIST(f2, tp2, l2); + else if (f1 < l1) + COPY_LIST(f1, tp2, l1); *p1 = l2; } tp2 = list1; /* swap list1, list2 */