From nobody Sun Aug 9 11:00:59 2026 X-Original-To: dev-commits-src-branches@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 4hHw0w6qMLz6nWRK for ; Sun, 09 Aug 2026 11:01:04 +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 4hHw0w5ztwz42Yk for ; Sun, 09 Aug 2026 11:01:04 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1786273264; 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=+Y50RwrHCzJF0Ws7JzLVMOPxB5GnZejI7hzMTQaunsA=; b=fVa5WkB3Jx0tTNAYkZsoMNPThkhWpLAvDVw+iZSyDoQVzqWoW9EpGNO7I2dAjBIhy/FbOO 51FyOsTBnZ/gDKa4DqlUvYBKXGjFgPqwPLC8kqjWQBdsaJZW0tvD74omkjovw5Fr8jUtKx 3/XRvaIQTyyBhET5Q+RsxL4xI3jkgy4fy54lpmqpJQtdZUKAybil3HO6rjdH12UTgmn25d upZVwlgkWxB1iBLO9HUiirvBArZu9lWShPFHwL0mlxEr4aqzITME0ztiR+2EnTKHIiq1E9 pOiBGJ6PTZAzbEwP4YaDBkG0N9zImokgUVe3OdPwokrsVmLta28nmoQmhac/Hw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1786273264; a=rsa-sha256; cv=none; b=M8OlLmujJcfwd7DUgjAal1Oe5tTdkFAdWIMNz4aEy41wI0FDPRI+0kM6gchi+YxVj+lyuA BbJK9FcajNA1uxqCi1TCLL+gg7+5yA/3UNk7Cx5TqAkg3lLVUiv9vehAcdLkMSNA0uWFhT UtQU8HayiGPppKX2zzQbti3BB7nzzbKQRg95Hcfxf85bGdpxbjWIVJttC9Lakz3v6FYehw PBu74+TdcahR2asNn2ywS6M/QFl4TyPdaXtCSa/aSk9D3ASWxrUvpMT//SHFOf0o5FJ6tu o5OuJNtBX8DjFyn/cWBtDlDzYrM3NsLPKVeoROBuEvSj2PcAGjF7iB/gs+cL8w== 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=1786273264; 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=+Y50RwrHCzJF0Ws7JzLVMOPxB5GnZejI7hzMTQaunsA=; b=lq1Ff35HTsA5pNGrDfNcshSKgbSQ44FMHJD5zJaTk/VQc+L2fjLYWhHTT3GmdIux+83bkV y9L1PTnYErhr33ZF5lh3MRqhysuhDQLSVZnigqQ9rpKN46UG+6GUfrU7mkWCmmGTA/XX1M vIwlzufpmhIFG022br0zeZL6QIX61Fa+EJgIm+LTCB15NIhSNaBBlEPzuWppbiGFbPF7z9 BzhF7+lJmXExXapn4F3HGPUEGMnzP6L2fsh4rA8kZt5AKVlfBLvTM4Mhjsxehi3HkZZ/6D 3GamGn3dd0Po6p4F0lchFJEdfFJEIf+R6PUgiGfp5WQW50f1/+ZFPRWcQ7Ptiw== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) by mxrelay.nyi.freebsd.org (Postfix) with ESMTP id 4hHw0w4YzWz4wh for ; Sun, 09 Aug 2026 11:01:04 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from git (uid 1279) (envelope-from git@FreeBSD.org) id 3a9d8 by gitrepo.freebsd.org (DragonFly Mail Agent v0.13+ on gitrepo.freebsd.org); Sun, 09 Aug 2026 11:00:59 +0000 To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org Cc: Minsoo Choo From: Robert Clausecker Subject: git: be97ef6e94a6 - stable/15 - libc/merge.c: use memcpy() for copying List-Id: Commits to the stable branches of the FreeBSD src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-branches List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-branches@freebsd.org Sender: owner-dev-commits-src-branches@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/stable/15 X-Git-Reftype: branch X-Git-Commit: be97ef6e94a65630242f4abb5847f27feca21ffd Auto-Submitted: auto-generated Date: Sun, 09 Aug 2026 11:00:59 +0000 Message-Id: <6a785deb.3a9d8.7a1d1b8b@gitrepo.freebsd.org> The branch stable/15 has been updated by fuz: URL: https://cgit.FreeBSD.org/src/commit/?id=be97ef6e94a65630242f4abb5847f27feca21ffd commit be97ef6e94a65630242f4abb5847f27feca21ffd Author: Minsoo Choo AuthorDate: 2026-07-02 19:47:33 +0000 Commit: Robert Clausecker CommitDate: 2026-08-09 09:29:50 +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 (cherry picked from commit 00a79975c062650ba15e432e30776d42fc44fbaa) --- 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 */