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 1q1hXN-0001Yq-NI for pgsql-hackers@arkaria.postgresql.org; Wed, 24 May 2023 05:59: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 1q1hXM-0005Qs-Ir for pgsql-hackers@arkaria.postgresql.org; Wed, 24 May 2023 05:59:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q1hXM-0005Qj-8E for pgsql-hackers@lists.postgresql.org; Wed, 24 May 2023 05:59:52 +0000 Received: from mx.zeyos.com ([2a01:4f8:c012:b02b::1]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q1hXJ-001aVr-E4 for pgsql-hackers@postgresql.org; Wed, 24 May 2023 05:59:51 +0000 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id 4QR0rd2lBmz1tnvc for ; Wed, 24 May 2023 05:59: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= 1684907985; x=1685771986; bh=Q/qvzq9/m3OjaRjTLZhdPRcxUoAWIJJq4O4 7DdQn59c=; b=vpHwQl74BHGZ39w6R9Vp0Qra/Ln46tqpPSRfbiqxPqoDkmd85/N PuM/2tgMl4nK+cVAKojAzCqOwo4R+eV+b5KpfkCS8nsSB+2pgc3Q42RyDe9DiqA5 skBuVQCcD8VEb9Liy9Tpmhtla0SJwzIkNBETgfV6aD89d1X5nQSF0Yzg= 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 UblCUkhCCkGz for ; Wed, 24 May 2023 05:59: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 4QR0rd1RYgz1tnvX; Wed, 24 May 2023 05:59:45 +0000 (UTC) Date: Wed, 24 May 2023 07:59:45 +0200 Subject: Re: Insertion Sort Improvements From: Benjamin Coutu To: John Naylor Cc: pgsql-hackers Message-ID: <96811ebc460cd665205f@zeyos.com> In-Reply-To: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > That's worth trying out. It might also then be worth trying to push both = unordered values -- the big one up / the small one down. I've seen other im= plementations do that, but don't remember where, or what it's called. It is important that we do not do 2 compares two avoid one copy (assignment= to temporary) as you did in your patch earlier in this thread, cause compa= res are usually pretty costly (also two compares are then inlined, bloating= the binary). Assigning a sort tuple to a temporary translates to pretty simple assembly = code, so my suggested modification should outperform. It cuts down the cost= of the inner loop by ca. 40% comparing the assembly. And it avoids having = to re-read memory on each comparison, as the temporary can be held in regis= ters. > "Unbounded" means no bounds check on the array. That's not possible in it= s current form, so I think you misunderstood something. Sorry for the confusion. I didn't mean unbounded in the "array bound checki= ng" sense, but in the "unrestricted number of loops" sense. > I only remember implementations tracking loop iterations, not swaps. You'= d need evidence that this is better. Well, the idea was to include the presorted check somehow. Stopping after a= certain number of iterations is surely more safe than stopping after a num= ber of swaps, but we would then implicitly also stop our presort check. We = could change that though: Count loop iterations and on bailout continue wit= h a pure presort check, but from the last position of the insertion sort --= not all over again -- by comparing against the maximum value recorded duri= ng the insertion sort. Thoughts? > An important part not mentioned yet: This might only be worth doing if th= e previous recursion level performed no swaps during partitioning and the c= urrent pivot candidates=C2=A0are ordered. Agreed. > It's currently 7, but should really be something like 10. A script that r= epeats tests for, say, 7 through 18 should show a concave-up shape in the t= imes. The bottom of the bowl should shift to higher values, and that minimu= m is what should be compared. Yeah, as alluded to before, it should be closer to 10 nowadays. In any case it should be changed at least from 7 to 8, cause then we could = at least discard the additional check for n > 7 in the quicksort code path = (see /src/include/lib/sort_template.h#L322). Currently we check n < 7 and a= few lines down we check for n > 7, if we check n < 8 for insertion sort th= en the second check becomes obsolete. Benjamin Coutu http://www.zeyos.com