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 1t5YMI-006t3a-0Y for pgsql-hackers@arkaria.postgresql.org; Mon, 28 Oct 2024 22:37:10 +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 1t5YMF-00ALXO-9E for pgsql-hackers@arkaria.postgresql.org; Mon, 28 Oct 2024 22:37:07 +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 1t5YME-00ALXG-W0 for pgsql-hackers@lists.postgresql.org; Mon, 28 Oct 2024 22:37:07 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t5YMC-003UUa-IV for pgsql-hackers@lists.postgresql.org; Mon, 28 Oct 2024 22:37:06 +0000 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2fb498a92f6so45950751fa.1 for ; Mon, 28 Oct 2024 15:37:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1730155022; x=1730759822; darn=lists.postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=8iFClydnHqanRVHZLl1pQDnIaUt9Em50ZvYO5a6f2gM=; b=IFRnVszug4kiSB9vh2enHiDDU7nB9MYB271jaDVtiGfkCLwY18OdDTQSZsC3kccVh/ ARemDgBqBMq7GO97s3tCiGOu/z8tLq1E7jJhbH4IIsicev+S7bb8xnJqZ0+zQfF6pnXC m5ugu0Ti80H5nAYV159oQ/g2r6l4ENKUeGLac0Y3wUpeEaXwD5XB6rDKyDKsXuUe06Up sJy+tRYcPWzt3XzxoSAjF+Ac3FfS9p2N7AHxXOOJaMbZE0FfsphpSwjKywrDhK0SHrK9 WwEU8kA0yMuEa9o9djzvvzaia7wbBhRrrxicjDqp/ML5ws8N28ctTCUkV0Jhru8YK7gM SwdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730155022; x=1730759822; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8iFClydnHqanRVHZLl1pQDnIaUt9Em50ZvYO5a6f2gM=; b=iXqHWMKdt7Th737a/MykRcBWrApU7Q07bMaVft4A7DL8Hgg0fFjVRc0OE7gq8v6pSa qVp7GokipjjbdEN7IXax0j3oOdneJZRW5PQ/IA3CPhjUD8BSFsDnwhxurn3YiXTBE8xS msti1XO+O3TN6m44oa+z+KJJugNEdt96aIpQuxDmLhnqvfbXwiB50A7MYD9Sm7ys5Cge r0s3T1s1WYQ+8hRd4Cg0wlqr6zxCJT9hHnAXC9C11m2+HQ407/sz4jOYWVEUTFPVzFHT QiA93RM3B1N6FM9KFdJPltabScanXCCVpS8iMfQM7JXDmqFkeBPM3OGDl55/xuazRnfr 2L8A== X-Forwarded-Encrypted: i=1; AJvYcCWk9EjlW40mn+dT6cXWSJ24PWmd/WoC0B1kEFeLgXEqCDUJeDYtsonIM/hM5IwGdhTBPpyPrc426ru8ZeLi@lists.postgresql.org X-Gm-Message-State: AOJu0YyYW0FqJkBe72Wwsqi393ZIUDD2i9XZGL8dQitfxcwMXeEDzQiU RYNA7zcHNZnhZp+ueZQZU+yXczSkrAUPhdBDUpn5+OGVgfEaX72xaLFcMtNdsQ4OuTIp4ptiAl6 adZz/0Tb4E1Ksmfs0kPmoM4Cf39UhN91/ X-Google-Smtp-Source: AGHT+IHJdAixOXwiaNjfUBSPmv1SU9Qtydz8nYCc+y6ezDhFxf2BtW3XiqDe+dOyLssKX/FiLBBk1p9xWQF6lKBTFGM= X-Received: by 2002:a2e:4619:0:b0:2f3:a06a:4c5 with SMTP id 38308e7fff4ca-2fcbe09b9f7mr36522621fa.29.1730155022108; Mon, 28 Oct 2024 15:37:02 -0700 (PDT) MIME-Version: 1.0 References: <3023d29e-0fe7-4bec-9028-7f3c0102e437@eisentraut.org> In-Reply-To: From: Peter Smith Date: Tue, 29 Oct 2024 09:36:34 +1100 Message-ID: Subject: Re: define pg_structiszero(addr, s, r) To: Bertrand Drouvot Cc: Peter Eisentraut , pgsql-hackers@lists.postgresql.org 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 On Thu, Sep 19, 2024 at 4:57=E2=80=AFAM Bertrand Drouvot wrote: > > Hi, > > On Wed, Sep 18, 2024 at 10:03:21AM +0200, Peter Eisentraut wrote: > > On 18.09.24 06:16, Bertrand Drouvot wrote: > > > +#define pg_structiszero(addr, s, r) = \ > > > + do { = \ > > > + /* We assume this initializes to zeroes */ = \ > > > + static const s all_zeroes; = \ > > > + r =3D (memcmp(addr, &all_zeroes, sizeof(all_zeroes)) =3D= =3D 0); \ > > > + } while (0) > > > > Thanks for the feedback. > > > This assumption is kind of the problem, isn't it? Because, you can't a= ssume > > that. And the existing code is arguably kind of wrong. But moreover, = this > > macro also assumes that the "addr" argument has no random padding bits. > > > > In the existing code, you can maybe make a local analysis that the code= is > > working correctly, although I'm not actually sure. > > I think it is but will give it a closer look. > > > But if you are > > repackaging this as a general macro under a general-sounding name, then= the > > requirements should be more stringent. > > Agree. That said in v2 ([1]), it has been renamed to pgstat_entry_all_zer= os(). > > I think that I will: > > 1/ take a closer look regarding the existing assumption > 2/ if 1/ outcome is fine, then add more detailed comments around > pgstat_entry_all_zeros() to make sure it's not used outside of the existi= ng > context > > Sounds good to you? > > [1]: https://www.postgresql.org/message-id/ZuqHLCdZXtEsbyb/%40ip-10-97-1-= 34.eu-west-3.compute.internal > > Regards, > > -- > Bertrand Drouvot > PostgreSQL Contributors Team > RDS Open Source Databases > Amazon Web Services: https://aws.amazon.com > Hi, if this is performance critical then FWIW my understanding is that memcmp can outperform simple loop checking, and my hacky testing seemed to confirm that. See https://godbolt.org/z/GWY1ob9bE static inline bool is_all_zeros2(const char *p, size_t len) { static size_t nz =3D 0; static const char *z =3D NULL; if (nz < len) { if (z) free((void *)z); nz =3D len; z =3D (const char *)calloc(1, nz); } return memcmp(p, z, len) =3D=3D 0; } ~~ Executor x86-64 gcc 14.2 (C, Editor #1) Program stdout Iterate 1000000 times... check zeros using loop -- elapsed=3D0.041196s check zeros using memcmp -- elapsed=3D0.016407s =3D=3D=3D=3D=3D=3D Kind Regards, Peter Smith. Fujitsu Australia