diff --git a/td/telegram/files/FileUploader.cpp b/td/telegram/files/FileUploader.cpp index c4b7809f..d83d9022 100644 --- a/td/telegram/files/FileUploader.cpp +++ b/td/telegram/files/FileUploader.cpp @@ -277,12 +277,10 @@ Result> FileUploader::start_part(Part part, int32 p if (big_flag_) { auto query = telegram_api::upload_saveBigFilePart(file_id_, part.id, local_is_ready_ ? part_count : -1, std::move(bytes)); - net_query = G()->net_query_creator().create(create_storer(query), DcId::main(), NetQuery::Type::Upload, - NetQuery::AuthFlag::On, NetQuery::GzipFlag::Off); + net_query = G()->net_query_creator().create(create_storer(query), DcId::main(), NetQuery::Type::Upload); } else { auto query = telegram_api::upload_saveFilePart(file_id_, part.id, std::move(bytes)); - net_query = G()->net_query_creator().create(create_storer(query), DcId::main(), NetQuery::Type::Upload, - NetQuery::AuthFlag::On, NetQuery::GzipFlag::Off); + net_query = G()->net_query_creator().create(create_storer(query), DcId::main(), NetQuery::Type::Upload); } net_query->file_type_ = narrow_cast(file_type_); return std::make_pair(std::move(net_query), false); diff --git a/td/telegram/net/NetQueryCreator.cpp b/td/telegram/net/NetQueryCreator.cpp index 9e2f0564..f3b801ab 100644 --- a/td/telegram/net/NetQueryCreator.cpp +++ b/td/telegram/net/NetQueryCreator.cpp @@ -14,19 +14,26 @@ namespace td { NetQueryCreator::Ptr NetQueryCreator::create(uint64 id, const Storer &storer, DcId dc_id, NetQuery::Type type, - NetQuery::AuthFlag auth_flag, NetQuery::GzipFlag gzip_flag) { + NetQuery::AuthFlag auth_flag) { BufferSlice slice(storer.size()); auto real_size = storer.store(slice.as_slice().ubegin()); LOG_CHECK(real_size == slice.size()) << real_size << " " << slice.size() << " " << format::as_hex_dump<4>(Slice(slice.as_slice())); - // TODO: magic constant - if (slice.size() < (1 << 8)) { - gzip_flag = NetQuery::GzipFlag::Off; - } int32 tl_constructor = NetQuery::tl_magic(slice); + + size_t MIN_GZIPPED_SIZE = 128; + auto gzip_flag = slice.size() < MIN_GZIPPED_SIZE ? NetQuery::GzipFlag::Off : NetQuery::GzipFlag::On; + if (slice.size() >= 16384) { + // test compression ratio for the middle part + // if it is less than 0.9, then try to compress the whole request + size_t TESTED_SIZE = 1024; + BufferSlice compressed_part = gzencode(slice.as_slice().substr((slice.size() - TESTED_SIZE) / 2, TESTED_SIZE), 0.9); + if (compressed_part.empty()) { + gzip_flag = NetQuery::GzipFlag::Off; + } + } if (gzip_flag == NetQuery::GzipFlag::On) { - // TODO: try to compress files? BufferSlice compressed = gzencode(slice.as_slice(), 0.9); if (compressed.empty()) { gzip_flag = NetQuery::GzipFlag::Off; diff --git a/td/telegram/net/NetQueryCreator.h b/td/telegram/net/NetQueryCreator.h index 353eebe3..98fd1e21 100644 --- a/td/telegram/net/NetQueryCreator.h +++ b/td/telegram/net/NetQueryCreator.h @@ -35,13 +35,11 @@ class NetQueryCreator { } Ptr create(const Storer &storer, DcId dc_id = DcId::main(), NetQuery::Type type = NetQuery::Type::Common, - NetQuery::AuthFlag auth_flag = NetQuery::AuthFlag::On, - NetQuery::GzipFlag gzip_flag = NetQuery::GzipFlag::On) { - return create(UniqueId::next(), storer, dc_id, type, auth_flag, gzip_flag); + NetQuery::AuthFlag auth_flag = NetQuery::AuthFlag::On) { + return create(UniqueId::next(), storer, dc_id, type, auth_flag); } Ptr create(uint64 id, const Storer &storer, DcId dc_id = DcId::main(), NetQuery::Type type = NetQuery::Type::Common, - NetQuery::AuthFlag auth_flag = NetQuery::AuthFlag::On, - NetQuery::GzipFlag gzip_flag = NetQuery::GzipFlag::On); + NetQuery::AuthFlag auth_flag = NetQuery::AuthFlag::On); private: ObjectPool object_pool_;