Subject: Re: bug#46111: Reverting fns.c hash function due to OpenBSD/SPARC64 compile breaking

From: Eli Zaretskii <[email-masked]>
Date: Thu, 28 Jan 2021 19:22:47 +0200

> From: Stefan Monnier <[email-masked]>
> Date: Thu, 28 Jan 2021 11:17:31 -0500
> Cc: [email-masked]
> 
> +      /* Here `p` is *almost* always be properly aligned, so we want to
> +         optimize for the aligned case, but we still need to support the
> +         non-aligned case.  */
> +      /* FIXME: Could we just always use `memcpy` and rely on GCC optimizing
> +         it to a single word-sized memory access on all-but-sparc64?  */
> +#ifdef __sparc64__  /* Arch that still insists on aligned memory accesses. */
> +      EMACS_UINT c;
> +      if (!((unsigned long)p) % sizeof (c))
> +        c = *p;
> +      else
> +        memcpy (&c, (char const *)p, sizeof (c)); /* `p` is unaligned!  */
> +#else
>        EMACS_UINT c = *p;
> +#fi

AFAIK, memcpy itself optimizes: once it gets to aligned address, it
starts copying words instead of bytes.  So I'm not sure we need either
#ifdef or the run-time condition.  I suggest to time both variants on
x86 architecture, to see whether there's any performance hit due to
use of memcpy, and if not, use memcpy always -- it will let us have
the cake and eat it, too.