From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pj1-f44.google.com (mail-pj1-f44.google.com [209.85.216.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AE0AD2AEF5 for ; Mon, 17 Nov 2025 14:20:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763389252; cv=none; b=VfJpVG1D9voekgr5H7FqB3BONdX8tMT5akwXYyy1zVnNhs68CqYqJrMoGD9F2FJ3vAHVEfqU5NQOxv5l2wzLxG6+FkkXTCIx3kKmrZc6+mkPM6qcYMaxWpALzEWVa66Zfr/n8nBe44bBE6DUwvRgU51ki1cAgH9caXcMxEOlVRA= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763389252; c=relaxed/simple; bh=uOFmWd34SNOW0WSeH9ve8vatlJxo806lXJIQDOR18cs=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=CbtbN8JM50wcWc18X3vQ0U3BWi4TXmP6A4WxpIlVUeL/9rwS7KROzlPTbsd36yA+ZgxBQ9OsIQxC/Ovn0F6dIAWMizSMjVIBcPL1HnZlL4a6v4KMWLQRtEC+6ELgJcOZ05wfYgBDeUoa2BtKjkSnAM3fgDUW7O41PhKJ0FbRtRM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gms.tku.edu.tw; spf=pass smtp.mailfrom=gms.tku.edu.tw; dkim=pass (2048-bit key) header.d=gms-tku-edu-tw.20230601.gappssmtp.com header.i=@gms-tku-edu-tw.20230601.gappssmtp.com header.b=IQ5EDc2m; arc=none smtp.client-ip=209.85.216.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=gms.tku.edu.tw Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gms.tku.edu.tw Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gms-tku-edu-tw.20230601.gappssmtp.com header.i=@gms-tku-edu-tw.20230601.gappssmtp.com header.b="IQ5EDc2m" Received: by mail-pj1-f44.google.com with SMTP id 98e67ed59e1d1-34381ec9197so3992338a91.1 for ; Mon, 17 Nov 2025 06:20:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gms-tku-edu-tw.20230601.gappssmtp.com; s=20230601; t=1763389249; x=1763994049; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=WDjwQ+AQw6XteHbhrcM93QrZO2AXe3GggbYl4g1PYWE=; b=IQ5EDc2mPK61X6n2a1zEjVn4nLylSI2FW0vZW9DdKnPLJoCX2M5esik0/3bAIqPw3g F/AZOjEPjWxjzPBFi0xAOLwXsVoFN4Yidoqsuef8t9dQb3tGZJdERIEWGLO/tdDk+Rmw eAslQ9+j4eAHVjm+cCL+nX9/+vvR62MjLqqbGq7wpLU2/RiFJgKtFF/16CPw3bG+Oeuo YxSrmMmgh7JixnbS82ekDPCFVkx9QeQ8jNAlrZViD0E9D/vybDxdTOIofiFdkPBqblWu oDerlMYc+k3+S94cI//2ULSHyx4uB0A8YY+w0LOWBLCWhdipOd1yXh7kGkSEtqvwJqkV MTsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1763389249; x=1763994049; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=WDjwQ+AQw6XteHbhrcM93QrZO2AXe3GggbYl4g1PYWE=; b=L09AMBljrx1zyGPtM4Hmz/4DB9vxVgxMnK5/EmfuVql9Ojr8AEk+JL4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Sn5kFimyaTjHjveuWTGxlwiZFjVicXc5llUq5ydaR55j1pBCw== X-Received: by 2002:a17:90b:2790:b0:340:e8e9:cc76 with SMTP id 98e67ed59e1d1-343f9e9f3a9mr15610880a91.11.1763389248980; Mon, 17 Nov 2025 06:20:48 -0800 (PST) Received: from wu-Pro-E500-G6-WS720T ([2001:288:7001:2703:d709:f3be:9719:dfe]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-343e07b4b23sm18321505a91.13.2025.11.17.06.20.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 17 Nov 2025 06:20:48 -0800 (PST) Date: Mon, 17 Nov 2025 22:20:45 +0800 From: Guan-Chun Wu <409411716@gms.tku.edu.tw> To: David Laight Cc: Theodore Ts'o , Andreas Dilger , linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org, visitorckw@gmail.com Subject: Re: [PATCH] ext4: improve str2hashbuf by processing 4-byte chunks Message-ID: References: <20251116130105.1988020-1-409411716@gms.tku.edu.tw> <20251116193513.0f90712a@pumpkin> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20251116193513.0f90712a@pumpkin> Hi David, On Sun, Nov 16, 2025 at 07:35:13PM +0000, David Laight wrote: > On Sun, 16 Nov 2025 21:01:05 +0800 > Guan-Chun Wu <409411716@gms.tku.edu.tw> wrote: > > > The original byte-by-byte implementation with modulo checks is less > > efficient. Refactor str2hashbuf_unsigned() and str2hashbuf_signed() > > to process input in explicit 4-byte chunks instead of using a > > modulus-based loop to emit words byte by byte. > > There are much bigger gains to be made - the current code is horrid. > Not least due to the costs of the indirect calls. > It is better to use conditionals than indirect calls. > Thanks for the feedback. I'll remove the redundant casts, and for the unsigned version I'll switch to using get_unaligned_be32() to avoid duplicating the implementation. If this approach looks reasonable, I can send a v2 that replaces the indirect calls with conditionals. Best regards, Guan-Chun > > > > > This change removes per-byte modulo checks and reduces loop iterations, > > improving efficiency. > > > > Performance test (x86_64, Intel Core i7-10700 @ 2.90GHz, average over 10000 > > runs, using kernel module for testing): > > > > len | orig_s | new_s | orig_u | new_u > > ----+--------+-------+--------+------- > > 1 | 70 | 71 | 63 | 63 > > 8 | 68 | 64 | 64 | 62 > > 32 | 75 | 70 | 75 | 63 > > 64 | 96 | 71 | 100 | 68 > > 255 | 192 | 108 | 187 | 84 > > > > Signed-off-by: Guan-Chun Wu <409411716@gms.tku.edu.tw> > > --- > > fs/ext4/hash.c | 48 ++++++++++++++++++++++++++++++++---------------- > > 1 file changed, 32 insertions(+), 16 deletions(-) > > > > diff --git a/fs/ext4/hash.c b/fs/ext4/hash.c > > index 33cd5b6b02d5..75105828e8b4 100644 > > --- a/fs/ext4/hash.c > > +++ b/fs/ext4/hash.c > > @@ -141,21 +141,29 @@ static void str2hashbuf_signed(const char *msg, int len, __u32 *buf, int num) > > pad = (__u32)len | ((__u32)len << 8); > > pad |= pad << 16; > > > > - val = pad; > > if (len > num*4) > > len = num * 4; > > - for (i = 0; i < len; i++) { > > - val = ((int) scp[i]) + (val << 8); > > - if ((i % 4) == 3) { > > - *buf++ = val; > > - val = pad; > > - num--; > > - } > > + > > + while (len >= 4) { > > + val = ((int)scp[0] << 24) + ((int)scp[1] << 16) + > > + ((int)scp[2] << 8) + (int)scp[3]; > > The (int) casts are unnecessary (throughout), 'char' is always promoted to > 'signed int' before any arithmetic. > > > + *buf++ = val; > > + scp += 4; > > + len -= 4; > > + num--; > > } > > + > > + val = pad; > > + > > + for (i = 0; i < len; i++) > > + val = (int)scp[i] + (val << 8); > > + > > if (--num >= 0) > > *buf++ = val; > > + > > while (--num >= 0) > > *buf++ = pad; > > + > > } > > > > static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num) > > @@ -167,21 +175,29 @@ static void str2hashbuf_unsigned(const char *msg, int len, __u32 *buf, int num) > > pad = (__u32)len | ((__u32)len << 8); > > pad |= pad << 16; > > > > - val = pad; > > if (len > num*4) > > len = num * 4; > > - for (i = 0; i < len; i++) { > > - val = ((int) ucp[i]) + (val << 8); > > - if ((i % 4) == 3) { > > - *buf++ = val; > > - val = pad; > > - num--; > > - } > > + > > + while (len >= 4) { > > + val = ((int)ucp[0] << 24) | ((int)ucp[1] << 16) | > > + ((int)ucp[2] << 8) | (int)ucp[3]; > > Isn't that get_misaligned_be32() ? > > David > > > + *buf++ = val; > > + ucp += 4; > > + len -= 4; > > + num--; > > } > > + > > + val = pad; > > + > > + for (i = 0; i < len; i++) > > + val = (int)ucp[i] + (val << 8); > > + > > if (--num >= 0) > > *buf++ = val; > > + > > while (--num >= 0) > > *buf++ = pad; > > + > > } > > > > /* >