Re: define pg_structiszero(addr, s, r)

Michael Paquier <michael@paquier.xyz>

From: Michael Paquier <michael@paquier.xyz>
To: Ranier Vilela <ranier.vf@gmail.com>
Cc: Bertrand Drouvot <bertranddrouvot.pg@gmail.com>, David Rowley <dgrowleyml@gmail.com>, Peter Smith <smithpb2250@gmail.com>, Peter Eisentraut <peter@eisentraut.org>, Heikki Linnakangas <hlinnaka@iki.fi>, pgsql-hackers@lists.postgresql.org
Date: 2024-11-13T00:33:31Z
Lists: pgsql-hackers
On Tue, Nov 12, 2024 at 01:32:36PM -0300, Ranier Vilela wrote:
> See v1_allzeros_small.c attached.

In your pg_memory_is_all_zeros_v11:
    while (((uintptr_t) p & (sizeof(size_t) - 1)) != 0)
    {
        if (p == end)
            return true;

        if (*p++ != 0)
            return false;
    }

    if (len > sizeof(size_t) * 8)
    {
      for (; p < aligned_end - (sizeof(size_t) * 7); p += sizeof(size_t) * 8)
      {
          if ((((size_t *) p)[0] != 0) | (((size_t *) p)[1] != 0) |
              (((size_t *) p)[2] != 0) | (((size_t *) p)[3] != 0) |
              (((size_t *) p)[4] != 0) | (((size_t *) p)[5] != 0) |
              (((size_t *) p)[6] != 0) | (((size_t *) p)[7] != 0))
              return false;
      }
    }

If I'm reading that right, this could still read a couple of bytes
past the wanted memory area.  For example, imagine a case of 65 bytes
with a location a bit unaligned (more than 2 bytes).  You'd want to
check the remaining size after the first loop, not the initial one.

I'd be OK to have a quick loop for the less-than-64-byte case rather
than more checks depending on sizeof(size_t) spread, like Bertrand is
suggesting.  I'd like to imagine that compilers would like that a bit
better, though I am not completely sure, either.
--
Michael

Commits

  1. Use pg_memory_is_all_zeros() in PageIsVerifiedExtended()

  2. Optimize pg_memory_is_all_zeros() in memutils.h

  3. Remove use of pg_memory_is_all_zeros() in bufpage.c

  4. Add pg_memory_is_all_zeros() in memutils.h