[PATCH 2/2] Avoid copying in lookup_name_info

Pedro Alves palves@redhat.com
Tue Mar 31 19:23:52 GMT 2020


On 3/31/20 8:11 PM, Tom Tromey wrote:
>>>>>> "Pedro" == Pedro Alves <palves@redhat.com> writes:
> 
> Pedro> I think it would be better for this overload to take a
> Pedro> gdb::string_view instead of a const std::string& .  That
> Pedro> way, it still works if you only have a string_view handy.
> Pedro> As is, there's no way to created a lookup_name_info starting
> Pedro> with a string_view without copying.  WDYT?
> 
> The issue is that there's a trick in the implementation:
> 
>   /* Like the "name" method but guarantees that the returned string is
>      \0-terminated.  */
>   const char *c_str () const
>   {
>     /* Actually this is always guaranteed due to how the class is
>        constructed.  */
>     return m_name.data ();
>   }
> 
> This is needed in some spots.
> 
> However, if we take a string_view at construction, then we'd be
> violating the string_view contract, or we'd need to make a copy -- but
> the former is bad, and the latter is the goal the patch.

I see.  I agree with leaving it as is then.

Thanks,
Pedro Alves



More information about the Gdb-patches mailing list