Boost serialization and vector of lots of objects

I am using boost serialization to serialize/deserialize 3d model meshes, like this:

struct PackageMesh
{
    std::vector<Vec3> mVertexData;
    std::vector<Vec3> mNormalData;
    std::vector<Vec2> mTexCoordsData;
    std::vector<uint32_t> mIndiceData;
    uint16_t mMaterialIndex;
    bool mHasMaterial;


    PackageMesh();
};

Now each vector can contain several thousand Vec3's, and there can be alot of packagemeshes to derserialize.

I am concerned that since it is a vector of custom objects, I believe I will spend alot of time in the constructor of Vec3/Vec2 which might hurt performance - is that true? Would it be faster to store it all as floats instead?

EDIT: So loading a decently complicated model and its textures etc takes like 5-10 seconds or so, so I did some profiling...

http://i43.tinypic.com/xqansy.png

You can see the first 5-10 seconds is typically where the deserialization occurs

I believe the killer is actually deserializing the texture struct

struct PackageTexture
{
    std::string mName;
    std::vector<uint8_t> mTextureData;
    uint32_t mTextureWidth;         // width/height in pixels
    uint32_t mTextureHeight;
    ITexture::TextureFormat mTextureFormat;
    ITexture::TextureType mTextureType;


    PackageTexture();
};

Each texture can be around 200k bytes or more, so thats alot of entries.

What can I do to optimize this?


If at all possible (platform bit ordering not being an issue, for example), you would want to serialize each vector as a single block, and not one VecN at a time. I would not abandon your good use of structure for raw floats. If your VecN classes are POD types (most likely) bitwise serialization in blocks is possible and recommended.

Something along these lines (apologies if it's not idiomatic - there may be a simpler std algorithm you can use):

struct Vec3
{
    float x;
    float y;
    float z;
};

vector<Vec3> mesh;
Vec3 v0 = {1,2,3};
Vec3 v1 = {4,5,6};
Vec3 v2 = {7,8,9};
mesh.reserve(3);
mesh.push_back(v0);
mesh.push_back(v1);
mesh.push_back(v2);
std::stringstream ss;
auto ptr = mesh.data();
auto count = mesh.size();
ss << count;
ss.write((char*)ptr, count * sizeof(Vec3));
ss.seekg(0);
mesh.clear();
ss >> count;
mesh.resize(count);
ptr = mesh.data();
ss.read((char*)ptr, count * sizeof(Vec3));

Chances are strong that the cost of actual I/O will so drastically exceed the cost of extra constructors that you'll never notice. The only way to know for sure is to try it out in your application and profile it.

链接地址: http://www.djcxy.com/p/45664.html

上一篇: 如何Boost ::序列化矢量散列表?

下一篇: 增强许多对象的序列化和矢量