From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f46.google.com (mail-wm1-f46.google.com [209.85.128.46]) (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 6A6CB2D9EDC for ; Wed, 28 Jan 2026 18:59:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769626749; cv=none; b=JHoPJD1Ip/sfGhGtum9audCqxPd+lD5fkyKJpZELy5jDq7AmTvUm4wabFziRZuRTDttiFdlLq3EgYomzEkiL2im91/vbcTVGuWUS7xhrNnsCeuq/3eHLVYem138pGlZFy9kzCxZp2L9ct/ekDbXPooSzBnR6iiQB154MnJWRO6Y= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769626749; c=relaxed/simple; bh=t9HRh2/aqddUbiilaOIcaAID1Ye64SZ1msyFFekrxCo=; h=Date:From:To:Cc:Subject:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MGIAuFcRSTeJZLsdNcUZmJabn7stDTcvCjXl2Fy2UxTPXwFaX3hD8H11FlogNQ4We6C3hbPOAieXrrUtPEh523bsWvt2kh2T6tOJKfj941UAVl+RREIi/NXnyqSh0EW3PywFSH3cjqAWqMUfy9YuYOtG03e8a1lhB0VdC2EztfQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HUB/XK7v; arc=none smtp.client-ip=209.85.128.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HUB/XK7v" Received: by mail-wm1-f46.google.com with SMTP id 5b1f17b1804b1-47d6a1f08bbso855415e9.2 for ; Wed, 28 Jan 2026 10:59:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1769626747; x=1770231547; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:from:to:cc:subject:date :message-id:reply-to; bh=JamM64RSSSNUgknQxElRWBW/Tc9C5G8ZEIUqJTiS1t4=; b=HUB/XK7vP+HYzQ4rrCTwhimSXO89L8WHgNsq2tnXCZKegAQOZ2JXDFjXGnmvAqmn2I tKX1bTg086OFsy37llbixqsH28Q1JzddZqyzqz2eLCsxT/eq7PRxFbwnq4eZNgx9GKJ+ MMWsrJ1bxyh6wOeZ7a5PK6WZxr1W0bXJJvFxrQHT/hudcmpGD8imFVHrYGzW3fEFx9z7 BnL/zH9BOVl5azABd0MMaMIrK65LSXAzpL0I6uCuHI39kaHHI9n444qCfudGk6WUexKj ypr8gWCHx85gBz2vaWNyEDCbqgJcaMOBGIK1Qikz+EjsXshXNbc2oa5Fw8XGBJ/qiJN0 YHIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769626747; x=1770231547; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:subject:cc:to:from:date:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=JamM64RSSSNUgknQxElRWBW/Tc9C5G8ZEIUqJTiS1t4=; b=ikrW6ySUCtI2DrrcOfvDtrdkHxeTO0fGTdC3F9lGxVPq601HImXKm7zz2XZXdLhTFq XSLAAGDIiwadGv3ZDUgmycvvRqc3eYvlh1a8kqwVOMIaSJAcNASucdzfRXYseJzH1eSZ 5Ye6ZnrpFjULA+SBRtv3LXUmB+Sc5u09IXaleDkuZd/FhtcZZjvPnTgCA6o7fuVEdYBv G4GLo3vmYYsyp4ryB0DcAdGfoax73nNnIv+McWyFJU6WwvUKvObNwLZ6a34XPnLny3VN xEvEerPNNjAQ0qwxbKrVyeclx4alqnpkaUMW82R5Dgje1vWD0R0wo796AzL/Q1K2K5+C Ix7w== X-Forwarded-Encrypted: i=1; AJvYcCVUlqhnDNzLOebwZBTkhz5b5vNtLDdrmdLCGS5HOnljUrwPbjDgcgEdsgIbusAUV+E66iVW81Ieq9xUnnA=@vger.kernel.org X-Gm-Message-State: AOJu0YwPVtywOFg3DtnWd2AOQFCAWRWFyg8xI412XMJM5n1OjrB5R2d1 wzxqw8eDOyyC6j5FdRSyl+iuMxz0pTP2hScGIDBo8nh84C7XeQSNqR4i X-Gm-Gg: AZuq6aIcfCW6duSKN/YmfeWUa+HeF6il3Sfm3OeFQR5vHuiA1X0Xd3DL2+d/hAnROev URyeFrpmXRg0Xeo+1g24TbpRouDlT3QtbIlIU97tp+rQrcemL6QSDN5NCDR8UX1q0w2jxch9ZtD 4kSBwkz4bfJQxzCanocjlMRzGsM+0lCZBw29l6c04aWJGA5GqD4b6XM9R/PzRXHbKVqqPqXqPfa bYcrFAixR0gyI6WsUQpZ4XNCrnC/5ynehOoOKTZMmx8XcGtzjPeVFZs6xiurmDmiqmzH019pV/x Qx3a6qIGwpmbrH6k46RTs13zDvmneWddIrTPWp3Y+k9uQgEQmd6KK4B5nHOgb6BRp0PYsrIaohG dNMeOciXt469/GNH5IHsjAEUV/4JvLaMzY2UWBrolffVg0WsScmqt0Cd0suY+kpU6i1USJetzCH SBhNxroWyK332cxrp1WqUz01oyhLUytj8fow1MnazwoY1RBYNPWHr2 X-Received: by 2002:a05:600c:608f:b0:480:68ed:1e73 with SMTP id 5b1f17b1804b1-48069cbf2bdmr64592595e9.36.1769626746306; Wed, 28 Jan 2026 10:59:06 -0800 (PST) Received: from pumpkin (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4806ce56490sm75659735e9.12.2026.01.28.10.59.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 28 Jan 2026 10:59:05 -0800 (PST) Date: Wed, 28 Jan 2026 18:59:04 +0000 From: David Laight To: Paul Walmsley Cc: Feng Jiang , palmer@dabbelt.com, aou@eecs.berkeley.edu, alex@ghiti.fr, samuel.holland@sifive.com, charlie@rivosinc.com, conor.dooley@microchip.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] riscv: lib: optimize strlen loop efficiency Message-ID: <20260128185904.5ec5c24e@pumpkin> In-Reply-To: <20260115184619.574f1b36@pumpkin> References: <20251218032614.57356-1-jiangfeng@kylinos.cn> <20260115111947.54929ed0@pumpkin> <20260115184619.574f1b36@pumpkin> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; arm-unknown-linux-gnueabihf) 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-Transfer-Encoding: 7bit On Thu, 15 Jan 2026 18:46:19 +0000 David Laight wrote: ... > While I suspect the per-byte cost is 'two bytes/clock' on x86-64 > the fixed cost may move the break-even point above the length of the > average strlen() in the kernel. > Of course, x86 probably falls back to 'rep scasb' at (maybe) > (40 + 2n) clocks for 'n' bytes. > A carefully written slightly unrolled asm loop might manage one > byte per clock! > I could spend weeks benchmarking different versions. I've spent a quick half-hour... On my zen-5 in userspace: glibc's strlen() is showing the same fixed cost (50 clocks including overhead) for sizes below (about) 100 bytes, for big buffers add 1 clock for ~50 bytes. It must be using some simd instructions. A simple: len = 0; while (s[len]) len++; return len; loop is about 1 byte/clock, overhead ~25 clocks (probably the mostly one 'rdpmc' instruction). (Needs a barrier() to stop gcc converting it to a libc call.) Unrolling the loop once: for (len = 0; s[len]; len += 2) if (!s[len + 1] return len + 1; return len; actually runs twice as fast - so 2 bytes/clock. Unrolling 4 times doesn't help, suddenly goes somewhat slower somewhere between 128 and 256 bytes (to 1.5 bytes/clock). The C 'longs' loop has an overhead of ~45 clocks and does 6 bytes/clock. So the is better for buffers longer than 64 bytes. The 'elephant in the room' is 'repne scasb'. The fixed cost is some 150 clocks and the cost 3 clocks/byte. I don't think any of the Intel cpu I have will do a 'one clock loop'. I certainly failed to get one in the past when there was a data-dependency between the iterations. But I don't have anything modern (newest is an i7-7xxx) and I don't have any old amd ones. I needs to get a zen-1 (or 1a) and one of the Intel system that should be cheap because they won't run win-11. David