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 1q1O2f-0002qg-OE for pgsql-hackers@arkaria.postgresql.org; Tue, 23 May 2023 09:10:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q1O2e-0001Zf-3f for pgsql-hackers@arkaria.postgresql.org; Tue, 23 May 2023 09:10:52 +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 1q1O2d-0001ZU-PA for pgsql-hackers@lists.postgresql.org; Tue, 23 May 2023 09:10:51 +0000 Received: from mx.zeyos.com ([188.34.176.68]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q1O2Y-001cdm-Bk for pgsql-hackers@postgresql.org; Tue, 23 May 2023 09:10:51 +0000 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id 4QQT7T5k67z1tnvY for ; Tue, 23 May 2023 09:10:45 +0000 (UTC) Authentication-Results: mx.zeyos.com (amavisd-new); dkim=pass (1024-bit key) reason="pass (just generated, assumed good)" header.d=zeyos.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=zeyos.com; h= content-transfer-encoding:content-type:mime-version:references :in-reply-to:message-id:to:from:subject:date; s=dkim; t= 1684833045; x=1685697046; bh=hYglkLoG43HZhyM2VVlnRw2wvU/ii2pwkrx DD/9A9L0=; b=cLc/a9IVIBwoP7cWPetuvwGqM6BR5c74SqH4UHy4561oOUtXBPr wNTMyz9Fn7e7+oopz4Gr4gpQJ6DEhI5uP0kx/VhGzG/f5ux5AV3cFoUiqRquLFVb MKpYVbTSksXbyEohPaMwJ//BGtB31xfKMrIYv4jbGn47dSWZL1s+/Hog= Received: from mx.zeyos.com ([127.0.0.1]) by mx.zeyos.com (mx.zeyos.com [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id 7aPM015Sfrrz for ; Tue, 23 May 2023 09:10:45 +0000 (UTC) Received: from cloud.zeyos.com (static.182.82.88.23.clients.your-server.de [23.88.82.182]) by mx.zeyos.com (Postfix) with ESMTPSA id 4QQT7T4Yv2z1tnvX; Tue, 23 May 2023 09:10:45 +0000 (UTC) Date: Tue, 23 May 2023 11:10:45 +0200 Subject: Re: Insertion Sort Improvements From: Benjamin Coutu To: John Naylor Cc: pgsql-hackers Message-ID: <042b841e324e22c79e23@zeyos.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Greetings, I would like to revisit the discussion and concur with John's perspective t= hat incremental progress through small, successive modifications is the app= ropriate approach to move forward. Therefore, I would like to propose two d= istinct ideas aimed at enhancing the functionality of insertion sort: 1. Implementation of a more efficient variant (as described in the introduc= tory part of this thread): ------------ OLD ------------ for (pm =3D a + ST_POINTER_STEP; pm < a + n * ST_POINTER_STEP; =09 pm +=3D ST_POINTER_STEP) =09for (pl =3D pm; pl > a && DO_COMPARE(pl - ST_POINTER_STEP, pl) > 0; =09=09 pl -=3D ST_POINTER_STEP) =09=09DO_SWAP(pl, pl - ST_POINTER_STEP); ------------ NEW ------------ for ( =09pm =3D a + ST_POINTER_STEP; =09pm < a + n * ST_POINTER_STEP; =09pm +=3D ST_POINTER_STEP ) { =09ST_POINTER_TYPE tmp =3D *pm; =09=20 =09for ( =09=09pl =3D pm - ST_POINTER_STEP; =09=09pl >=3D a && DO_COMPARE(pl, &tmp) > 0; =09=09pl -=3D ST_POINTER_STEP =09) =09=09*(pl + ST_POINTER_STEP) =3D *pl; =09=09 =09*(pl + ST_POINTER_STEP) =3D tmp; } ------------ 2. It appears that there is a consensus against disregarding the presorted = check, despite its questionable value. In light of this, an alternative sug= gestion is to integrate the presorted check into the insertion sort path by= utilizing an unbounded insertion sort. Only after a maximum number of swap= s have occurred should we abandon the sorting process. If insertion sort is= executed on the entire array without any swaps, we can simply return. If n= ot, and we continue with quicksort after the swap limit has been reached, w= e at least have left the array in a more sorted state, which may likely be = advantageous for subsequent iterations. ------------ OLD ------------ if (n < 7) { =09for (pm =3D a + ST_POINTER_STEP; pm < a + n * ST_POINTER_STEP; =09=09 pm +=3D ST_POINTER_STEP) =09=09for (pl =3D pm; pl > a && DO_COMPARE(pl - ST_POINTER_STEP, pl) > 0; =09=09=09 pl -=3D ST_POINTER_STEP) =09=09=09DO_SWAP(pl, pl - ST_POINTER_STEP); =09return; } presorted =3D 1; for (pm =3D a + ST_POINTER_STEP; pm < a + n * ST_POINTER_STEP; =09 pm +=3D ST_POINTER_STEP) { =09DO_CHECK_FOR_INTERRUPTS(); =09if (DO_COMPARE(pm - ST_POINTER_STEP, pm) > 0) =09{ =09=09presorted =3D 0; =09=09break; =09} } if (presorted) =09return; ------------ NEW ------------ #define LIMIT_SWAPS 30 /* to be determined empirically */ int swaps =3D 0; =09 for (pm =3D a + ST_POINTER_STEP; pm < a + n * ST_POINTER_STEP; =09 pm +=3D ST_POINTER_STEP) =09for (pl =3D pm; pl > a && DO_COMPARE(pl - ST_POINTER_STEP, pl) > 0; =09=09 pl -=3D ST_POINTER_STEP) { =09=09DO_SWAP(pl, pl - ST_POINTER_STEP); =09=09 =09=09if (++swaps =3D=3D LIMIT_SWAPS) =09=09=09goto quicksort; =09} =09 if (swaps =3D=3D 0) =09return; =09 quicksort: ------------ Naturally, both modifications (with point 2 being highly speculative) are c= urrently independent of each other, and it is also crucial to benchmark the= combined variant as well as different values for LIMIT_SWAPS. I would greatly appreciate assistance in benchmarking these proposed change= s. Your collaboration in this matter would be invaluable. Cheers, Ben