简体   繁体   English

一个const字符串构造函数,不分配任何char内存?

[英]A const string constructor that doesn't allocate any char memory?

I'm trying to optimize some code I've written to handle several layers of an application protocol. 我正在尝试优化我编写的一些代码来处理应用程序协议的多个层。 I made liberal use of the std::string class, and strove for simplicity rather than premature optimization. 我自由地使用了std::string类,并且为了简单而不是过早优化而努力。 The application is too slow, and valgrind & gprof show I'm spending significant time copy-constructing strings as a buffer moves upward through my stack. 应用程序太慢了, valgrindgprof显示我正在花费大量时间复制构造字符串,因为缓冲区向上移动到我的堆栈中。

It seems to me that, after copying chars from the system buffer to my lowest application buffer, I should be able to avoid copying the data any more: after all, it is not mutated as it moves up the stack. 在我看来,在将chars从系统缓冲区复制到我的最低应用程序缓冲区之后,我应该能够避免再次复制数据了:毕竟,它在向上移动堆栈时不会发生变异。

My protocol format is a "transmission", consisting of one or more newline-terminated records , each consisting of several tab-separated fields , and terminated with a special token. 我的协议格式是一个“传输”,由一个或多个换行符终止的记录组成,每个记录由几个以制表符分隔的字段组成 ,并以特殊标记终止。 Eg 例如

RECORD 1\tHAS\tTHESE\tFIELDS\nRECORD 2\tLOOKS\tLIKE\tTHIS\nEND-OF-TRANSMISSION\n

This would be assembled in a single std::string called input_buffer. 这将被组装在一个名为input_buffer的std::string

The processing of a transmission involves extracting a record from the buffer and passing it to the next layer; 传输的处理涉及从缓冲区中提取记录并将其传递给下一层; extracting a vector of fields from the record and passing it to the next layer; 从记录中提取字段向量并将其传递给下一层; storing the fields into a map. 将字段存储到地图中。 At each stage, data is being copied as new std::strings are allocated. 在每个阶段,在分配新的std :: strings时复制数据。

Is it possible to allocate a const string from an index into input_buffer, and a length ... without any copying being done? 是否有可能将索引中的const字符串分配到input_buffer,并且长度...没有进行任何复制? For example, RECORD 2 begins at offset 26 and is 24 chars long: 例如,RECORD 2从偏移量26开始,长度为24个字符:

const std:string record (substr(input_buffer, 26), 24 );

I'm not familiar with the innards of a string object, but its performance guarantees seem to imply that somewhere there's a simple char sequence, and almost undoubtedly a pointer to those chars' memory. 我不熟悉字符串对象的内部,但它的性能保证似乎暗示某处有一个简单的char序列,而且几乎无疑是指向那些字符内存的指针。 Could that pointer be initialized to memory belonging to another string? 该指针可以初始化为属于另一个字符串的内存吗?

(My compiler is g++ 4.7, but if this is something that requires 4.8, I'd appreciate knowing about that too.) (我的编译器是g ++ 4.7,但如果这需要4.8,我也很高兴知道这一点。)

From what I understand, this sounds like a good candidate for boost::string_ref . 根据我的理解,这听起来像是boost :: string_ref的一个很好的候选者。 You would simply do boost::string_ref input(input_buffer); 你只需要做boost::string_ref input(input_buffer); and then pass string_ref s up the stack instead. 然后将string_ref s传递给堆栈。 The only thing you have to worry about is keeping the original buffer alive the whole time. 您唯一需要担心的是保持原始缓冲区始终存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM