I have the following class, it contains a data structure called Index, which is expensive to compute. So I am caching the index to disk and reading it in again. The index element id of template type T can be used with a variety of primitive datatypes.
But I would also like to use id with the type std::string. I wrote the serialize/deserilize code for the general case and also tested if it works with normal C++ strings and they work, if they are short enough. Small string optimization seems to kick in.
I also wrote a different implementation just for handling longer strings safely. But the safe code is about 10x slower and I would really like to just read in the strings with fread (500ms readin are very painful, while 50ms are perfectly fine).
How can I reliably use my libcpp small string optimization, if I know that all identifiers are shorter than the longest possible short string? How can I reliably tell how long the 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 perfectly fine
template class Reader<int32_t>;
// works perfectly fine for strings shorter than 22 bytes
template class Reader<std::string>;