Skip to content

Commit

Permalink
Switch to memory mapping in serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
vk2gpu committed Jan 28, 2018
1 parent 701f6d9 commit 69cbc98
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/serialization/private/serializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ namespace Serialization
virtual Core::String GetObjectKey(i32 idx) = 0;
virtual bool IsReading() const = 0;
virtual bool IsWriting() const = 0;
virtual bool IsValid() const = 0;
};

struct SerializerImplWriteJson : SerializerImpl
Expand Down Expand Up @@ -427,6 +428,7 @@ namespace Serialization

bool IsReading() const override { return false; }
bool IsWriting() const override { return true; }
bool IsValid() const override { return objectStack_.size() > 0; }
};

struct SerializerImplReadJson : SerializerImpl
Expand All @@ -440,12 +442,14 @@ namespace Serialization
: inFile_(inFile)
{
Json::Reader reader;
Core::Vector<char> inBuffer;
inBuffer.resize((i32)inFile.Size());
inFile.Read(inBuffer.data(), inBuffer.size());
reader.parse(inBuffer.data(), inBuffer.data() + inBuffer.size(), rootValue_, false);

objectStack_.push_back(&rootValue_);
i64 offset = inFile.Tell();
i64 size = inFile.Size() - offset;
if(auto mapped = Core::MappedFile(inFile, offset, size))
{
const char* data = (const char*)mapped.GetAddress();
reader.parse(data, data + size, rootValue_, false);
objectStack_.push_back(&rootValue_);
}
}

~SerializerImplReadJson() { DBG_ASSERT(objectStack_.size() == 1); }
Expand Down Expand Up @@ -588,6 +592,7 @@ namespace Serialization

bool IsReading() const override { return true; }
bool IsWriting() const override { return false; }
bool IsValid() const override { return objectStack_.size() > 0; }
};

Serializer::Serializer(Core::File& file, Flags flags)
Expand All @@ -600,6 +605,12 @@ namespace Serialization
if(Core::ContainsAnyFlags(file.GetFlags(), Core::FileFlags::READ))
impl_ = new SerializerImplReadJson(file);
}

if(!impl_->IsValid())
{
delete impl_;
impl_ = nullptr;
}
}

Serializer::~Serializer() { delete impl_; }
Expand Down
2 changes: 2 additions & 0 deletions src/serialization/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ namespace Serialization
bool IsReading() const;
bool IsWriting() const;

explicit operator bool() const { return impl_ != nullptr; }

private:
i32 BeginObject(const char* key, bool isArray = false);
void EndObject();
Expand Down

0 comments on commit 69cbc98

Please sign in to comment.