小编Jam*_*ock的帖子

std :: string没有空闲的内存分配

我有一个非常相似的问题

如何使用glibc的字符串实现在堆栈上分配std :: string?

但我认为值得重新询问.

我想要一个std::string溢出到免费商店的本地存储.std::basic_string提供一个分配器作为模板参数,所以看起来要做的就是用本地存储编写一个分配器并用它来参数化basic_string,如下所示:

std::basic_string<
char, 
std::char_traits<char>, 
inline_allocator<char, 10> 
> 
x("test");
Run Code Online (Sandbox Code Playgroud)

我试着编写一个inline_allocator可以按照你期望的方式工作的类:它为存储保留10个字节,如果basic_string需要超过10个字节,则调用::operator new().我无法让它发挥作用.在执行上面的代码行的过程中,我的GCC 4.5标准字符串库调用了复制构造函数inline_allocator4次.我不清楚是否有一种合理的方法来编写复制构造函数inline_allocator.

在另一个StackOverflow线程中,Eric Melski将此链接提供给Chromium中的一个类:

http://src.chromium.org/svn/trunk/src/base/stack_container.h

这很有趣,但它不是替代品std::string,因为它包装std::basic_string在一个容器中,所以你必须调用一个重载operator->()来获取std::basic_string.

我找不到任何其他解决方案.难道没有好的解决方案吗?如果这是真的,那么这些std::basic_stringstd::allocator概念是否存在严重缺陷?我的意思是,似乎这应该是一个非常基本和简单的用例std::basic_stringstd::allocator.我想这个std::allocator概念主要是为游泳池设计的,但我认为它也应该涵盖这个.

看起来像C++ 0x中的rvalue-reference移动语义可能使写入成为可能inline_allocator,如果重写了字符串库,那么basic_string使用其分配器的移动构造函数而不是复制构造函数.有谁知道这个结果的前景如何?

我的应用程序需要每秒构建一百万个微小的ASCII字符串,所以我最终编写了自己的固定长度字符串类Boost.Array,它工作正常,但这仍然困扰着我.

c++ memory-management stdstring c++11

33
推荐指数
3
解决办法
5606
查看次数

测量NUMA(非统一内存访问).没有可观察到的不对称性.为什么?

我试图测量NUMA的非对称内存访问效果,但都失败了.

本实验

采用Intel Xeon X5570 @ 2.93GHz,2个CPU,8个内核.

在固定到核心0的线程上,我使用numa_alloc_local在核心0的NUMA节点上分配大小为10,000,000字节的数组x.然后我迭代数组x 50次并读取和写入数组中的每个字节.测量进行50次迭代所用的时间.

然后,在我的服务器中的每个其他核心上,我固定一个新线程并再次测量经过50次迭代读取和写入数组x中每个字节的时间.

数组x很大,可以最大限度地减少缓存效 我们想要测量CPU必须一直到RAM加载和存储的速度,而不是在缓存有帮助的时候.

我的服务器中有两个NUMA节点,因此我希望在分配了数组x的同一节点上具有亲缘关系的核心具有更快的读/写速度.我没有看到.

为什么?

也许NUMA仅与8-12核心的系统相关,正如我在别处看到的那样?

http://lse.sourceforge.net/numa/faq/

numatest.cpp

#include <numa.h>
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <pthread.h>

void pin_to_core(size_t core)
{
    cpu_set_t cpuset;
    CPU_ZERO(&cpuset);
    CPU_SET(core, &cpuset);
    pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &cpuset);
}

std::ostream& operator<<(std::ostream& os, const bitmask& bm)
{
    for(size_t i=0;i<bm.size;++i)
    {
        os << numa_bitmask_isbitset(&bm, i);
    }
    return os;
}

