From owner-dev-commits-src-all@freebsd.org Fri Jan 15 14:26:56 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id F09BE4E6AB8; Fri, 15 Jan 2021 14:26:56 +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 "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4DHNmJ6Y2Xz51Cf; Fri, 15 Jan 2021 14:26:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (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 did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D45631D9DE; Fri, 15 Jan 2021 14:26:56 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 10FEQuqL013709; Fri, 15 Jan 2021 14:26:56 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 10FEQuD1013708; Fri, 15 Jan 2021 14:26:56 GMT (envelope-from git) Date: Fri, 15 Jan 2021 14:26:56 GMT Message-Id: <202101151426.10FEQuD1013708@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Ed Maste Subject: git: 13bc5a7358e5 - stable/12 - libc: optimize memmem two-way bad character shift MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: emaste X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: 13bc5a7358e5a39feec12b77b097c70dd111305e Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Jan 2021 14:26:57 -0000 The branch stable/12 has been updated by emaste: URL: https://cgit.FreeBSD.org/src/commit/?id=13bc5a7358e5a39feec12b77b097c70dd111305e commit 13bc5a7358e5a39feec12b77b097c70dd111305e Author: Ed Maste AuthorDate: 2020-11-19 00:02:12 +0000 Commit: Ed Maste CommitDate: 2021-01-15 14:25:35 +0000 libc: optimize memmem two-way bad character shift first, the condition (mem && k < p) is redundant, because mem being nonzero implies the needle is periodic with period exactly p, in which case any byte that appears in the needle must appear in the last p bytes of the needle, bounding the shift (k) by p. second, the whole point of replacing the shift k by mem (=l-p) is to prevent shifting by less than mem when discarding the memory on shift, in which case linear time could not be guaranteed. but as written, the check also replaced shifts greater than mem by mem, reducing the benefit of the shift. there is no possible benefit to this reduction of the shift; since mem is being cleared, the full shift is valid and more optimal. so only replace the shift by mem when it would be less than mem. musl commits: 8f5a820d147da36bcdbddd201b35d293699dacd8 122d67f846cb0be2c9e1c3880db9eb9545bbe38c Obtained from: musl (cherry picked from commit 7dbcd06e63101d51e6a777f7315cfde794411e53) --- lib/libc/string/memmem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/libc/string/memmem.c b/lib/libc/string/memmem.c index 27863b9db623..9e7bf94b1464 100644 --- a/lib/libc/string/memmem.c +++ b/lib/libc/string/memmem.c @@ -153,8 +153,8 @@ twoway_memmem(const unsigned char *h, const unsigned char *z, if (BITOP(byteset, h[l - 1], &)) { k = l - shift[h[l - 1]]; if (k) { - if (mem0 && mem && k < p) - k = l - p; + if (k < mem) + k = mem; h += k; mem = 0; continue;