[LyX/master] Do not return copies of string members

Enrico Forestieri forenr at lyx.org
Fri May 12 09:53:53 UTC 2023


On Fri, May 12, 2023 at 09:22:03AM +0200, Jean-Marc Lasgouttes wrote:
> 
>Le 11/05/2023 à 23:04, Enrico Forestieri a écrit :
>>Anyway, I don't think the autoconf test is broken because:
>
>From what I read here:
>https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html
>this define is merely a default. It looks like one can choose to 
>change it, as long as all the libraries you use (e.g. Qt) do the same.

I found the attached test for COW on stackoverflow. I first tried it on 
Solaris 10 with the following results:

$ /opt/csw/bin/g++ -dumpversion
5.5.0
$ /opt/csw/bin/g++ cow.cpp -o cow
$ ./cow
std::string is NOT COW.

$ /usr/sfw/bin/g++ -dumpversion
3.4.3
$ /usr/sfw/bin/g++ cow.cpp -o cow
$ ./cow
std::string is COW.

Then I tried it on cygwin:

$ g++ cow.cpp -o cow
$ ./cow
std::string is NOT COW.

and all of this despite the fact that _GLIBCXX_USE_CXX11_ABI=0 on 
cygwin. I am very confused, but the build I obtained by manually 
commenting out STD_STRING_USES_COW in config.h (and changing nothing 
else, e.g., leaving USE_GLIBCXX_CXX11_ABI commented out) is working fine 
until now.

So, maybe the define for STD_STRING_USES_COW should be performed by 
actually directly testing for it (as in the attached) rather than 
inferring it from _GLIBCXX_USE_CXX11_ABI?

-- 
Enrico
-------------- next part --------------
// https://stackoverflow.com/questions/4496150/programmatically-determine-if-stdstring-uses-copy-on-write-cow-mechanism

#include <iostream>
#include <string>

bool stdstring_supports_cow()
{
   //make sure the string is longer than the size of potential
   //implementation of small-string.
   std::string s1 = "012345678901234567890123456789"
                    "012345678901234567890123456789"
                    "012345678901234567890123456789"
                    "012345678901234567890123456789"
                    "012345678901234567890123456789";
   std::string s2 = s1;
   std::string s3 = s2;

   bool result1 = s1.data() == s2.data();
   bool result2 = s1.data() == s3.data();

   s2[0] = 'X';

   bool result3 = s1.data() != s2.data();
   bool result4 = s1.data() == s3.data();

   s3[0] = 'X';

   bool result5 = s1.data() != s3.data();

   return result1 && result2 &&
          result3 && result4 &&
          result5;
}

int main()
{
  if (stdstring_supports_cow())
      std::cout << "std::string is COW." << std::endl;
   else
      std::cout << "std::string is NOT COW." << std::endl;
   return 0;
}


More information about the lyx-devel mailing list