From 649453ce0b6c1294e112dc6724c3aa25b195c3c7 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 2 Jan 2019 21:17:08 +0300 Subject: [PATCH] Reduce sizeof(LocalFileLocation). GitOrigin-RevId: cd6911a2654535303bb2b086052e767d5a912caf --- td/telegram/files/FileLocation.h | 43 ++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/td/telegram/files/FileLocation.h b/td/telegram/files/FileLocation.h index cf01b472..38a0a673 100644 --- a/td/telegram/files/FileLocation.h +++ b/td/telegram/files/FileLocation.h @@ -910,6 +910,38 @@ inline StringBuilder &operator<<(StringBuilder &sb, const FullLocalFileLocation return sb << "[full local location of " << location.file_type_ << "] at \"" << location.path_ << '"'; } +struct PartialLocalFileLocationPtr { + unique_ptr location_; // must never be equal to nullptr + + PartialLocalFileLocationPtr() : location_(make_unique()) { + } + explicit PartialLocalFileLocationPtr(PartialLocalFileLocation location) + : location_(make_unique(location)) { + } + PartialLocalFileLocationPtr(const PartialLocalFileLocationPtr &other) + : location_(make_unique(*other.location_)) { + } + PartialLocalFileLocationPtr &operator=(const PartialLocalFileLocationPtr &other) { + *location_ = *other.location_; + } + PartialLocalFileLocationPtr(PartialLocalFileLocationPtr &&other) + : location_(make_unique(std::move(*other.location_))) { + } + PartialLocalFileLocationPtr &operator=(PartialLocalFileLocationPtr &&other) { + *location_ = std::move(*other.location_); + } + ~PartialLocalFileLocationPtr() = default; + + template + void store(StorerT &storer) const { + td::store(*location_, storer); + } +}; + +inline bool operator==(const PartialLocalFileLocationPtr &lhs, const PartialLocalFileLocationPtr &rhs) { + return *lhs.location_ == *rhs.location_; +} + class LocalFileLocation { public: enum class Type : int32 { Empty, Partial, Full }; @@ -919,13 +951,13 @@ class LocalFileLocation { } PartialLocalFileLocation &partial() { - return variant_.get<1>(); + return *variant_.get<1>().location_; } FullLocalFileLocation &full() { return variant_.get<2>(); } const PartialLocalFileLocation &partial() const { - return variant_.get<1>(); + return *variant_.get<1>().location_; } const FullLocalFileLocation &full() const { return variant_.get<2>(); @@ -961,7 +993,7 @@ class LocalFileLocation { variant_ = EmptyLocalFileLocation(); return; case Type::Partial: - variant_ = PartialLocalFileLocation(); + variant_ = PartialLocalFileLocationPtr(); return parse(partial(), parser); case Type::Full: variant_ = FullLocalFileLocation(); @@ -972,7 +1004,8 @@ class LocalFileLocation { LocalFileLocation() : variant_{EmptyLocalFileLocation()} { } - explicit LocalFileLocation(const PartialLocalFileLocation &partial) : variant_(partial) { + explicit LocalFileLocation(const PartialLocalFileLocation &partial) + : variant_(PartialLocalFileLocationPtr(partial)) { } explicit LocalFileLocation(const FullLocalFileLocation &full) : variant_(full) { } @@ -981,7 +1014,7 @@ class LocalFileLocation { } private: - Variant variant_; + Variant variant_; friend bool operator==(const LocalFileLocation &lhs, const LocalFileLocation &rhs); };