void* thread1(void** x, size_t core, size_t N, size_t M)
{
    pin_to_core(core);

    void* y …
Run Code Online (Sandbox Code Playgroud)

c++ linux performance linux-kernel numa

33
推荐指数
5
解决办法
9114
查看次数

在io_service :: stop()之后,在io_service :: ~io_service()中调用boost :: asio - asio_handler_deallocate

我有一个ip::udp::socket带有一个io_service.只有一个boost::thread调用io_service::run()方法,以及一个io_service::work防止io_service::run()返回的实例.我的完成处理程序ip::udp::socket有自定义asio_handler_allocate()asio_handler_deallocate()功能,由一个支持my::custom_memory_pool.

当我的应用程序退出时,这个事件序列发生在我的关闭线程上:

  1. ip::udp::socket::close()
  2. work::~work()
  3. io_service::stop()
  4. thread::join()
  5. my::custom_memory_pool::~custom_memory_pool()
  6. ip::udp::socket::~socket()
  7. thread::~thread()
  8. io_service::~io_service()

在步骤8中,调用io_service::~io_service()原因...

Program terminated with signal 11, Segmentation fault.
#0  0x00000000005ad93c in my::custom_memory_pool<boost::aligned_storage<512u, -1u> >::deallocate (this=0x36323f8, t=0x7fca97a07880)
    at memory.hpp:82
82                      reinterpret_cast<pool_node*>(t)->next_ = head_;
(gdb) bt 30
#0  0x00000000005ad93c in my::custom_memory_pool<boost::aligned_storage<512u, -1u> >::deallocate (this=0x36323f8, t=0x7fca97a07880)
    at memory.hpp:82
#1  0x00000000005ad40a in asio_handler_deallocate (p=0x7fca97a07880, s=96, h=0x7fffe09d5480) at net.cpp:22
#2  0x0000000000571a07 in boost_asio_handler_alloc_helpers::deallocate<socket_multicast::completion_handler> …
Run Code Online (Sandbox Code Playgroud)

c++ boost boost-asio

6
推荐指数
1
解决办法
2378
查看次数

是UTF8内射映射?

我们编写一个C++应用程序,需要知道这个:

UTF8文本是否编码从字节到字符的内射映射,这意味着每个字符(字母...)只以一种方式编码?因此,例如字母'Ž'不能编码为3231和32119.

c c++ unicode utf-8 unicode-normalization

6
推荐指数
2
解决办法
427
查看次数

使用Parsec搜索模式

不确定这是否可行(或推荐),但我实际上是在尝试使用Parsec在文件中搜索一系列字符.示例文件:

START (name)

junk
morejunk=junk;
dontcare
    foo ()
    bar

care_about this (stuff in here i dont care about);

don't care about this
or this
foo = bar;

also_care
about_this
(dont care whats in here);
and_this too(only the names
   at the front
   do i care about
);

foobar
may hit something = perhaps maybe (like this);
foobar

END
Run Code Online (Sandbox Code Playgroud)

这是我尝试让它运作:

careAbout :: Parser (String, String)
careAbout = do
    name1 <- many1 (noneOf " \n\r")
    skipMany space
    name2 <- many1 (noneOf " (\r\n") …
Run Code Online (Sandbox Code Playgroud)

parsing haskell parsec

6
推荐指数
1
解决办法
206
查看次数

std :: set <std :: pair <size_t,std :: string >>> :: find(),没有字符串复制结构

我有一个std :: std ::对,第二对是一个字符串.我想检查一下是否存在一对.

std::set< std::pair<size_t, std::string> > set_;

bool exists(size_t x, const std::string& s)
{
    std::set< std::pair<size_t, std::string> >::iterator i = set_.find(std::make_pair(x, s)); // copy of s is constructed by make_pair!
    return i != set_.end();
}

我经常调用这个函数(是的,非常经常),所以我想在不制作字符串的临时副本的情况下执行此检查.有没有办法做到这一点,就像我在这里一样简单和简洁,但是没有制作字符​​串的临时副本?使用STL或Boost容器的任何解决方案都会很好.

c++ stl

3
推荐指数
1
解决办法
2306
查看次数

Phabricator审查所有提交

我为一个新项目创建了一个新的git存储库,并添加了几个提交.我想请我的同事检查Phabricator中的整个项目.

是否有一个Arcanist命令,我可以发出这个命令会导致整个项目,所有提交都出现在Phabricator中进行审查?

我想要做的基本上是这样的:

arc diff before-first-commit
Run Code Online (Sandbox Code Playgroud)

第一个提交有id'aabbcc'.这不起作用:

arc diff aabbcc~
Usage Exception: Unable to find any git commit named 'aabbcc~' in this repository.
Run Code Online (Sandbox Code Playgroud)

相关问题

在git中,是否存在类似于空分支的伪分支名称"第一次提交之前的提交"?

是否有一个git diff命令可以输出项目中的所有文件?就像是:

git diff before-first-commit HEAD
Run Code Online (Sandbox Code Playgroud)

git phabricator arcanist

3
推荐指数
1
解决办法
1643
查看次数

为什么在输入`::时出现解析错误

我想猜一个数字游戏。

`

main :: IO()

   checkGuess :: (Integral a) => a -> Bool
   checkGuess b = if b == 9 then return True 

main = do
   print "Guess the number?"
   guess <- getLine
   checkGuess guess
Run Code Online (Sandbox Code Playgroud)

但是我在功能checkGuess的输入'::'上得到解析错误

haskell

1
推荐指数
1
解决办法
52
查看次数