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 1trkyP-0097Oo-Uw for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Mar 2025 21:47:46 +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 1trkyN-000u66-Hl for pgsql-hackers@arkaria.postgresql.org; Mon, 10 Mar 2025 21:47:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1trkyN-000u57-7m for pgsql-hackers@lists.postgresql.org; Mon, 10 Mar 2025 21:47:43 +0000 Received: from mail-qk1-x72b.google.com ([2607:f8b0:4864:20::72b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1trkyL-0026Zo-1C for pgsql-hackers@lists.postgresql.org; Mon, 10 Mar 2025 21:47:42 +0000 Received: by mail-qk1-x72b.google.com with SMTP id af79cd13be357-7c07b65efeeso450140385a.2 for ; Mon, 10 Mar 2025 14:47:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1741643260; x=1742248060; darn=lists.postgresql.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=XL+AGPDAqHyy2R3UeU1Jhhf/M2B1gdsAuImA/M6AmaU=; b=i5H3iadVtuG29VKCoULaKXbv3759vOb/7ufDEolgfpBt5RDWZXNFlItu6net2ZuxAv Q3SFLQ+d9ri9QTwqUU83pSYlCmP3ViIyxmNfY6xVxvwdTpvfvsr3f+p7iJ15ood44g5d Cxh3lq1mvaRfLzKSLCP7iBmZEoW5yEHuL8qX8SPUiHBuVrwXJWj/xSZLSzywaXLn/8lL 1iP1z1T2ust3uGmOctB4x4grFVlD011OmkQp/wcOphqLmFNulaYRi0j2LURLckMpxSB7 OhT0n+dAtuvRFAMe31upKyIPoeJHuDL1tM6yBUNhu3oAss6nMqzm9O8Cr45PWQLKp78Q xBLQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741643260; x=1742248060; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=XL+AGPDAqHyy2R3UeU1Jhhf/M2B1gdsAuImA/M6AmaU=; b=ap7yrlD3pEmEA9uRPAQeIl5DdECQ82T/0bHyCALDAz3ma1uJHRHHn6RmRGenNm50o/ DrEP0h7zCdTQrNgLT4AxJWtg1wCMdNmEhduhs7FKbPrrrdspJrQnCfpdZMccnHx23etu 4R8itwgyytXHzGr+ZPZdEaKtcMIZe4TZhYWuoQxdwvS4IFKAbHegK2U6LmMuWZFDNQby f23MgKBENoGkxeFSeh/zlJ5wiJ7lqIgPmrxoDCrBhbkjA4c5VYUp4ynGdZv/FC8oVTTp spsr+D2xop7gRgMd/DBXjbzrI257SrobECSd46R1xPJXEyNRuQQPsgtFEIspJOFChCQj me3Q== X-Forwarded-Encrypted: i=1; AJvYcCX60NDZsI4voH/CJx+yF2xAyZ1FtPZZ04VLsqN0Isuu9H2+LBSJRiXc+Kd3COadSBFyAiGTOStvprsXwfTA@lists.postgresql.org X-Gm-Message-State: AOJu0YwjEtlZZ7c8+9yLBR0V8LACXq8mwPQSxXeMpvV2Hf8A9XD1FGjC 8P6VgW+PxF3M6YZlBoVVNNt6hFlTLx7xZdF1+8/oZXlEbaP0fJHT X-Gm-Gg: ASbGncvDDpPt307TRBw04Z9mDhtWnq5ZwbLV9TmjxiBU7DhAVk10b4vcQMxDa7f+Py3 GNMGZ8eFcvK0fQ1CRhw7SPwFIdTiV4KD49cxBmj/Fs6zgrR5WUxh2G9g6sFrihC2K3FA1LFuodv XljTQg9C0yWz5VWYFk/+A//TgsPfGOirrzV5oyorvhASZTgQahkgc1Th8Dwv/GijwA9CS8umSv+ 4yfuVnRqh5ZrmHKVZS3PHySbbFlnKaVDdeN62rok3giVyWnmRX6ePpE7A+PBgs9i7lnUyPdzgsz SMooUJk5Y0Bp5AXS/jSq3FWvYcbCf4Wk3jflkHCN00N+yLjhAJTJvthZGT0qt7iaWOp8YMNGu8E DauEK6Lwle/rzYoUJxsrte5QUZfZGFUDBTp4= X-Google-Smtp-Source: AGHT+IFRF3IF8u5bJnbFDUiLWz0HWGPJ5YetjqfSwV1Q1UgFTTdPuuaI2mf2LofOJBpaYDcGLHBLew== X-Received: by 2002:a05:620a:1b95:b0:7c5:5343:8c2c with SMTP id af79cd13be357-7c55e886113mr268163385a.27.1741643260458; Mon, 10 Mar 2025 14:47:40 -0700 (PDT) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id af79cd13be357-7c55e601e97sm51175685a.44.2025.03.10.14.47.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 10 Mar 2025 14:47:39 -0700 (PDT) Date: Mon, 10 Mar 2025 16:47:37 -0500 From: Nathan Bossart To: John Naylor Cc: "Devulapalli, Raghuveer" , "pgsql-hackers@lists.postgresql.org" , "Shankaran, Akash" Subject: Re: Improve CRC32C performance on SSE4.2 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Mar 10, 2025 at 03:48:31PM +0700, John Naylor wrote: > On Tue, Mar 4, 2025 at 2:11 AM Nathan Bossart 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