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 1oRZyY-0005Pn-Vg for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Aug 2022 14:06:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oRZyX-0004LD-VL for pgsql-hackers@arkaria.postgresql.org; Fri, 26 Aug 2022 14:06:21 +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 1oRZyX-0003tR-JG for pgsql-hackers@lists.postgresql.org; Fri, 26 Aug 2022 14:06:21 +0000 Received: from mx.zeyos.com ([88.99.153.70]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oRZyV-0006ra-A2 for pgsql-hackers@postgresql.org; Fri, 26 Aug 2022 14:06:20 +0000 Received: from mx.zeyos.com (localhost [127.0.0.1]) by mx.zeyos.com (Postfix) with ESMTP id AE0CF615DB for ; Fri, 26 Aug 2022 16:06:16 +0200 (CEST) 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:content-type:mime-version :references:in-reply-to:message-id:to:from:from:subject:subject :date:date; s=dkim; t=1661522776; x=1662386777; bh=UJmSYZVy7XHc+ g2uUiOghfjIX0+39G+oesFxnnDGDzA=; b=q34tkFVjg9VxALPSs3uOan9FJOSqv mTGBBn+ENgbsAOWWa94b50UwSr3SKPOIHBimSUyI1WcEIeh4B550gjtb0eRA5fYq Htr7qPxpdmfm0KB1+0B5tstWvFwo8zpY+zNfh+XB+2frWzPnIoLVSq1MgagRBiX9 WzRHuzuYGKhCS8= X-Virus-Scanned: Debian amavisd-new at mx.zeyos.com 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 321DVY0fXzm8 for ; Fri, 26 Aug 2022 16:06:16 +0200 (CEST) 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 6DA4160D7C; Fri, 26 Aug 2022 16:06:16 +0200 (CEST) Date: Fri, 26 Aug 2022 16:06:16 +0200 Subject: Re: Insertion Sort Improvements From: Benjamin Coutu To: John Naylor Cc: pgsql-hackers Message-ID: 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 > convenient if you know the type at compile time. See the attached, > which I had laying around when I was looking at PDQuicksort. I believe > it's similar to what you have in mind.=0A=0AThat looks very promising.=0A= I also love your recent proposal of partitioning into null and non-null. I = suspect that to be a clear winner.=0A > I think it belongs around 10 now anyway.=0A=0AYeah, I think that change i= s overdue given modern hardware characteristics (even with the current impl= ementation).=0A=0AAnother idea could be to run a binary insertion sort and = use a much higher threshold. This could significantly cut down on compariso= ns (especially with presorted runs, which are quite common in real workload= s).=0A=0AIf full binary search turned out to be an issue regarding cache lo= cality, we could do it in smaller chunks, e.g. do a micro binary search bet= ween the current position (I) and position minus chunk size (say 6-12 or so= , whatever fits 1 or 2 cachelines) whenever A[I] < A[I-1] and if we don't f= ind the position within that chunk we continue with the previous chunk, the= reby preserving cache locality.=0A=0AWith less comparisons we should start = keeping track of swaps and use that as an efficient way to determine presor= tedness. We could change the insertion sort threshold to a swap threshold a= nd do insertion sort until we hit the swap threshold. I suspect that would = make the current presorted check obsolete (as binary insertion sort without= or even with a few swaps should be faster than the current presorted-check= ).=0A=0ACheers, Ben