Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tEW6k-007rLI-6Y for pgsql-hackers@arkaria.postgresql.org; Fri, 22 Nov 2024 16:02:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tEW6i-009qPD-QA for pgsql-hackers@arkaria.postgresql.org; Fri, 22 Nov 2024 16:02:08 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tEW6i-009qOy-Bu for pgsql-hackers@lists.postgresql.org; Fri, 22 Nov 2024 16:02:08 +0000 Received: from lahtoruutu.iki.fi ([185.185.170.37]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tEW6a-003J1b-Ob for pgsql-hackers@lists.postgresql.org; Fri, 22 Nov 2024 16:02:07 +0000 Received: from [192.168.1.110] (dsl-hkibng22-50ddb7-241.dhcp.inet.fi [80.221.183.241]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: hlinnaka) by lahtoruutu.iki.fi (Postfix) with ESMTPSA id 4Xw0Fv3hjsz49Px6; Fri, 22 Nov 2024 18:01:23 +0200 (EET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1732291284; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sZQlRcCHkpk4VMGL7vKWQslo1YX0UIJl6w0D66Y453g=; b=uxSMPMR1/vFK/yxF2KTmgIhVQXX3VhS8UQ48GW4xnlMCh65ig/SLX3gNQfac3tBXJ6KgKP 0jUJbd645joATImQAAANjz09ybIsgnZWV4f7vXoY4nL/11bxbxyBBKQ1xMYDHgEfl2GtEJ XPKxiudQt19AFtWjlmm7qMtcSUtQ7OmsVAzKZON2wf1AknhMECZmpXJU88i+QQtIHgwYc2 +C7z5x1nQS1vPOHa+W6WYFmJpmAQeRz86u2M6sg+5WWxiu3VjGXugQqs7YX3KcRKH3r2J4 9X98SFLFts6UHLHnO52IHNjORs5GJ0+amSJNt+C30c79r9Tdugfm2021jQu9Rw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=iki.fi; s=lahtoruutu; t=1732291284; 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: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=sZQlRcCHkpk4VMGL7vKWQslo1YX0UIJl6w0D66Y453g=; b=PGEPRhJ2zee/1acR7Ys9Er0GR1CBQOWk1w1RXWliVoOQ62UId9TY16nuawzcVrI2ca07Xx XZnOlAyfY+iNdCP17MpXADOtJSbW7X8BejDU0D85tjthoogRopEeAVfdzHDz1u/NbPXke6 mG/twS6DTBawMVxKgkVPgpa95sbeMaGe7CABfuO7CGYP5lgLumboT/9MP1i3/aeo7WNuB5 HlOasLy4YrqZfGy9FoAPNfFh7kCOvNe+EsqqoSA9aE89Z/Ylhv+D/hoknVCCIZuy01Ujj0 8I7iqStMZnMyOhZ/nK9YMrvRormGCgcJ0gKzuKxU4zAjkKR4tD3g2lniC7Z2jw== ARC-Seal: i=1; s=lahtoruutu; d=iki.fi; t=1732291284; a=rsa-sha256; cv=none; b=Dq8fN2P6KjKiBFi8q7YjDwp9eRu4eTiPb/xhn9Nr8RqlA5hawu5iP1MOCqvmAa/oZWkZIh b4Doyx83AjouXZOWxBVKyOguCAhZVcB9DJTO/1B069uOoWrf7M3kjsDs8UajDTd7nHuEf+ I7ulXaOR6S2ckYHjnJ3vOo+XfSA+8DcTeKFo8SRPNv4G9ziRSRvOO8owb5PQdIX8/MxTR7 1jY+9vLAEDVTWn++qV/lybr6CqyM1m/YKp7xFnN+VvHB59H9Uso3KGYgNs82/0PCVIsaqy 7X1vT6tdDyx8eTb9nUfezlVafG92NZHkYy0KhBNzEVRdrQ59iv5ogVC2EyxnrQ== ARC-Authentication-Results: i=1; ORIGINATING; auth=pass smtp.auth=hlinnaka smtp.mailfrom=hlinnaka@iki.fi Message-ID: Date: Fri, 22 Nov 2024 18:01:22 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: SIMD optimization for list_sort To: "Giacchino, Luca" , "pgsql-hackers@lists.postgresql.org" Cc: "R, Rakshit" , "Shankaran, Akash" , "Devulapalli, Raghuveer" References: Content-Language: en-US From: Heikki Linnakangas In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 22/11/2024 01:27, Giacchino, Luca wrote: > The existing list_sort takes a comparator function to compare pairs of > ListCell. On the other hand, x86-simd-sort requires an array of numeric > values to sort, and it returns an array of sorted indices. To enable > x86-simd-sort, we add new list_sort_simd functions that take an > extractor function. The function extracts a value (float or uint32) from > a ListCell. Those values are then collected into an array for x86-simd- > sort to work on. A comparator function can still be passed to be used as > tie-breaker. > > typedef float (*list_sort_extractor_float)(const ListCell *a); > > typedef uint32 (*list_sort_extractor_uint32)(const ListCell *a); > > void list_sort_simd_float(List *list, list_sort_extractor_float extract, > list_sort_comparator cmp); > > void list_sort_simd_uint32(List *list, list_sort_extractor_uint32 > extract, list_sort_comparator cmp); > > These functions will exist alongside the current list_sort. Existing > list_sort use cases in Postgres or extensions will not be affected by > default, and they can be converted to list_sort_simd functions where it > makes sense in terms of performance. I'd suggest targeting pg_qsort() directly, instead of list_sort(). list_sort() is not used in very performance critical parts. > We identified a first use case for list_sort_simd_float in pgvector. As > part of HNSW index construction, pgvector uses list_sort to sort > candidate vectors by distance. Using list_sort_simd_float, we observed > reduction in index build time in some scenarios. For example, we > observed 7% reduction in index build time with the gist-960 dataset and > 10% with the dbpedia-openai-1000k dataset (ANN-Benchmarks, HNSW index > with halfvec, m=80). We are also looking into microbenchmarks to measure > list_sort performance independently. That's interesting. I'd suggest proposing this to the pgvector project directly, since pgvector would immediately benefit. > We’d appreciate feedback on this approach. In the meantime, we will > complete the patch to share. We also plan to extend SIMD-based sort to > tuple sort in the future. If you could use this to speed up tuple sorting, that would be much more interesting for PostgreSQL itself. -- Heikki Linnakangas Neon (https://neon.tech)