pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Nathan Bossart <nathandbossart@gmail.com>
To: John Naylor <johncnaylorls@gmail.com>
Cc: Devulapalli, Raghuveer <raghuveer.devulapalli@intel.com>
Cc: pgsql-hackers@lists.postgresql.org <pgsql-hackers@lists.postgresql.org>
Cc: Shankaran, Akash <akash.shankaran@intel.com>
Subject: Re: Improve CRC32C performance on SSE4.2
Date: Mon, 10 Mar 2025 16:47:37 -0500
Message-ID: <Z89d-eRP9Tpd5Mq1@nathan> (raw)
In-Reply-To: <CANWCAZabmia25iu8Z_qRhLKoOV1VxhcSMkJuzDomQrA2RWdTUA@mail.gmail.com>
References: <PH8PR11MB828602D3AE9A19531E42A2DBFBC02@PH8PR11MB8286.namprd11.prod.outlook.com>
	<CANWCAZapv-=PcDBEd43AEiYZApweANpS=stOcSvaPowo6Ff1yw@mail.gmail.com>
	<PH8PR11MB82867CEDDECB60530DB96BD1FBC22@PH8PR11MB8286.namprd11.prod.outlook.com>
	<CANWCAZbG_pPFuLRaczYApBykaHCXQ-STAXO1=J6_4TRvy23Byg@mail.gmail.com>
	<PH8PR11MB8286B017901C2C30665B106CFBC22@PH8PR11MB8286.namprd11.prod.outlook.com>
	<CANWCAZZjuH8daxK-tcnYbK_sAQdq8R6d3T3iGfL43Dj6DkFv0g@mail.gmail.com>
	<PH8PR11MB82862B464609340FB0A4BA37FBCD2@PH8PR11MB8286.namprd11.prod.outlook.com>
	<CANWCAZZEiTzhZcuwTiJ2=opiNpAUn1vuDRu1N02z61AthwRZLA@mail.gmail.com>
	<Z8X-0wg7wXztjMQ2@nathan>
	<CANWCAZabmia25iu8Z_qRhLKoOV1VxhcSMkJuzDomQrA2RWdTUA@mail.gmail.com>

On Mon, Mar 10, 2025 at 03:48:31PM +0700, John Naylor wrote:
> On Tue, Mar 4, 2025 at 2:11 AM Nathan Bossart <nathandbossart@gmail.com> wrote:
>> Overall, I wish we could avoid splitting things into separate files and
>> adding more header file gymnastics, but maybe there isn't much better we
>> can do without overhauling the CPU feature detection code.
> 
> I wanted to make an attempt to make this aspect nicer. v13-0002
> incorporates deliberately compact and simple loops for inlined
> constant input into the dispatch function, and leaves the existing
> code alone. This avoids code churn and saves vertical space in the
> copied code. It needs a bit more commentary, but I hope this is a more
> digestible prerequisite to the CLMUL algorithm -- as a reminder, it'll
> be simpler if we can always assume non-constant input can go through a
> function pointer.

That is certainly more readable.  FWIW I think it would be entirely
reasonable to replace the pg_crc32c_sse42.c implementation with a call to
this new pg_comp_crc32c_dispatch() function.  Of course, you'd have to
split things up like:

	pg_attribute_no_sanitize_alignment()
	static inline
	pg_crc32c
	pg_comp_crc32c_sse42_inline(pgcrc32c crc, const void *data, size_t len)
	{
		const unsigned char *p = data;

	#ifdef __x86_64__
		for (; len >= 8; p += 8, len -= 8)
			crc = _mm_crc32_u64(crc, *(const uint64 *) p);
	#endif
		for (; len >= 4; p += 4, len -= 4)
			crc = _mm_crc32_u32(crc, *(const uint32 *) p);
		for (; len > 0; len--)
			crc = _mm_crc32_u8(crc, *p++)
		return crc;
	}

	pg_attribute_no_sanitize_alignment()
	static inline
	pg_crc32c
	pg_comp_crc32c_dispatch(pg_crc32c crc, const void *data, size_t len)
	{
		if (__builtin_constant_p(len))
			return pg_comp_crc32c_sse42_inline(crc, data, len);
		return pg_comp_crc32c_sse42(crc, data, len);
	}

And then in pg_crc32c_sse42.c:

	pg_attribute_no_sanitize_alignment()
	pg_attribute_target("sse4.2")
	pg_crc32c
	pg_comp_crc32c_sse42(pg_crc32c crc, const void *data, size_t len)
	{
		return pg_comp_crc32c_sse42_inline(crc, data, len);
	}

Granted, that adds back some of the complexity you were trying to avoid
(and is very similar to your v12 patches), but it's pretty compact and
avoids some code duplication.  FTR I don't feel too strongly about this,
but on balance I guess I'd be okay with a tad more complexity than your
v13 patches if it served a useful purpose.

-- 
nathan





view thread (82+ messages)  latest in thread

Message-ID: <Z89d-eRP9Tpd5Mq1@nathan>
Permalink:  ../Z89d-eRP9Tpd5Mq1@nathan/
Also on:    postgresql.org/message-id/Z89d-eRP9Tpd5Mq1@nathan

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: nathandbossart@gmail.com, johncnaylorls@gmail.com, raghuveer.devulapalli@intel.com, akash.shankaran@intel.com
  Subject: Re: Improve CRC32C performance on SSE4.2
  In-Reply-To: <Z89d-eRP9Tpd5Mq1@nathan>
  References: <PH8PR11MB828602D3AE9A19531E42A2DBFBC02@PH8PR11MB8286.namprd11.prod.outlook.com> <CANWCAZapv-=PcDBEd43AEiYZApweANpS=stOcSvaPowo6Ff1yw@mail.gmail.com> <PH8PR11MB82867CEDDECB60530DB96BD1FBC22@PH8PR11MB8286.namprd11.prod.outlook.com> <CANWCAZbG_pPFuLRaczYApBykaHCXQ-STAXO1=J6_4TRvy23Byg@mail.gmail.com> <PH8PR11MB8286B017901C2C30665B106CFBC22@PH8PR11MB8286.namprd11.prod.outlook.com> <CANWCAZZjuH8daxK-tcnYbK_sAQdq8R6d3T3iGfL43Dj6DkFv0g@mail.gmail.com> <PH8PR11MB82862B464609340FB0A4BA37FBCD2@PH8PR11MB8286.namprd11.prod.outlook.com> <CANWCAZZEiTzhZcuwTiJ2=opiNpAUn1vuDRu1N02z61AthwRZLA@mail.gmail.com> <Z8X-0wg7wXztjMQ2@nathan> <CANWCAZabmia25iu8Z_qRhLKoOV1VxhcSMkJuzDomQrA2RWdTUA@mail.gmail.com> <Z89d-eRP9Tpd5Mq1@nathan>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox