You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@subversion.apache.org by Philip Martin <ph...@wandisco.com> on 2012/06/01 14:22:35 UTC

COPY_TWO_BYTES and C strict aliasing

GCC gives a compiler warning where the COPY_TWO_BYTES macro is used.  A
typical warning is:

../src/subversion/libsvn_subr/string.c:971:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
           COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
           ^

The code is:

/* Copy the two bytes at SOURCE[0] and SOURCE[1] to DEST[0] and DEST[1] */
#if SVN_UNALIGNED_ACCESS_IS_OK
#  define COPY_TWO_BYTES(dest,source)\
      *(apr_uint16_t*)(dest) = *(apr_uint16_t*)(source);
#else
#  define COPY_TWO_BYTES(dest,source) \
    do { \
      (dest)[0] = (source)[0]; \
      (dest)[1] = (source)[1]; \
    } while (0)
#endif

apr_size_t
svn__ui64toa(char * dest, apr_uint64_t number)
{
  char buffer[SVN_INT64_BUFFER_SIZE];
  apr_uint32_t reduced;   /* used for 32 bit DIV */
  char* target;

  /* Small numbers are by far the most common case.
   * Therefore, we use special code.
   */
  if (number < 100)
    {
      if (number < 10)
        {
          dest[0] = (char)('0' + number);
          dest[1] = 0;
          return 1;
        }
      else
        {
          COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
          dest[2] = 0;
          return 2;
        }
    }


Is the warning something we can ignore?  My understanding of C aliasing
is that writing to memory is only supposed to happen as the declared
type of that memory.

Is COPY_TWO_BYTES a significant optimisation?  On Linux we can avoid the
warning by simply using

   memcpy(dest, source, 2)

since that memcpy call will be inlined these days.

-- 
uberSVN: Apache Subversion Made Easy
http://www.uberSVN.com

Re: COPY_TWO_BYTES and C strict aliasing

Posted by Stefan Fuhrmann <eq...@web.de>.
Am 01.06.2012 14:22, schrieb Philip Martin:
> GCC gives a compiler warning where the COPY_TWO_BYTES macro is used.  A
> typical warning is:
>
> ../src/subversion/libsvn_subr/string.c:971:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
>             COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
>             ^
>
> The code is:
>
> /* Copy the two bytes at SOURCE[0] and SOURCE[1] to DEST[0] and DEST[1] */
> #if SVN_UNALIGNED_ACCESS_IS_OK
> #  define COPY_TWO_BYTES(dest,source)\
>        *(apr_uint16_t*)(dest) = *(apr_uint16_t*)(source);
> #else
> #  define COPY_TWO_BYTES(dest,source) \
>      do { \
>        (dest)[0] = (source)[0]; \
>        (dest)[1] = (source)[1]; \
>      } while (0)
> #endif
>
> apr_size_t
> svn__ui64toa(char * dest, apr_uint64_t number)
> {
>    char buffer[SVN_INT64_BUFFER_SIZE];
>    apr_uint32_t reduced;   /* used for 32 bit DIV */
>    char* target;
>
>    /* Small numbers are by far the most common case.
>     * Therefore, we use special code.
>     */
>    if (number<  100)
>      {
>        if (number<  10)
>          {
>            dest[0] = (char)('0' + number);
>            dest[1] = 0;
>            return 1;
>          }
>        else
>          {
>            COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
>            dest[2] = 0;
>            return 2;
>          }
>      }
>
>
> Is the warning something we can ignore?  My understanding of C aliasing
> is that writing to memory is only supposed to happen as the declared
> type of that memory.
>
> Is COPY_TWO_BYTES a significant optimisation?  On Linux we can avoid the
> warning by simply using
>
>     memcpy(dest, source, 2)
>
> since that memcpy call will be inlined these days.
>
I just verified that memcpy will indeed be properly inlined.
Switched to memcpy as per your suggestion in r1345883.

Thanks,
Stefan^2.

Re: COPY_TWO_BYTES and C strict aliasing

Posted by Philip Martin <ph...@wandisco.com>.
Branko Čibej <br...@apache.org> writes:

> On 01.06.2012 14:22, Philip Martin wrote:
>> GCC gives a compiler warning where the COPY_TWO_BYTES macro is used.  A
>> typical warning is:
>>
>> ../src/subversion/libsvn_subr/string.c:971:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
>>            COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
>>            ^
> [...]
>
>
>> Is the warning something we can ignore?  My understanding of C aliasing
>> is that writing to memory is only supposed to happen as the declared
>> type of that memory.
>>
>> Is COPY_TWO_BYTES a significant optimisation?  On Linux we can avoid the
>> warning by simply using
>>
>>    memcpy(dest, source, 2)
>>
>> since that memcpy call will be inlined these days.
>>
>
> I think it's OK if that macro becomes an alias for memcpy. On modern
> systems I'd even expect it to be faster than the current code, since a
> properly inlined memcpy will know when to pick word-size over byte-size
> operations, since it'll know the alignment of the source and target
> (which this macro doesn't).

With Linux/gcc using memcpy causes the generated assembler to be ordered
slightly differently but it appears to be the same instructions (to my
inexpert eye):

10,19c10,19
< 	jbe	.L281
< 	.loc 1 971 0
< 	leaq	decimal_table(%rip), %rax
< 	movzwl	(%rax,%rsi,4), %eax
< 	.loc 1 972 0
< 	movb	$0, 2(%rdi)
< 	.loc 1 971 0
< 	movw	%ax, (%rdi)
< 	.loc 1 973 0
< 	movl	$2, %eax
---
> 	ja	.L271
> 	.loc 1 965 0
> 	addl	$48, %esi
> 	.loc 1 966 0
> 	movb	$0, 1(%rdi)
> 	.loc 1 967 0
> 	movl	$1, %eax
> 	.loc 1 965 0
> 	movb	%sil, (%rdi)
> 	.loc 1 1019 0
183,192c183,192
< .L281:
< 	.loc 1 965 0
< 	addl	$48, %esi
< 	.loc 1 966 0
< 	movb	$0, 1(%rdi)
< 	.loc 1 967 0
< 	movl	$1, %eax
< 	.loc 1 965 0
< 	movb	%sil, (%rdi)
< 	.loc 1 1019 0
---
> .L271:
> 	.loc 1 971 0
> 	leaq	decimal_table(%rip), %rax
> 	.loc 1 972 0
> 	movb	$0, 2(%rdi)
> 	.loc 1 971 0
> 	movzwl	(%rax,%rsi,4), %eax
> 	movw	%ax, (%rdi)
> 	.loc 1 973 0
> 	movl	$2, %eax

That's replacing

  *(apr_uint16_t*)(dest) = *(apr_uint16_t*)(source)

with

  memcpy((dest), (source), 2)

-- 
uberSVN: Apache Subversion Made Easy
http://www.uberSVN.com

Re: COPY_TWO_BYTES and C strict aliasing

Posted by Branko Čibej <br...@apache.org>.
On 01.06.2012 14:22, Philip Martin wrote:
> GCC gives a compiler warning where the COPY_TWO_BYTES macro is used.  A
> typical warning is:
>
> ../src/subversion/libsvn_subr/string.c:971:11: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
>            COPY_TWO_BYTES(dest, decimal_table[(apr_size_t)number]);
>            ^
[...]


> Is the warning something we can ignore?  My understanding of C aliasing
> is that writing to memory is only supposed to happen as the declared
> type of that memory.
>
> Is COPY_TWO_BYTES a significant optimisation?  On Linux we can avoid the
> warning by simply using
>
>    memcpy(dest, source, 2)
>
> since that memcpy call will be inlined these days.
>

I think it's OK if that macro becomes an alias for memcpy. On modern
systems I'd even expect it to be faster than the current code, since a
properly inlined memcpy will know when to pick word-size over byte-size
operations, since it'll know the alignment of the source and target
(which this macro doesn't).

-- brane

Re: COPY_TWO_BYTES and C strict aliasing

Posted by Branko Čibej <br...@apache.org>.
On 01.06.2012 19:24, Philip Martin wrote:
> Philip Martin <ph...@wandisco.com> writes:
>
>> Is COPY_TWO_BYTES a significant optimisation?
> r1325896 implies this is most significant for ra_svn.  So I compared the
> testsuite over ra_svn built with the "fast" code and the "slow" code.
> The CPU used by the testsuite and svnserve was:
>
>    "fast" code
>
>    749.93user 404.98system 9:09.25elapsed testsuite
>    0:32.43 svnserve
>
>    742.84user 400.00system 8:59.75elapsed testsuite
>    0:32.70 svnserve
>
>    739.51user 400.87system 8:58.91elapsed testsuite
>    0:32.66 svnserve
>
>    "slow" code
>
>    745.06user 402.36system 9:04.21elapsed testsuite
>    0:32.73 svnserve
>
>    739.94user 401.86system 8:57.13elapsed testsuite
>    0:32.58 svnserve
>
> On my Linux box this optimisation isn't significant.  Perhaps it makes a
> difference on some other platform or with some other test scenario, but
> at the moment it's not clear why we would want with this code.

I can't see how any compiler these days, except possibly one that
targets really tiny embedded platforms, would do worse with memcpy than
with the "optimized" code. I suspect someone has been a bit too eager
with optimizations without actually measuring their impact. :)

-- Brane

Re: COPY_TWO_BYTES and C strict aliasing

Posted by Philip Martin <ph...@wandisco.com>.
Philip Martin <ph...@wandisco.com> writes:

> Is COPY_TWO_BYTES a significant optimisation?

r1325896 implies this is most significant for ra_svn.  So I compared the
testsuite over ra_svn built with the "fast" code and the "slow" code.
The CPU used by the testsuite and svnserve was:

   "fast" code

   749.93user 404.98system 9:09.25elapsed testsuite
   0:32.43 svnserve

   742.84user 400.00system 8:59.75elapsed testsuite
   0:32.70 svnserve

   739.51user 400.87system 8:58.91elapsed testsuite
   0:32.66 svnserve

   "slow" code

   745.06user 402.36system 9:04.21elapsed testsuite
   0:32.73 svnserve

   739.94user 401.86system 8:57.13elapsed testsuite
   0:32.58 svnserve

On my Linux box this optimisation isn't significant.  Perhaps it makes a
difference on some other platform or with some other test scenario, but
at the moment it's not clear why we would want with this code.

-- 
uberSVN: Apache Subversion Made Easy
http://www.uberSVN.com