This is the mail archive of the mailing list for the GDB project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFC 2/7] Add libiberty/concat styled concat_path function

On 01/12/2017 11:32 AM, Philipp Rudo wrote:
>  static inline int
> -startswith (const char *string, const char *pattern)
> +startswith (std::string str, std::string pattern)


This is passing by copy, so will force unnecessary deep
string copies all over the place.

>  {
> -  return strncmp (string, pattern, strlen (pattern)) == 0;
> +  return (str.find (pattern) == 0);
> +}
> +

I.e., before, this caused 0 copies:

  startswith ("foo, "f");

After, you force a deep string copy for "foo", and another
for "f".  It's as if you wrote:

  startswith (xstrdup ("foo), xstrdup ("f"));

Also, this function is a static inline in a header so that
the compiler can see when "pattern" is a string literal, and
thus strlen can be optimized to a plain 'sizeof (pattern) - 1',
which is very frequent.

If you want to add overloads that can take "const std::string &"
for convenience, to avoid str.c_str(), that's maybe fine,
but you'd have to add all the combinations of
'const char *' x 'const std::string &' in the parameters, I

Pedro Alves

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]