Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oTpIH-0001GZ-EK for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Sep 2022 18:52:01 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oTpIG-0008Qm-3r for pgsql-hackers@arkaria.postgresql.org; Thu, 01 Sep 2022 18:52:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oTpIF-0008Qd-Rd for pgsql-hackers@lists.postgresql.org; Thu, 01 Sep 2022 18:51:59 +0000 Received: from mail-pl1-x62a.google.com ([2607:f8b0:4864:20::62a]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oTpID-0002Nl-Di for pgsql-hackers@postgresql.org; Thu, 01 Sep 2022 18:51:59 +0000 Received: by mail-pl1-x62a.google.com with SMTP id v5so11684198plo.9 for ; Thu, 01 Sep 2022 11:51:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :from:to:cc:subject:date; bh=rlNxqoO7DmErggnv+l/uO3X463W43imJ1IdQpbxK+nY=; b=J+/RMkwKghSsWrV7XUG/BVALQ7c8Buu6vs912xty2h7ZakrNXy8gqrkoWScYLq3Vr1 RB/R6WKY2QuZlQ+Gszyb+ib+wphyHxezo+QnrAkNqLXrOgRedzlQSRR4PS5S/C+0OuAU CwJGUtCTDs4y6o2mj0W7h9Z2YCOc8EZOAnBGTRQRwtsAPzguyLdkX1G2P4U1hHNtAYF4 VWzGEOTECxZkp2d7OqOn9ENSXPa9DED362kAUTVSmFkirdvLzWjAbrEYKahK/ecneWty nAcjLLW460vT1CvIzIWc/RcKCJsTpiS52flBxk4bH+pgiaQLWpgAfydz+xoaaPXhdkA+ NEjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=rlNxqoO7DmErggnv+l/uO3X463W43imJ1IdQpbxK+nY=; b=HW/EK7J8uFLxPqdBiQMZ7QaVAMMg1lNyKmSUxgUS/BMWDJApEd20M4igAjPf4yg7Jg 3vtYKpIF1j7r+Ywjz/p9xpdxhU0D3rwp8t+g89hF3GW2BzZMZruPL5h+vh50Zm8+zWZU nNZRzFg4d1n4B1PQcaWwfqDIzpksiWPEkAy+U2FMhe92xWvNtv9KkoBbPLqNqbWrNmhP TeVMqxSPHzh78lHqHErPVvyupyIjUUWmGKP8NA5A74UVdN5llb953iSPuP6VB25UmcpW ZH+cr9McIcOIt+xJMHEqyWoBcGvE8GFIatXaj8pY2ANKBKYN+f/fyguEBchCrz3xlHok AbzA== X-Gm-Message-State: ACgBeo17Lk1e5cobX/UUXQI0NYz1jHdMUfnsjfJ5l9vrIYi8Lc4ITvWO jJMk19/2NXTCbmG9hitTgzXxdwn1+Tg= X-Google-Smtp-Source: AA6agR4CTdudCWkjK3oAY0HE6gpFN2h2vEQzLwbRYwep54s0mg+AonYprHd9Rl6CXm5dktfi4mI8Pg== X-Received: by 2002:a17:902:f549:b0:175:50a5:fe1f with SMTP id h9-20020a170902f54900b0017550a5fe1fmr7044443plf.40.1662058315034; Thu, 01 Sep 2022 11:51:55 -0700 (PDT) Received: from nathanxps13 ([50.47.162.83]) by smtp.gmail.com with ESMTPSA id 23-20020a631157000000b004277f43b736sm5543576pgr.92.2022.09.01.11.51.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Sep 2022 11:51:54 -0700 (PDT) Date: Thu, 1 Sep 2022 11:51:53 -0700 From: Nathan Bossart To: pgsql-hackers@postgresql.org Subject: make additional use of optimized linear search routines Message-ID: <20220901185153.GA783106@nathanxps13> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="HlL+5n6rz5pIUxbD" Content-Disposition: inline List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --HlL+5n6rz5pIUxbD Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi hackers, Now that we have some optimized linear search routines [0], I thought I'd quickly check whether we could use them elsewhere. To start, I took another look at a previously posted patch [1] and noticed two potentially useful applications of pg_lfind32(). The attached patches replace the open-coded linear searches with calls to pg_lfind32(). I haven't done any performance analysis with these patches yet, and the overall impact might be limited, but it seemed like low-hanging fruit. I'm hoping to spend a bit more time looking for additional applications of the pg_lfind*() suite of functions (and anywhere else where SIMD might be useful, really). If you have any ideas in mind, I'm all ears. [0] https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/include/port/pg_lfind.h [1] https://postgr.es/m/20220802221301.GA742739%40nathanxps13 -- Nathan Bossart Amazon Web Services: https://aws.amazon.com --HlL+5n6rz5pIUxbD Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0001-use-optimized-linear-search-in-TransactionIdIsInP.patch" From 1ea31af477d66557fdbc5f83a5bdacadd6159a9d Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Thu, 1 Sep 2022 11:23:15 -0700 Subject: [PATCH v1 1/2] use optimized linear search in TransactionIdIsInProgress --- src/backend/storage/ipc/procarray.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index 0555b02a8d..a8b7b65518 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -58,6 +58,7 @@ #include "commands/dbcommands.h" #include "miscadmin.h" #include "pgstat.h" +#include "port/pg_lfind.h" #include "storage/proc.h" #include "storage/procarray.h" #include "storage/spin.h" @@ -1586,14 +1587,9 @@ TransactionIdIsInProgress(TransactionId xid) */ topxid = SubTransGetTopmostTransaction(xid); Assert(TransactionIdIsValid(topxid)); - if (!TransactionIdEquals(topxid, xid)) - { - for (int i = 0; i < nxids; i++) - { - if (TransactionIdEquals(xids[i], topxid)) - return true; - } - } + if (!TransactionIdEquals(topxid, xid) && + pg_lfind32(topxid, xids, nxids)) + return true; cachedXidIsNotInProgress = xid; return false; -- 2.25.1 --HlL+5n6rz5pIUxbD Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="v1-0002-use-optimized-linear-search-in-XidIsConcurrent.patch" From d82f9b1a6e3f41e434bb5826d277269b891c9882 Mon Sep 17 00:00:00 2001 From: Nathan Bossart Date: Thu, 1 Sep 2022 11:29:38 -0700 Subject: [PATCH v1 2/2] use optimized linear search in XidIsConcurrent --- src/backend/storage/lmgr/predicate.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/backend/storage/lmgr/predicate.c b/src/backend/storage/lmgr/predicate.c index 5136da6ea3..f287ddfa73 100644 --- a/src/backend/storage/lmgr/predicate.c +++ b/src/backend/storage/lmgr/predicate.c @@ -202,6 +202,7 @@ #include "access/xlog.h" #include "miscadmin.h" #include "pgstat.h" +#include "port/pg_lfind.h" #include "storage/bufmgr.h" #include "storage/predicate.h" #include "storage/predicate_internals.h" @@ -4065,7 +4066,6 @@ static bool XidIsConcurrent(TransactionId xid) { Snapshot snap; - uint32 i; Assert(TransactionIdIsValid(xid)); Assert(!TransactionIdEquals(xid, GetTopTransactionIdIfAny())); @@ -4078,13 +4078,7 @@ XidIsConcurrent(TransactionId xid) if (TransactionIdFollowsOrEquals(xid, snap->xmax)) return true; - for (i = 0; i < snap->xcnt; i++) - { - if (xid == snap->xip[i]) - return true; - } - - return false; + return pg_lfind32(xid, snap->xip, snap->xcnt); } bool -- 2.25.1 --HlL+5n6rz5pIUxbD--