Optimize FileNode size by moving GenerateLocation to unique_ptr.

GitOrigin-RevId: b98aedd415ff779a62becbba86c4c967d3aba4f7
This commit is contained in:
levlam 2018-02-04 20:27:05 +03:00
parent a0fdce5f85
commit 63ec560e82
4 changed files with 39 additions and 34 deletions

View File

@ -184,8 +184,8 @@ class FileDb : public FileDbInterface {
local_key = as_key(file_data.local_.full());
}
string generate_key;
if (file_data.generate_.type() == GenerateFileLocation::Type::Full) {
generate_key = as_key(file_data.generate_.full());
if (file_data.generate_ != nullptr) {
generate_key = as_key(*file_data.generate_);
}
send_closure(file_db_actor_, &FileDbActor::clear_file_data, id, remote_key, local_key, generate_key);
}
@ -199,8 +199,8 @@ class FileDb : public FileDbInterface {
local_key = as_key(file_data.local_.full());
}
string generate_key;
if (file_data.generate_.type() == GenerateFileLocation::Type::Full && new_generate) {
generate_key = as_key(file_data.generate_.full());
if (file_data.generate_ != nullptr && new_generate) {
generate_key = as_key(*file_data.generate_);
}
LOG(DEBUG) << "SAVE " << id << " -> " << file_data << " "
<< tag("remote", format::as_hex_dump<4>(Slice(remote_key)))

View File

@ -1085,7 +1085,7 @@ class FileData {
uint64 pmc_id_ = 0;
RemoteFileLocation remote_;
LocalFileLocation local_;
GenerateFileLocation generate_;
unique_ptr<FullGenerateFileLocation> generate_;
int64 size_ = 0;
int64 expected_size_ = 0;
string name_;
@ -1108,7 +1108,8 @@ class FileData {
store(pmc_id_, storer);
store(remote_, storer);
store(local_, storer);
store(generate_, storer);
auto generate = generate_ == nullptr ? GenerateFileLocation() : GenerateFileLocation(*generate_);
store(generate, storer);
if (has_expected_size) {
store(expected_size_, storer);
} else {
@ -1134,7 +1135,13 @@ class FileData {
parse(pmc_id_, parser);
parse(remote_, parser);
parse(local_, parser);
parse(generate_, parser);
GenerateFileLocation generate;
parse(generate, parser);
if (generate.type() == GenerateFileLocation::Type::Full) {
generate_ = std::make_unique<FullGenerateFileLocation>(generate.full());
} else {
generate_ = nullptr;
}
if (has_expected_size) {
parse(expected_size_, parser);
} else {
@ -1154,8 +1161,8 @@ inline StringBuilder &operator<<(StringBuilder &sb, const FileData &file_data) {
if (file_data.local_.type() == LocalFileLocation::Type::Full) {
sb << " local " << file_data.local_.full();
}
if (file_data.generate_.type() == GenerateFileLocation::Type::Full) {
sb << " generate " << file_data.generate_.full();
if (file_data.generate_ != nullptr) {
sb << " generate " << *file_data.generate_;
}
if (file_data.remote_.type() == RemoteFileLocation::Type::Full) {
sb << " remote " << file_data.remote_.full();

View File

@ -89,9 +89,10 @@ void FileNode::set_remote_location(const RemoteFileLocation &remote, FileLocatio
on_changed();
}
void FileNode::set_generate_location(const GenerateFileLocation &generate) {
if (generate_ != generate) {
generate_ = generate;
void FileNode::set_generate_location(unique_ptr<FullGenerateFileLocation> &&generate) {
bool is_changed = generate_ == nullptr ? generate != nullptr : generate == nullptr || *generate_ != *generate;
if (is_changed) {
generate_ = std::move(generate);
on_pmc_changed();
}
}
@ -199,9 +200,9 @@ bool FileNode::need_pmc_flush() const {
return true;
}
bool has_generate_location = generate_.type() == GenerateFileLocation::Type::Full;
bool has_generate_location = generate_ != nullptr;
// Do not save "#file_id#" conversion.
if (has_generate_location && begins_with(generate_.full().conversion_, "#file_id#")) {
if (has_generate_location && begins_with(generate_->conversion_, "#file_id#")) {
has_generate_location = false;
}
@ -242,11 +243,11 @@ const FullRemoteFileLocation &FileView::remote_location() const {
return node_->remote_.full();
}
bool FileView::has_generate_location() const {
return node_->generate_.type() == GenerateFileLocation::Type::Full;
return node_->generate_ != nullptr;
}
const FullGenerateFileLocation &FileView::generate_location() const {
CHECK(has_generate_location());
return node_->generate_.full();
return *node_->generate_;
}
int64 FileView::size() const {
@ -639,7 +640,7 @@ Result<FileId> FileManager::register_generate(FileType file_type, FileLocationSo
string original_path, string conversion, DialogId owner_dialog_id,
int64 expected_size) {
FileData data;
data.generate_ = GenerateFileLocation(FullGenerateFileLocation(file_type, original_path, std::move(conversion)));
data.generate_ = make_unique<FullGenerateFileLocation>(file_type, std::move(original_path), std::move(conversion));
data.owner_dialog_id_ = owner_dialog_id;
data.expected_size_ = expected_size;
return register_file(std::move(data), file_location_source, "register_generate", false);
@ -648,7 +649,7 @@ Result<FileId> FileManager::register_generate(FileType file_type, FileLocationSo
Result<FileId> FileManager::register_file(FileData data, FileLocationSource file_location_source, const char *source,
bool force) {
bool has_remote = data.remote_.type() == RemoteFileLocation::Type::Full;
bool has_generate = data.generate_.type() == GenerateFileLocation::Type::Full;
bool has_generate = data.generate_ != nullptr;
if (data.local_.type() == LocalFileLocation::Type::Full && !force) {
if (file_location_source == FileLocationSource::FromDb) {
PathView path_view(data.local_.full().path_);
@ -762,9 +763,9 @@ static int merge_choose(const RemoteFileLocation &x, int8 x_source, const Remote
}
return 2;
}
static int merge_choose(const GenerateFileLocation &x, const GenerateFileLocation &y) {
int32 x_type = static_cast<int32>(x.type());
int32 y_type = static_cast<int32>(y.type());
static int merge_choose(const unique_ptr<FullGenerateFileLocation> &x, const unique_ptr<FullGenerateFileLocation> &y) {
int x_type = static_cast<int>(x != nullptr);
int y_type = static_cast<int>(y != nullptr);
if (x_type != y_type) {
return x_type < y_type;
}
@ -946,7 +947,7 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
other_node->download_priority_ = 0;
//cancel_generate(node);
//node->set_generate_location(other_node->generate_);
//node->set_generate_location(std::move(other_node->generate_));
//node->generate_id_ = other_node->generate_id_;
//node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_);
//other_node->generate_id_ = 0;
@ -974,7 +975,7 @@ Result<FileId> FileManager::merge(FileId x_file_id, FileId y_file_id, bool no_sy
if (generate_i == other_node_i) {
cancel_generate(node);
node->set_generate_location(other_node->generate_);
node->set_generate_location(std::move(other_node->generate_));
node->generate_id_ = other_node->generate_id_;
node->set_generate_priority(other_node->generate_download_priority_, other_node->generate_upload_priority_);
other_node->generate_id_ = 0;
@ -1116,7 +1117,7 @@ void FileManager::clear_from_pmc(FileNodePtr node) {
data.remote_ = node->remote_;
}
if (file_view.has_generate_location()) {
data.generate_ = node->generate_;
data.generate_ = std::make_unique<FullGenerateFileLocation>(*node->generate_);
}
file_db_->clear_file_data(node->pmc_id_, data);
}
@ -1139,11 +1140,8 @@ void FileManager::flush_to_pmc(FileNodePtr node, bool new_remote, bool new_local
prepare_path_for_pmc(data.local_.full().file_type_, data.local_.full().path_);
}
data.remote_ = node->remote_;
data.generate_ = node->generate_;
if (data.generate_.type() == GenerateFileLocation::Type::Full &&
begins_with(data.generate_.full().conversion_, "#file_id#")) {
data.generate_ = GenerateFileLocation();
if (node->generate_ != nullptr && !begins_with(node->generate_->conversion_, "#file_id#")) {
data.generate_ = std::make_unique<FullGenerateFileLocation>(*node->generate_);
}
// TODO: not needed when GenerateLocation has constant convertion
@ -1568,7 +1566,7 @@ void FileManager::run_generate(FileNodePtr node) {
QueryId id = queries_container_.create(Query{file_id, Query::Generate});
node->generate_id_ = id;
send_closure(file_generate_manager_, &FileGenerateManager::generate_file, id, node->generate_.full(), node->local_,
send_closure(file_generate_manager_, &FileGenerateManager::generate_file, id, *node->generate_, node->local_,
node->name_, [file_manager = this, id] {
class Callback : public FileGenerateCallback {
ActorId<FileManager> actor_;

View File

@ -34,8 +34,8 @@ enum class FileLocationSource : int8 { None, FromUser, FromDb, FromServer };
class FileNode {
public:
FileNode(LocalFileLocation local, RemoteFileLocation remote, GenerateFileLocation generate, int64 size,
int64 expected_size, string name, string url, DialogId owner_dialog_id, FileEncryptionKey key,
FileNode(LocalFileLocation local, RemoteFileLocation remote, unique_ptr<FullGenerateFileLocation> generate,
int64 size, int64 expected_size, string name, string url, DialogId owner_dialog_id, FileEncryptionKey key,
FileId main_file_id, int8 main_file_id_priority)
: local_(std::move(local))
, remote_(std::move(remote))
@ -51,7 +51,7 @@ class FileNode {
}
void set_local_location(const LocalFileLocation &local, int64 ready_size);
void set_remote_location(const RemoteFileLocation &remote, FileLocationSource source, int64 ready_size);
void set_generate_location(const GenerateFileLocation &generate);
void set_generate_location(unique_ptr<FullGenerateFileLocation> &&generate);
void set_size(int64 size);
void set_expected_size(int64 expected_size);
void set_name(string name);
@ -85,7 +85,7 @@ class FileNode {
FileLoadManager::QueryId download_id_ = 0;
int64 remote_ready_size_ = 0;
GenerateFileLocation generate_;
unique_ptr<FullGenerateFileLocation> generate_;
FileLoadManager::QueryId generate_id_ = 0;
int64 size_ = 0;