为什么COW std :: string optimization仍然在GCC 5.1中启用? [英] Why is COW std::string optimization still enabled in GCC 5.1?

查看:448
本文介绍了为什么COW std :: string optimization仍然在GCC 5.1中启用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据GCC 5版本更改页面( https://gcc.gnu.org/ gcc-5 / changes.html ):

According to GCC 5 release changes page (https://gcc.gnu.org/gcc-5/changes.html):


默认情况下启用std :: string的新实现,小字符串优化而不是写时复制引用计数

A new implementation of std::string is enabled by default, using the small string optimization instead of copy-on-write reference counting

我决定检查它并写了一个简单的程序:

I decided to check it and wrote a simple program:

int main()
{
    std::string x{"blah"};
    std::string y = x;
    printf("0x%X\n", x.c_str());
    printf("0x%X\n", y.c_str());
    x[0] = 'c';
    printf("0x%X\n", x.c_str());
    printf("0x%X\n", y.c_str());
}

结果是:

0x162FC38
0x162FC38
0x162FC68
0x162FC38

请注意,x.c_str()指针在x [0] ='c'后改变。这意味着在写入时复制内部缓冲区。所以看来COW仍在工作。为什么?

Notice that the x.c_str() pointer changes after x[0] = 'c'. This means that the internal buffer is copied upon write. So it seems that COW is still in work. Why?

我在Ubuntu上使用g ++ 5.1.0。

I use g++ 5.1.0 on Ubuntu.

推荐答案

一些分布有意偏离FSF GCC选择,默认为新的ABI。 这里有一个解释为什么Fedora 22偏离上游GCC像这样。 short:

Some distributions intentionally deviate from the FSF GCC choice to default to the new ABI. Here's an explanation of why Fedora 22 deviates from upstream GCC like that. In short:

在程序中,最好不要混合旧的和新的ABI,而是选择一个并坚持它。

In a program, it's best not to mix the old and the new ABIs, but to pick one and stick with it. Things break if one part of the program assumes a different internal representation for a type than another part of the program.

因此,如果使用任何C ++库,它使用的是旧的C ++ ABI,那么使用该库的程序也应该使用旧的C ++ ABI。

Therefore, if any C++ library is used that uses the old C++ ABI, then the programs using that library should also use the old C++ ABI.

因此,如果使用任何使用GCC 4.9或更早版本构建的C ++库,使用该库的程序也应该使用旧的C ++ ABI。

Therefore, if any C++ library is used that was built with GCC 4.9 or earlier, then the programs using that library should also use the old C++ ABI.

Fedora 22仍然提供了许多用GCC 4.9构建的库,在Fedora 22发布之前,有足够的时间重新使用GCC 5.1。要允许程序使用这些库,GCC默认切换到旧的ABI。

Fedora 22 still provides (or provided?) a lot of libraries built with GCC 4.9, because there wasn't enough time to rebuild them all with GCC 5.1 before the Fedora 22 release. To allow programs to use those libraries, the GCC default was switched to the old ABI.

据我所知,GCC 5不是Ubuntu中的默认编译器但是(但很快会),所以如果它作为额外的安装提供,那些来自Fedora的相同参数也适用于Ubuntu。

As far as I can tell, GCC 5 isn't the default compiler in Ubuntu yet (but will soon be), so if it's provided as an extra install, those same arguments from Fedora also apply to Ubuntu.

这篇关于为什么COW std :: string optimization仍然在GCC 5.1中启用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