Performance optimization
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <ReArchive/types/Header.h>
|
||||
#include <ReArchive/types/FileTable.h>
|
||||
#include <ReArchive/types/FileEntry.h>
|
||||
#include <cassert>
|
||||
|
||||
using ReArchive::Header;
|
||||
using ReArchive::FileTable;
|
||||
@@ -20,39 +21,35 @@ Header GetHeader(const unsigned char* archive) {
|
||||
/// @param in Our input stream to the file.
|
||||
/// @note Does not close the input stream.
|
||||
FileTable GetFileTable(const Header& header, std::ifstream& in) {
|
||||
FileTable result;
|
||||
|
||||
std::vector<unsigned char> buffer;
|
||||
|
||||
in.seekg(0, std::ios::end);
|
||||
int64_t file_table_size = in.tellg() - header.FileTableOffset();
|
||||
in.seekg(header.FileTableOffset(), std::ios::beg);
|
||||
buffer.resize(sizeof(int64_t));
|
||||
|
||||
in.read(reinterpret_cast<char *>(buffer.data()), (int64_t) buffer.size());
|
||||
int64_t file_table_entry_count = be64toh(*reinterpret_cast<int64_t*>(buffer.data()));
|
||||
std::vector<unsigned char> buffer(file_table_size);
|
||||
in.read(reinterpret_cast<char *>(buffer.data()), buffer.size());
|
||||
|
||||
if (file_table_entry_count) {
|
||||
// To put us at the first "string size" for each FileEntry.
|
||||
in.seekg(header.FileTableOffset() + 8, std::ios::beg);
|
||||
unsigned char* ptr = buffer.data();
|
||||
int64_t file_table_entry_count = be64toh(*reinterpret_cast<int64_t*>(ptr));
|
||||
ptr += sizeof(int64_t);
|
||||
|
||||
// for each file entry,
|
||||
for (int64_t i = 0; i < file_table_entry_count; i++) {
|
||||
in.read(reinterpret_cast<char *>(buffer.data()), (int64_t) buffer.size());
|
||||
int64_t string_size = be64toh(*reinterpret_cast<int64_t*>(buffer.data()));
|
||||
FileTable result;
|
||||
for (int64_t i = 0; i < file_table_entry_count; i++) {
|
||||
// Out of bounds.
|
||||
assert(ptr < (buffer.data() + buffer.size()));
|
||||
|
||||
buffer.resize(string_size);
|
||||
in.read(reinterpret_cast<char *>(buffer.data()), (int64_t) buffer.size());
|
||||
std::string path(buffer.begin(), buffer.end());
|
||||
int64_t string_size = be64toh(*reinterpret_cast<const int64_t*>(ptr));
|
||||
ptr += sizeof(int64_t);
|
||||
|
||||
buffer.resize(sizeof(int64_t));
|
||||
std::string path(reinterpret_cast<const char*>(ptr), string_size);
|
||||
ptr += string_size;
|
||||
|
||||
in.read(reinterpret_cast<char *>(buffer.data()), (int64_t) buffer.size());
|
||||
int64_t data_size = be64toh(*reinterpret_cast<int64_t*>(buffer.data()));
|
||||
int64_t data_size = be64toh(*reinterpret_cast<const int64_t*>(ptr));
|
||||
ptr += sizeof(int64_t);
|
||||
|
||||
in.read(reinterpret_cast<char *>(buffer.data()), (int64_t) buffer.size());
|
||||
int64_t data_offset = be64toh(*reinterpret_cast<int64_t*>(buffer.data()));
|
||||
int64_t data_offset = be64toh(*reinterpret_cast<const int64_t*>(ptr));
|
||||
ptr += sizeof(int64_t);
|
||||
|
||||
result.Append(FileEntry(data_size, data_offset, path));
|
||||
}
|
||||
result.Append(FileEntry(data_size, data_offset, path));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
@@ -23,8 +23,7 @@ std::vector<unsigned char> FileTable::Serialize(const FileTable& file_table) {
|
||||
auto files = file_table.GetEntries();
|
||||
int64_t count = files.size();
|
||||
auto network_count = htobe64(count);
|
||||
std::vector<unsigned char> result(reinterpret_cast<unsigned char*>(&network_count),
|
||||
reinterpret_cast<unsigned char*>(&network_count) + sizeof(network_count));
|
||||
std::vector<unsigned char> result(reinterpret_cast<unsigned char*>(&network_count), reinterpret_cast<unsigned char*>(&network_count) + sizeof(network_count));
|
||||
|
||||
if (files.empty())
|
||||
return result;
|
||||
|
Reference in New Issue
Block a user