Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1055127rwn; Thu, 15 Sep 2022 09:46:16 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6dP0b6t0t+aFTUb2UOedkxhKV9sXEJwvsfo9a0AJyHMsrjpTUBaVK6oNVvgOk0wTll6fPw X-Received: by 2002:a65:6cce:0:b0:439:49b5:48b8 with SMTP id g14-20020a656cce000000b0043949b548b8mr653764pgw.514.1663260375825; Thu, 15 Sep 2022 09:46:15 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663260375; cv=none; d=google.com; s=arc-20160816; b=G5tzXqulRAJKo0xYWTi/yL6Tp4TjKmTgofiRgAwFFDmiAQfyaUi3H42qoMLqoGmYjr PSTw5G5M6gra9KKtCWexXROvl1jIf6EhRDO2GRJMPMIpYbeHVui3Ig/MRPiLsvOn6PMs O4jBCDiU/405q8CJsNzuT+uBkZ8OWmspunyREzPS+X1qPEf/FryTfcDWRWGTarP3xJtd OLWyMk7WS8xqKaJc6lfsGoramVXghYv99Mbp1l/Rp65+/OBMssxF6RyZvScPmU3p2/SB yZdk8UyRRcifiRKLy9qBN+yeOYE7egrl7ND0J9LaaHKturyHFsavBMydDOz2v/u9jq5c 8JUA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:dkim-signature; bh=PRNaAPJIxfa3ZuIvB7F/07ny/tmf9pDcXpC1rsM9po8=; b=Ohf1Aj+SiwwRUl3rueVBwJMiiblo2fg5xi4GirVjecLFYemm1fpv6yBaDuwH2pkKn4 XGfo6z5Dygo2rvnQTZ3FlrO0RijUlJFgeZ/0v0H5/9yRP6N+QIwxa0USPwPcgtP9Amb+ USbpk8Tvdfaw9otdkAwbJYaDffBNWqEXHoKV9iJiBn2EI2/GNIrdkDEcpka99yXCY9Nk iPs4VvzNfP+o3lG9lZXxhPukozu3cE1ZvhxISigj+YTac0rvOnETqWnLZ/uSa2MWr59u X4uAKHcS9rm6oGG+GfTjOHeSFpbW8RiQ6V3JoxkBKU3AFz4OnIfAiN0f/7P/SUDfhfkm pHoA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="jG3/WdR0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u14-20020a056a00158e00b00545c1d801d7si9701882pfk.364.2022.09.15.09.46.04; Thu, 15 Sep 2022 09:46:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b="jG3/WdR0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229936AbiIOQZd (ORCPT + 99 others); Thu, 15 Sep 2022 12:25:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52332 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229542AbiIOQZc (ORCPT ); Thu, 15 Sep 2022 12:25:32 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EFA2E8FD7A for ; Thu, 15 Sep 2022 09:25:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1663259130; 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: in-reply-to:in-reply-to:references:references; bh=PRNaAPJIxfa3ZuIvB7F/07ny/tmf9pDcXpC1rsM9po8=; b=jG3/WdR0Or4OEotOD8a20XSUXXVUOIg32gTCuDm0BGIBgsnN/mDOhx/DU6ZAHpRIkLamcr EPO9MIIBeFzs+FFMvLcrvEdDVqcVzSKsar9S80tLQVV83pTYRnt4kPwdjB2MGGABTOTPr/ bEYH5Xt89pFnY7K/4E2LasAwOOHXf7A= Received: from mail-wm1-f69.google.com (mail-wm1-f69.google.com [209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-434-tro2moaDM8i6EQy_deunEg-1; Thu, 15 Sep 2022 12:25:29 -0400 X-MC-Unique: tro2moaDM8i6EQy_deunEg-1 Received: by mail-wm1-f69.google.com with SMTP id q10-20020a1ce90a000000b003a60123678aso6756973wmc.6 for ; Thu, 15 Sep 2022 09:25:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date; bh=PRNaAPJIxfa3ZuIvB7F/07ny/tmf9pDcXpC1rsM9po8=; b=dQWdFsh5AF04cQT1dt7ifwd7Qz3yHsoTnwPxHLkv/HPe9BwErKaElRjPcTx0cLq3jT Smt8R0qCJXBTWTVLEg2MAKpNXZJVzCSO2wCr70TSxt7TOhDvxG47G9eUzYaFNqv4cbez yiot97d88JZjXHsR0gPnBx7Pm3s99+ozTC0/xz4y2/4oosolE34HdCinRfHlruq8tKBY U8wTmPmMWrkFVAlViCN3eFUen/IJmMaqNfudyNVJ8RAp0czM/CYzSY45SJ7N8crR2EkG XC3BCwZdeDizz4+sBeB0Od//8HUW3EMx51V5zF6teZCiuMSPlHUcudqHW72oo/PLz00+ aN3Q== X-Gm-Message-State: ACrzQf2Y6UkcrhhN+gbPDKLR/fUMpx6zJo1IzlTMRG2jyqDm5ZUim+Xg LwD8BYeJVQzPnN2PUX5tQn6BkiFq77lwob/0YLgSsAbusYs9ZWXuuXVUs1s/oAygPh0zAHLj3pm GJaBztkffDJsFW/zO2QMN0iCj X-Received: by 2002:a5d:45c6:0:b0:228:8bda:f345 with SMTP id b6-20020a5d45c6000000b002288bdaf345mr315197wrs.584.1663259126282; Thu, 15 Sep 2022 09:25:26 -0700 (PDT) X-Received: by 2002:a5d:45c6:0:b0:228:8bda:f345 with SMTP id b6-20020a5d45c6000000b002288bdaf345mr315180wrs.584.1663259126081; Thu, 15 Sep 2022 09:25:26 -0700 (PDT) Received: from vschneid.remote.csb ([185.11.37.247]) by smtp.gmail.com with ESMTPSA id v13-20020adfebcd000000b0022a403954c3sm3154289wrn.42.2022.09.15.09.25.25 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 15 Sep 2022 09:25:25 -0700 (PDT) From: Valentin Schneider To: Yury Norov , Linus Torvalds , linux-kernel@vger.kernel.org Cc: Yury Norov , Alexey Klimov , Andy Shevchenko , Andy Whitcroft , Catalin Marinas , David Laight , Dennis Zhou , Guenter Roeck , Kees Cook , Rasmus Villemoes , Sven Schnelle , Russell King Subject: Re: [PATCH v4 3/4] lib/find_bit: optimize find_next_bit() functions In-Reply-To: <20220915020730.852234-4-yury.norov@gmail.com> References: <20220915020730.852234-1-yury.norov@gmail.com> <20220915020730.852234-4-yury.norov@gmail.com> Date: Thu, 15 Sep 2022 17:25:24 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-2.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 14/09/22 19:07, Yury Norov wrote: > Over the past couple years, the function _find_next_bit() was extended > with parameters that modify its behavior to implement and- zero- and le- > flavors. The parameters are passed at compile time, but current design > prevents a compiler from optimizing out the conditionals. > > As find_next_bit() API grows, I expect that more parameters will be added. > Current design would require more conditional code in _find_next_bit(), > which would bloat the helper even more and make it barely readable. > > This patch replaces _find_next_bit() with a macro FIND_NEXT_BIT, and adds > a set of wrappers, so that the compile-time optimizations become possible. > > The common logic is moved to the new macro, and all flavors may be > generated by providing a FETCH macro parameter, like in this example: > > #define FIND_NEXT_BIT(FETCH, MUNGE, size, start) ... > > find_next_xornot_and_bit(addr1, addr2, addr3, size, start) > { > return FIND_NEXT_BIT(addr1[idx] ^ ~addr2[idx] & addr3[idx], > /* nop */, size, start); > } > > The FETCH may be of any complexity, as soon as it only refers the bitmap(s) > and an iterator idx. > > MUNGE is here to support _le code generation for BE builds. May be > empty. > > I ran find_bit_benchmark 16 times on top of 6.0-rc2 and 16 times on top > of 6.0-rc2 + this series. The results for kvm/x86_64 are: > > v6.0-rc2 Optimized Difference Z-score > Random dense bitmap ns ns ns % > find_next_bit: 787735 670546 117189 14.9 3.97 > find_next_zero_bit: 777492 664208 113284 14.6 10.51 > find_last_bit: 830925 687573 143352 17.3 2.35 > find_first_bit: 3874366 3306635 567731 14.7 1.84 > find_first_and_bit: 40677125 37739887 2937238 7.2 1.36 > find_next_and_bit: 347865 304456 43409 12.5 1.35 > > Random sparse bitmap > find_next_bit: 19816 14021 5795 29.2 6.10 > find_next_zero_bit: 1318901 1223794 95107 7.2 1.41 > find_last_bit: 14573 13514 1059 7.3 6.92 > find_first_bit: 1313321 1249024 64297 4.9 1.53 > find_first_and_bit: 8921 8098 823 9.2 4.56 > find_next_and_bit: 9796 7176 2620 26.7 5.39 > > Where the statistics is significant (z-score > 3), the improvement > is ~15%. > > According to the bloat-o-meter, the Image size is 10-11K less: > > x86_64/defconfig: > add/remove: 32/14 grow/shrink: 61/782 up/down: 6344/-16521 (-10177) > > arm64/defconfig: > add/remove: 3/2 grow/shrink: 50/714 up/down: 608/-11556 (-10948) > > Suggested-by: Linus Torvalds > Signed-off-by: Yury Norov Reviewed-by: Valentin Schneider