[llvm-dev] Is it time to allow StringRef to be constructed from nullptr?

Chris Lattner via llvm-dev llvm-dev at lists.llvm.org
Tue Sep 27 22:05:44 PDT 2016


> On Sep 27, 2016, at 9:41 AM, Mehdi Amini <mehdi.amini at apple.com> wrote:
> 
> 
>> On Sep 25, 2016, at 5:40 PM, Chris Lattner <clattner at apple.com <mailto:clattner at apple.com>> wrote:
>> 
>> The pointer could only be null if the length is zero.  If the length is zero, you shouldn't be loading it.
> 
> I was thinking about functions that takes a non-null pointer. For example, assuming you have:
> 
> extern void *my_memcpy (void *dest, const void *src, size_t len) __attribute__((nonnull (1, 2)));
> 
> You can just be calling it with any StringRef without a null pointer check.
> 
> That said it is just a thought I had, I don’t have any data or actual example to back this up.

Yeah, I’m not sure where this comes up, and whether memcpy should take null when the length is zero is also a highly contentious thing that flares up in debates periodically.

> 
> 
>>  Defaulting to null instead of "" is also a microoptimization.
> 
> You mean: because initializing to 0 is faster than a constant?

Yes, 0 is a “load immediate”, initializing to “” requires materializing the address of a global.

-Chris 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160927/e6670788/attachment.html>


More information about the llvm-dev mailing list