I am trying to optimize the code that I wrote to handle several layers of the application protocol. I made a liberal use of the class std::stringand strived for simplicity, not for premature optimization. The application is too slow, and valgrind and gprof show that I spend significant lines to build a copy when the buffer moves up through my stack.
It seems to me that after copying the characters from the system buffer to my lowest application buffer, I should be able to avoid copying the data anymore: in the end, it does not mutate when it moves up the stack.
My protocol format is a "transfer" consisting of one or more entries , each of which consists of several tab-delimited fieldsand ends with a special marker. For example.
RECORD 1\tHAS\tTHESE\tFIELDS\nRECORD 2\tLOOKS\tLIKE\tTHIS\nEND-OF-TRANSMISSION\n
This will be assembled into one std::stringcalled input_buffer.
Transmission processing includes retrieving an entry from the buffer and transferring it to the next level; extracting the vector of fields from the record and transferring it to the next level; keeping fields in the map. At each stage, the data is copied when new std :: lines are assigned.
Is it possible to extract the string const from the index in input_buffer and the length ... without any copying? For example, RECORD 2 starts at offset 26 and lasts 24 characters:
const std:string record (substr(input_buffer, 26), 24 );
, , , , - char . , ?
( - g++ 4.7, , 4.8, .)