Reliably using C++ Small String Optimization to fread short std::strings from Files into Memory -
i have following class, contains data structure called index, expensive compute. caching index disk , reading in again. index element id of template type t can used variety of primitive datatypes.
but use id type std::string. wrote serialize/deserilize code general case , tested if works normal c++ strings , work, if short enough. small string optimization seems kick in.
i wrote different implementation handling longer strings safely. safe code 10x slower , read in strings fread (500ms readin painful, while 50ms fine).
how can reliably use libcpp small string optimization, if know identifiers shorter longest possible short string? how can reliably tell how long longest possible small string is?
template<typename t> class reader { public: struct index { t id; size_t length; // ... values etc }; index* index; size_t indextablesize; void serialize(const char* filename) { file *file = fopen(filename, "w+b"); if (file == null) return; fwrite(&indextablesize, sizeof(size_t), 1, file); fwrite(index, sizeof(index), indextablesize, file); fclose(file); } void deserialize(const char* filename) { file *file = fopen(filename, "rb"); if (file == null) return; fread(&indextablesize, sizeof(size_t), 1, file); index = new index[indextablesize]; fread(index, sizeof(index), indextablesize, file); fclose(file); } }; // works fine template class reader<int32_t>; // works fine strings shorter 22 bytes template class reader<std::string>;
std::string
not trivially copyable. , performing memcpy
on type (which equivalent of fwrite
ing , fread
ing back) in c++ legal if trivially copyable. therefore, want not possible directly.
if want serialize string, must manually. must number of characters , write it, write characters themselves. read in, have read size of string, read many characters.
Comments
Post a Comment