Chris Lattner via llvm-dev
2016-Sep-26 00:40 UTC
[llvm-dev] Is it time to allow StringRef to be constructed from nullptr?
The pointer could only be null if the length is zero. If the length is zero, you shouldn't be loading it. Defaulting to null instead of "" is also a microoptimization. -Chris> On Sep 25, 2016, at 10:49 AM, Mehdi Amini via llvm-dev <llvm-dev at lists.llvm.org> wrote: > > >> On Sep 25, 2016, at 9:10 AM, Zachary Turner via llvm-dev <llvm-dev at lists.llvm.org> wrote: >> >> While porting LLDB over to StringRef, I am continuously running into difficulties caused by the fact that StringRef cannot be constructed from nullptr. So I wanted to see peoples' thoughts on removing this restriction from StringRef. To be clear, I'm only using LLDB as a motivating example, but I'm not requesting that it be done because LLDB is some kind of special case. If it is to be done it should be on its own merits. That said, here is some context: >> >> LLDB has a lot of functions that look like this: >> >> void foo(const char *, Bar, const char *). >> >> I'm trying to port these to functions that look like this: >> >> void foo(StringRef, Bar, StringRef). >> >> Often times the parameters are string literals or char arrays, but equally often they are another const char* that got passed into the calling function, or a return value from a CRT function like strstr(), or many other possible sources. This latter category presents a problem for porting code to StringRef, because if I simply change the function signature and fix up compile errors, I will probably have introduced a bug because hundreds of callers will now be implicitly converting from const char* to StringRef, leaving open the possibility that one of those was null. >> >> To work around this, I've started doing the following every time I port a function: >> >> void foo(const char *, Bar, const char*) = delete; >> >> This is pretty hackish, but it gets the job done. At least the compiler warns me and forces me to go inspect every callsite where there's an implicit conversion. Unfortunately it also makes for extremely verbose code. Now instead of: >> >> foo("bar", baz, "buzz") >> >> I have to write >> >> foo(StringRef("bar"), baz, StringRef("buzz")) >> >> even for string literals and char arrays, which will obviously never be null! If StringRef would handle a null argument gracefully, it would make my life much easier. >> >> >> With that out of the way, here are some reasons I can see to allow StringRef accept null to its constructor which are independent of LLDB and stand on their own merit. >> >> 1) std::string_view<> can be constructed with null. I don't know when we will be able to use std::string_view<>, but there's a chance that at some point in the future we may wish to remove StringRef in favor of string_view. That day isn't soon, but in any case, it will be easier if our assumptions are the same. >> >> 2) [nullptr, nullptr+0) is a valid range. Why shouldn't we be able to construct a StringRef from an otherwise perfectly valid range? >> >> 3) StringRef() can already be constructed from nullptr (!) Surprised? That's what happens when you invoke the default constructor. It happily initializes the internal Data with null. So why not allow the same behavior when invoking the const char * constructor? >> >> >> Thoughts? > > > As a tangent: I don’t like the fact that StringRef is implicitly built out of “const char *”, this is calling strlen() and because it is implicit folks don’t realize when they go from string -> char * -> StringRef. > I rather have this constructor explicit, and provide an implicit one for string literal. > > To come back to your point, I’m not sure if we should leave the internal pointer null or always set it to “”? This would provide the guarantee that dereferencing a StringRef is always valid without checking. > > — > Mehdi > > _______________________________________________ > LLVM Developers mailing list > llvm-dev at lists.llvm.org > http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160925/57747cc1/attachment.html>
Mehdi Amini via llvm-dev
2016-Sep-27 16:41 UTC
[llvm-dev] Is it time to allow StringRef to be constructed from nullptr?
> On Sep 25, 2016, at 5:40 PM, Chris Lattner <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.> Defaulting to null instead of "" is also a microoptimization.You mean: because initializing to 0 is faster than a constant? — Mehdi> > > On Sep 25, 2016, at 10:49 AM, Mehdi Amini via llvm-dev <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: > >> >>> On Sep 25, 2016, at 9:10 AM, Zachary Turner via llvm-dev <llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org>> wrote: >>> >>> While porting LLDB over to StringRef, I am continuously running into difficulties caused by the fact that StringRef cannot be constructed from nullptr. So I wanted to see peoples' thoughts on removing this restriction from StringRef. To be clear, I'm only using LLDB as a motivating example, but I'm not requesting that it be done because LLDB is some kind of special case. If it is to be done it should be on its own merits. That said, here is some context: >>> >>> LLDB has a lot of functions that look like this: >>> >>> void foo(const char *, Bar, const char *). >>> >>> I'm trying to port these to functions that look like this: >>> >>> void foo(StringRef, Bar, StringRef). >>> >>> Often times the parameters are string literals or char arrays, but equally often they are another const char* that got passed into the calling function, or a return value from a CRT function like strstr(), or many other possible sources. This latter category presents a problem for porting code to StringRef, because if I simply change the function signature and fix up compile errors, I will probably have introduced a bug because hundreds of callers will now be implicitly converting from const char* to StringRef, leaving open the possibility that one of those was null. >>> >>> To work around this, I've started doing the following every time I port a function: >>> >>> void foo(const char *, Bar, const char*) = delete; >>> >>> This is pretty hackish, but it gets the job done. At least the compiler warns me and forces me to go inspect every callsite where there's an implicit conversion. Unfortunately it also makes for extremely verbose code. Now instead of: >>> >>> foo("bar", baz, "buzz") >>> >>> I have to write >>> >>> foo(StringRef("bar"), baz, StringRef("buzz")) >>> >>> even for string literals and char arrays, which will obviously never be null! If StringRef would handle a null argument gracefully, it would make my life much easier. >>> >>> >>> With that out of the way, here are some reasons I can see to allow StringRef accept null to its constructor which are independent of LLDB and stand on their own merit. >>> >>> 1) std::string_view<> can be constructed with null. I don't know when we will be able to use std::string_view<>, but there's a chance that at some point in the future we may wish to remove StringRef in favor of string_view. That day isn't soon, but in any case, it will be easier if our assumptions are the same. >>> >>> 2) [nullptr, nullptr+0) is a valid range. Why shouldn't we be able to construct a StringRef from an otherwise perfectly valid range? >>> >>> 3) StringRef() can already be constructed from nullptr (!) Surprised? That's what happens when you invoke the default constructor. It happily initializes the internal Data with null. So why not allow the same behavior when invoking the const char * constructor? >>> >>> >>> Thoughts? >> >> >> As a tangent: I don’t like the fact that StringRef is implicitly built out of “const char *”, this is calling strlen() and because it is implicit folks don’t realize when they go from string -> char * -> StringRef. >> I rather have this constructor explicit, and provide an implicit one for string literal. >> >> To come back to your point, I’m not sure if we should leave the internal pointer null or always set it to “”? This would provide the guarantee that dereferencing a StringRef is always valid without checking. >> >> — >> Mehdi >> >> _______________________________________________ >> LLVM Developers mailing list >> llvm-dev at lists.llvm.org <mailto:llvm-dev at lists.llvm.org> >> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev <http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev>-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20160927/80991413/attachment.html>
Chris Lattner via llvm-dev
2016-Sep-28 05:05 UTC
[llvm-dev] Is it time to allow StringRef to be constructed from nullptr?
> 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>