From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-yb1-f169.google.com (mail-yb1-f169.google.com [209.85.219.169]) (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 36EE6199254 for ; Fri, 17 Jan 2025 16:32:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.169 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737131578; cv=none; b=njgWVb4ktP38a2r864rHYwweKUa24aP9CVNcQWNUagvUx+p5ce3D/kGoKVcevFzZ7bSHwwkEcTJuS4exSD2LXqEh/3BESvvulcs7zNor2RbqOBx9vO9hE0iYz5j3ZE990im/iHt7lEQ7klYlUmdd0+G4FQTdemQ7Yk9pm0F06wc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737131578; c=relaxed/simple; bh=2rWIPsJ47Q7Zd1k2mRi5tRsdvSep9YUB39lGSmUIZPw=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=ApKhzRVN9Cb0bqFSUVD65Cw+Di/LYeLRqQhWJ1Ym3nXCnDJvUsrg1jN0TNHRtX3394iRonNDUuRYskOZZhGIQ2vJ89BdD3bH+U5fwMDmudIArf3WO8XlXcKV6IRWIzToxfc/6NTxnw0a+54/01C5LSGjzG/4ZJ7/hLETR1aQ9DE= 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=cHRVDZPW; arc=none smtp.client-ip=209.85.219.169 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="cHRVDZPW" Received: by mail-yb1-f169.google.com with SMTP id 3f1490d57ef6-e549a71dd3dso4322292276.0 for ; Fri, 17 Jan 2025 08:32:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737131576; x=1737736376; 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=+IjQ3I7NGP2xvnkk32F+whEgRUJmuWt6D5hZNAbERYA=; b=cHRVDZPWRJILJ/UYPq8Q0jTWzrInTH/3SOEd0KYbIwKAMZtexHPxtoQUVc5Zyyw2tE zvLD3aOooXvAUe/X80tmprJhxprrX+SnwClKftqJ93OYwLx7k6RzlMVMvo7Zclc64Hcv KDwSLet9J05FdP4Y3LGLDc7+U20LkYzB9wQXJ1bvfXTzp76puKfwHN5Xl5R8mWKhr14O hMfsNNovUE/FYrNXpHD8HK0kthfj5M69aOHtraGp4vlv8EZuYqJ31kFhvwMLW+fa3abM j7lyFLQuJ7En6+mUixFunOPFLZYZD6le1hCvTA0NnSoDBfTYtP1U0ykCulEnfgGB0jcA g4yg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131576; x=1737736376; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=+IjQ3I7NGP2xvnkk32F+whEgRUJmuWt6D5hZNAbERYA=; b=SfeAaVdW8kp1pVuvkNQk8vU+63UE3RWL2dN1sLLHNvgXL8B18NlXHUzWZNBFGtU1vn VXLlozp8aoQamaG7+XmP/sY1aXjVLlQOFdFaKDWovinvRMnfT+v1ydJ/yjIfXred2oxv r3SYmANMX8oEyBkQEKm0TtkoVLlik9VTRQdXqd3rntfWkTDCf5jTRzlRcRRcm1D+DL5p U15o6njuiLu3fFo9HOHjpm1WYD+6pnl8o/QGg4w0EKUFJyyNZbRXAoD95QNpMl+KnvUq HNWb9v+g0kJoYLgUyppI1U9kwqnheKp6lxQgPjmS12ahFUQQzv2+1/RnCZGcyc6Iited mzNQ== X-Forwarded-Encrypted: i=1; AJvYcCWiHJZqvgK43dN/+V4YCwKvJ5e+UelGUKA0+31n1iZ7YS4DWPy7uzI9XT6FzOW4sdnXEVuKh57nZyHhs90=@vger.kernel.org X-Gm-Message-State: AOJu0YzmzLCCxDEVrka+B8u1as9mAk3rUY6il6Y3eRlmoQvDOOCLQGuG euKjEefyPDWM5sKAY69hN9rKOoymdu5ce6DoJ7uqXnd44PW0nMsgCBsq8tva X-Gm-Gg: ASbGncvRswPjWXZFzckrBwQ+NoLmLjPoPR60TuLFAnzp85IfeXMDq5By74A5q469uNy PhESpXyPXXMtfjpWaIbzWTrzKinMimmGS8WHwBx1rTxy/eKxgvrEe2tdRfdcbwxOUJnoykUmCGY pioi8Z5HWwNklGbIRHOraAyhM0yOCltUmoW+KN726ejy35XM/ioY4hWGk6AM2KWvjYYoqhcnLNK 4BZuozEBfteiGJLybRa4SSOnOTUEIoC8AGrz+y2PybCooZZwLiLIHHX X-Google-Smtp-Source: AGHT+IEaM3oYEbNJYVCQlW/HljiIpD1TQI85ZjpYHhQaoUK1Z6J/kIrBwGQzZdAkE3Xxp94izw5Vfw== X-Received: by 2002:a05:690c:6b02:b0:6f6:c95c:85b5 with SMTP id 00721157ae682-6f6eb92ed12mr25138467b3.25.1737131575981; Fri, 17 Jan 2025 08:32:55 -0800 (PST) Received: from localhost ([2601:347:100:5ea0:5f67:767f:8925:4f3b]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6f6e66d1167sm4674427b3.77.2025.01.17.08.32.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 17 Jan 2025 08:32:55 -0800 (PST) Date: Fri, 17 Jan 2025 11:32:54 -0500 From: Yury Norov To: I Hsin Cheng Cc: Kuan-Wei Chiu , linux@rasmusvillemoes.dk, jserv@ccns.ncku.edu.tw, mark.rutland@arm.com, linux-kernel@vger.kernel.org, eleanor15x@gmail.com Subject: Re: [PATCH] cpumask: Optimize cpumask_any_but() Message-ID: References: <20250117142658.297325-1-visitorckw@gmail.com> 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: On Fri, Jan 17, 2025 at 10:59:31PM +0800, I Hsin Cheng wrote: > On Fri, Jan 17, 2025 at 10:26:58PM +0800, Kuan-Wei Chiu wrote: > > The cpumask_any_but() function can avoid using a loop to determine the > > CPU index to return. If the first set bit in the cpumask is not equal > > to the specified CPU, we can directly return the index of the first set > > bit. Otherwise, we return the next set bit's index. > > > > This optimization replaces the loop with a single if statement, > > allowing the compiler to generate more concise and efficient code. I thought compilers are smart enough to unroll loop in this case. Can you show disassembled code before and after? > > > > As a result, the size of the bzImage built with x86 defconfig is > > reduced by 4096 bytes: > > > > * Before: > > $ size arch/x86/boot/bzImage > > text data bss dec hex filename > > 13537280 1024 0 13538304 ce9400 arch/x86/boot/bzImage > > > > * After: > > $ size arch/x86/boot/bzImage > > text data bss dec hex filename > > 13533184 1024 0 13534208 ce8400 arch/x86/boot/bzImage Comparing zipped images tells little about code generation. Please use scripts/bloat-o-meter. > > > > Co-developed-by: Yu-Chun Lin > > Signed-off-by: Yu-Chun Lin > > Signed-off-by: Kuan-Wei Chiu > > --- > > Not sure how to measure the efficiency difference, but I guess this > > patch might be slightly more efficient or nearly the same as before. If > > you have any good ideas for measuring efficiency, please let me know! Check lib/find_bit_benchmark.c > > > > include/linux/cpumask.h | 8 ++++---- > > 1 file changed, 4 insertions(+), 4 deletions(-) > > > > diff --git a/include/linux/cpumask.h b/include/linux/cpumask.h > > index 9278a50d514f..b769fcdbaa10 100644 > > --- a/include/linux/cpumask.h > > +++ b/include/linux/cpumask.h > > @@ -404,10 +404,10 @@ unsigned int cpumask_any_but(const struct cpumask *mask, unsigned int cpu) > > unsigned int i; > > > > cpumask_check(cpu); > > - for_each_cpu(i, mask) > > - if (i != cpu) > > - break; > > - return i; > > + i = find_first_bit(cpumask_bits(mask), small_cpumask_bits); > > Hi Kuan-Wei, > > How about using cpumask_first(mask) here to keep better consistency? I would do it the other way: introduce find_first_but_bit(), and then make cpumask_any_but() a wrapper around it. Doing this you'll be able to leverage find_bit_benchmark infrastructure to measure performance difference, if any. > > + if (i != cpu) > > + return i; > Wouldn't it benefit abit to check "i >= nr_cpu_ids" prior to > find_next_bit() ? Yes it would. Thanks, Yury > if "i >= nr_cpu_ids" holds it would be a waste to > perform find_next_bit(). > > > + return find_next_bit(cpumask_bits(mask), small_cpumask_bits, i + 1); > > } > > > > Regards, > I Hsin > > > /** > > -- > > 2.34.1 > >