0

If I pass memcmp the equal pointers for its first and second argument, I suspected that it might just return 0 without checking the elements -- since if the same pointer is passed the elements must be zero. It seems like to me it is good optimization to check for pointer equality and exit early.

I checked the glibc and Free BSD LibC implementations and neither seem to be making this optimization.

So I checked the standard (below): The Open Standards draft version of C99 says nothing one way or the other:

7.21.4.1 The memcmp function Synopsis

#include <string.h>
int memcmp(const void *s1, const void *s2, size_t n);

Description

The memcmp function compares the first n characters of the object pointed to by s1 to the first n characters of the object pointed to by s2.

Returns

The memcmp function returns an inte ger greater than, equal to, or less than zero, accordingly as the object pointed to by s1 is greater than, equal to, or less than the object pointed to by s2

As far as I can tell it doesn't forbid such an "trick", since one would still get the same returned values. It is really is an implementation detail, AFAICT.

Clearly the people writing these libraries have put much more thought into this than I have, so there is probably a good reason for not doing this. What is it?

Frames Catherine White
  • 27,368
  • 21
  • 87
  • 137
  • 5
    Just a guess: nobody expects this function to be called in such a "stupid" way very often, so they don't bother to optimize this case? The compiler could still eliminate the code if it can be statically proved the pointers are the same... –  May 02 '18 at 06:32
  • 2
    Why should anyone spend an instruction checking the pointers for equality which will not be true in any real world use case? The performance gain in a pure theoretical situation does not justify spending a single instruction for all other cases. – Gerhardh May 02 '18 at 06:33
  • Also, leaving such things to the compiler avoids a compare instruction that would be pointless for any "normal" call. –  May 02 '18 at 06:34
  • @Gerhardh it is hardly not true in **any** real world use-case. Consider if one is using some form of [interning](https://en.wikipedia.org/wiki/String_interning), if so then one will regularly get circumstances where you have two pointer's that are equal but that the compiler can't prove are equal. And if the author of the code doing the `memcmp` isn't the author of the code which does the interning. (say the `memcmp` is buried some 3rd party library function) then it is all but unavoidable. – Frames Catherine White May 02 '18 at 06:46

1 Answers1

1

I suspect it's due to practical utility concerns.

In practice, few people supply same pointer to the 1st and 2nd arguments to memcmp(), so in most cases the extra condition test does nothing effectively and is thus a waste of effort.

As Felix Palmen pointed out in comments, if two pointers can be statically confirmed to point to the same address, the compiler can do the optimization instead.

iBug
  • 35,554
  • 7
  • 89
  • 134