diff options
| author | Dan Engelbrecht <[email protected]> | 2024-05-30 14:44:34 +0200 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2024-05-30 14:44:34 +0200 |
| commit | 8ce1dc72cce381b2adae256504331f2e8893f262 (patch) | |
| tree | df5ab56ce8cdf0f6664ed611f4cf1fb848718ea6 /src | |
| parent | workspaces review feedback (diff) | |
| download | zen-8ce1dc72cce381b2adae256504331f2e8893f262.tar.xz zen-8ce1dc72cce381b2adae256504331f2e8893f262.zip | |
cache optimizations (#88)
* message formatting optimizations
* bump iostorecompression small value threshold to 1MB
Diffstat (limited to 'src')
| -rw-r--r-- | src/zencore/compactbinarypackage.cpp | 4 | ||||
| -rw-r--r-- | src/zencore/include/zencore/compactbinarypackage.h | 4 | ||||
| -rw-r--r-- | src/zencore/include/zencore/compositebuffer.h | 13 | ||||
| -rw-r--r-- | src/zenserver/cache/httpstructuredcache.cpp | 2 | ||||
| -rw-r--r-- | src/zenserver/projectstore/projectstore.cpp | 8 | ||||
| -rw-r--r-- | src/zenstore/cache/cachedisklayer.cpp | 8 | ||||
| -rw-r--r-- | src/zenstore/cache/cacherpc.cpp | 4 | ||||
| -rw-r--r-- | src/zenutil/packageformat.cpp | 62 |
8 files changed, 59 insertions, 46 deletions
diff --git a/src/zencore/compactbinarypackage.cpp b/src/zencore/compactbinarypackage.cpp index 4d4e0951e..0117b326e 100644 --- a/src/zencore/compactbinarypackage.cpp +++ b/src/zencore/compactbinarypackage.cpp @@ -261,7 +261,7 @@ CbAttachment::GetHash() const return Hash; } -CompositeBuffer +const CompositeBuffer& CbAttachment::AsCompositeBinary() const { if (const CompositeBuffer* BinValue = std::get_if<CompositeBuffer>(&Value)) @@ -283,7 +283,7 @@ CbAttachment::AsBinary() const return {}; } -CompressedBuffer +const CompressedBuffer& CbAttachment::AsCompressedBinary() const { if (const CompressedBuffer* CompValue = std::get_if<CompressedBuffer>(&Value)) diff --git a/src/zencore/include/zencore/compactbinarypackage.h b/src/zencore/include/zencore/compactbinarypackage.h index 4a6bca67a..fe4a60a30 100644 --- a/src/zencore/include/zencore/compactbinarypackage.h +++ b/src/zencore/include/zencore/compactbinarypackage.h @@ -77,10 +77,10 @@ public: ZENCORE_API [[nodiscard]] SharedBuffer AsBinary() const; /** Access the attachment as raw binary. Defaults to a null buffer on error. */ - ZENCORE_API [[nodiscard]] CompositeBuffer AsCompositeBinary() const; + ZENCORE_API [[nodiscard]] const CompositeBuffer& AsCompositeBinary() const; /** Access the attachment as compressed binary. Defaults to a null buffer if the attachment is null. */ - ZENCORE_API [[nodiscard]] CompressedBuffer AsCompressedBinary() const; + ZENCORE_API [[nodiscard]] const CompressedBuffer& AsCompressedBinary() const; /** Access the attachment as compact binary. Defaults to a field iterator with no value on error. */ ZENCORE_API [[nodiscard]] CbObject AsObject() const; diff --git a/src/zencore/include/zencore/compositebuffer.h b/src/zencore/include/zencore/compositebuffer.h index 1b6944fc0..b435c5e74 100644 --- a/src/zencore/include/zencore/compositebuffer.h +++ b/src/zencore/include/zencore/compositebuffer.h @@ -123,19 +123,32 @@ private: static inline size_t GetBufferCount(const CompositeBuffer& Buffer) { return Buffer.m_Segments.size(); } inline void AppendBuffers(const CompositeBuffer& Buffer) { + m_Segments.reserve(m_Segments.size() + Buffer.m_Segments.size()); m_Segments.insert(m_Segments.end(), begin(Buffer.m_Segments), end(Buffer.m_Segments)); } inline void AppendBuffers(CompositeBuffer&& Buffer) { AppendBuffers(std::move(Buffer.m_Segments)); } static inline size_t GetBufferCount(const SharedBuffer&) { return 1; } + static inline size_t GetBufferCount(const IoBuffer&) { return 1; } inline void AppendBuffers(const SharedBuffer& Buffer) { m_Segments.push_back(Buffer); } inline void AppendBuffers(SharedBuffer&& Buffer) { m_Segments.push_back(std::move(Buffer)); } + inline void AppendBuffers(IoBuffer&& Buffer) { m_Segments.push_back(SharedBuffer(std::move(Buffer))); } static inline size_t GetBufferCount(std::vector<SharedBuffer>&& Container) { return Container.size(); } + static inline size_t GetBufferCount(std::vector<IoBuffer>&& Container) { return Container.size(); } inline void AppendBuffers(std::vector<SharedBuffer>&& Container) { + m_Segments.reserve(m_Segments.size() + Container.size()); m_Segments.insert(m_Segments.end(), std::make_move_iterator(Container.begin()), std::make_move_iterator(Container.end())); } + inline void AppendBuffers(std::vector<IoBuffer>&& Container) + { + m_Segments.reserve(m_Segments.size() + Container.size()); + for (IoBuffer& Buffer : Container) + { + m_Segments.emplace_back(SharedBuffer(std::move(Buffer))); + } + } private: std::vector<SharedBuffer> m_Segments; diff --git a/src/zenserver/cache/httpstructuredcache.cpp b/src/zenserver/cache/httpstructuredcache.cpp index 449a43653..52e31ff40 100644 --- a/src/zenserver/cache/httpstructuredcache.cpp +++ b/src/zenserver/cache/httpstructuredcache.cpp @@ -883,7 +883,7 @@ HttpStructuredCacheService::HandleGetCacheRecord(HttpServerRequest& Request, con { if (StoreLocal) { - CompressedBuffer Chunk = Attachment->AsCompressedBinary(); + const CompressedBuffer& Chunk = Attachment->AsCompressedBinary(); WriteAttachmentBuffers.push_back(Chunk.GetCompressed().Flatten().AsIoBuffer()); WriteRawHashes.push_back(Attachment->GetHash()); } diff --git a/src/zenserver/projectstore/projectstore.cpp b/src/zenserver/projectstore/projectstore.cpp index 0b7c419ba..4c434c39b 100644 --- a/src/zenserver/projectstore/projectstore.cpp +++ b/src/zenserver/projectstore/projectstore.cpp @@ -1551,8 +1551,8 @@ ProjectStore::Oplog::AppendNewOplogEntry(CbPackage OpPackage) { ZEN_ASSERT(Attach.IsCompressedBinary()); - CompressedBuffer AttachmentData = Attach.AsCompressedBinary(); - const uint64_t AttachmentSize = AttachmentData.DecodeRawSize(); + const CompressedBuffer& AttachmentData = Attach.AsCompressedBinary(); + const uint64_t AttachmentSize = AttachmentData.DecodeRawSize(); WriteAttachmentBuffers.push_back(AttachmentData.GetCompressed().Flatten().AsIoBuffer()); WriteRawHashes.push_back(Attach.GetHash()); WriteRawSizes.push_back(AttachmentSize); @@ -3471,8 +3471,8 @@ ProjectStore::Rpc(HttpServerRequest& HttpReq, for (const CbAttachment& Attachment : Attachments) { - IoHash RawHash = Attachment.GetHash(); - CompressedBuffer Compressed = Attachment.AsCompressedBinary(); + IoHash RawHash = Attachment.GetHash(); + const CompressedBuffer& Compressed = Attachment.AsCompressedBinary(); WriteAttachmentBuffers.push_back(Compressed.GetCompressed().Flatten().AsIoBuffer()); WriteRawHashes.push_back(RawHash); } diff --git a/src/zenstore/cache/cachedisklayer.cpp b/src/zenstore/cache/cachedisklayer.cpp index 2e307118b..d67e8d6c8 100644 --- a/src/zenstore/cache/cachedisklayer.cpp +++ b/src/zenstore/cache/cachedisklayer.cpp @@ -690,6 +690,14 @@ ZenCacheDiskLayer::CacheBucket::CacheBucket(GcManager& Gc, // it makes sense to have a different strategy for legacy values m_Configuration.LargeObjectThreshold = Max(m_Configuration.LargeObjectThreshold, LegacyOverrideSize); } + else if (m_BucketName == std::string_view("iostorecompression")) + { + const uint64_t IoStoreDDCOverrideSize = 1024 * 1024; + + // This is pretty ad hoc but in order to avoid too many individual files + // it makes sense to have a different strategy for ddc pak compression stores + m_Configuration.LargeObjectThreshold = Max(m_Configuration.LargeObjectThreshold, IoStoreDDCOverrideSize); + } m_Gc.AddGcReferencer(*this); } diff --git a/src/zenstore/cache/cacherpc.cpp b/src/zenstore/cache/cacherpc.cpp index d56ceb98e..d28eda8c4 100644 --- a/src/zenstore/cache/cacherpc.cpp +++ b/src/zenstore/cache/cacherpc.cpp @@ -683,7 +683,7 @@ CacheRpcHandler::HandleRpcGetCacheRecords(const CacheRequestContext& Context, Cb bool StoreLocal = EnumHasAllFlags(ValuePolicy, CachePolicy::StoreLocal) && AreDiskWritesAllowed(); if (const CbAttachment* Attachment = Params.Package.FindAttachment(Value.ContentId)) { - if (CompressedBuffer Compressed = Attachment->AsCompressedBinary()) + if (const CompressedBuffer& Compressed = Attachment->AsCompressedBinary()) { Request.Source = Params.Source; Value.Exists = true; @@ -839,7 +839,7 @@ CacheRpcHandler::HandleRpcPutCacheValues(const CacheRequestContext& Context, con { if (Attachment->IsCompressedBinary()) { - CompressedBuffer Chunk = Attachment->AsCompressedBinary(); + const CompressedBuffer& Chunk = Attachment->AsCompressedBinary(); if (EnumHasAllFlags(Policy, CachePolicy::StoreRemote)) { // TODO: Implement upstream puts of CacheValues with StoreLocal == false. diff --git a/src/zenutil/packageformat.cpp b/src/zenutil/packageformat.cpp index 963a442cd..8087b7564 100644 --- a/src/zenutil/packageformat.cpp +++ b/src/zenutil/packageformat.cpp @@ -45,17 +45,7 @@ FormatPackageMessageBuffer(const CbPackage& Data, void* TargetProcessHandle) CompositeBuffer FormatPackageMessageBuffer(const CbPackage& Data, FormatFlags Flags, void* TargetProcessHandle) { - std::vector<IoBuffer> Message = FormatPackageMessage(Data, Flags, TargetProcessHandle); - - std::vector<SharedBuffer> Buffers; - - for (IoBuffer& Buf : Message) - { - ZEN_ASSERT(Buf.GetSize() > 0); - Buffers.push_back(SharedBuffer(Buf)); - } - - return CompositeBuffer(std::move(Buffers)); + return CompositeBuffer(FormatPackageMessage(Data, Flags, TargetProcessHandle)); } static void @@ -76,7 +66,7 @@ MarshalLocal(CbAttachmentEntry*& AttachmentInfo, .Flags = (IsCompressed ? uint32_t(CbAttachmentEntry::kIsCompressed) : 0u) | CbAttachmentEntry::kIsLocalRef, .AttachmentHash = AttachmentHash}; - ResponseBuffers.push_back(std::move(RefBuffer)); + ResponseBuffers.emplace_back(std::move(RefBuffer)); }; static bool @@ -182,29 +172,27 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags, void* TargetProce const std::span<const CbAttachment>& Attachments = Data.GetAttachments(); std::vector<IoBuffer> ResponseBuffers; - ResponseBuffers.reserve(3 + Attachments.size()); // TODO: may want to use an additional fudge factor here to avoid growing since each + ResponseBuffers.reserve(2 + Attachments.size()); // TODO: may want to use an additional fudge factor here to avoid growing since each // attachment is likely to consist of several buffers + IoBuffer AttachmentMetadataBuffer = IoBuffer{sizeof(CbPackageHeader) + sizeof(CbAttachmentEntry) * (Attachments.size() + /* root */ 1)}; + MutableMemoryView HeaderView = AttachmentMetadataBuffer.GetMutableView(); // Fixed size header - CbPackageHeader Hdr{.HeaderMagic = kCbPkgMagic, .AttachmentCount = gsl::narrow<uint32_t>(Attachments.size())}; - - ResponseBuffers.push_back(IoBufferBuilder::MakeCloneFromMemory(&Hdr, sizeof Hdr)); + CbPackageHeader* Hdr = (CbPackageHeader*)HeaderView.GetData(); + *Hdr = {.HeaderMagic = kCbPkgMagic, .AttachmentCount = gsl::narrow<uint32_t>(Attachments.size())}; + HeaderView.MidInline(sizeof(CbPackageHeader)); // Attachment metadata array - - IoBuffer AttachmentMetadataBuffer = IoBuffer{sizeof(CbAttachmentEntry) * (Attachments.size() + /* root */ 1)}; - CbAttachmentEntry* AttachmentInfo = reinterpret_cast<CbAttachmentEntry*>(AttachmentMetadataBuffer.MutableData()); - - ResponseBuffers.push_back(AttachmentMetadataBuffer); // Attachment metadata + CbAttachmentEntry* AttachmentInfo = reinterpret_cast<CbAttachmentEntry*>(HeaderView.GetData()); + ResponseBuffers.emplace_back(std::move(AttachmentMetadataBuffer)); // Attachment metadata // Root object IoBuffer RootIoBuffer = Data.GetObject().GetBuffer().AsIoBuffer(); ZEN_ASSERT(RootIoBuffer.GetSize() > 0); - ResponseBuffers.push_back(RootIoBuffer); // Root object - *AttachmentInfo++ = {.PayloadSize = RootIoBuffer.Size(), .Flags = CbAttachmentEntry::kIsObject, .AttachmentHash = Data.GetObjectHash()}; + ResponseBuffers.emplace_back(std::move(RootIoBuffer)); // Root object // Attachment payloads tsl::robin_map<void*, std::string> FileNameMap; @@ -215,10 +203,10 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags, void* TargetProce { ZEN_NOT_IMPLEMENTED("Null attachments are not supported"); } - else if (CompressedBuffer AttachmentBuffer = Attachment.AsCompressedBinary()) + else if (const CompressedBuffer& AttachmentBuffer = Attachment.AsCompressedBinary()) { - CompositeBuffer Compressed = AttachmentBuffer.GetCompressed(); - IoHash AttachmentHash = Attachment.GetHash(); + const CompositeBuffer& Compressed = AttachmentBuffer.GetCompressed(); + IoHash AttachmentHash = Attachment.GetHash(); // If the data is either not backed by a file, or there are multiple // fragments then we cannot marshal it by local reference. We might @@ -257,10 +245,12 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags, void* TargetProce .Flags = CbAttachmentEntry::kIsCompressed, .AttachmentHash = AttachmentHash}; - for (const SharedBuffer& Segment : Compressed.GetSegments()) + std::span<const SharedBuffer> Segments = Compressed.GetSegments(); + ResponseBuffers.reserve(ResponseBuffers.size() + Segments.size() - 1); + for (const SharedBuffer& Segment : Segments) { ZEN_ASSERT(Segment.GetSize() > 0); - ResponseBuffers.push_back(Segment.AsIoBuffer()); + ResponseBuffers.emplace_back(Segment.AsIoBuffer()); } } } @@ -268,13 +258,13 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags, void* TargetProce { IoBuffer ObjIoBuffer = AttachmentObject.GetBuffer().AsIoBuffer(); ZEN_ASSERT(ObjIoBuffer.GetSize() > 0); - ResponseBuffers.push_back(ObjIoBuffer); + ResponseBuffers.emplace_back(std::move(ObjIoBuffer)); *AttachmentInfo++ = {.PayloadSize = ObjIoBuffer.Size(), .Flags = CbAttachmentEntry::kIsObject, .AttachmentHash = Attachment.GetHash()}; } - else if (CompositeBuffer AttachmentBinary = Attachment.AsCompositeBinary()) + else if (const CompositeBuffer& AttachmentBinary = Attachment.AsCompositeBinary()) { IoHash AttachmentHash = Attachment.GetHash(); bool MarshalByLocalRef = @@ -309,10 +299,12 @@ FormatPackageMessage(const CbPackage& Data, FormatFlags Flags, void* TargetProce { *AttachmentInfo++ = {.PayloadSize = AttachmentBinary.GetSize(), .Flags = 0, .AttachmentHash = Attachment.GetHash()}; - for (const SharedBuffer& Segment : AttachmentBinary.GetSegments()) + std::span<const SharedBuffer> Segments = AttachmentBinary.GetSegments(); + ResponseBuffers.reserve(ResponseBuffers.size() + Segments.size() - 1); + for (const SharedBuffer& Segment : Segments) { ZEN_ASSERT(Segment.GetSize() > 0); - ResponseBuffers.push_back(Segment.AsIoBuffer()); + ResponseBuffers.emplace_back(Segment.AsIoBuffer()); } } } @@ -640,7 +632,7 @@ CbPackageReader::ProcessPackageHeaderData(const void* Data, uint64_t DataBytes) // the caller will need to handle the payload differently (i.e it's a // CbAttachmentReferenceHeader not the actual payload) - m_PayloadBuffers.push_back(IoBuffer{Entry.PayloadSize}); + m_PayloadBuffers.emplace_back(IoBuffer{Entry.PayloadSize}); } m_CurrentState = State::kReadingBuffers; @@ -738,14 +730,14 @@ CbPackageReader::Finalize() CompressedBuffer Compressed = CompressedBuffer::FromCompressed(SharedBuffer(ChunkReference), RawHash, RawSize); if (RawHash == Entry.AttachmentHash) { - m_Attachments.push_back(CbAttachment(Compressed, Entry.AttachmentHash)); + m_Attachments.emplace_back(CbAttachment(Compressed, Entry.AttachmentHash)); } } else { CompressedBuffer Compressed = CompressedBuffer::Compress(SharedBuffer(ChunkReference), OodleCompressor::NotSet, OodleCompressionLevel::None); - m_Attachments.push_back(CbAttachment(std::move(Compressed), Compressed.DecodeRawHash())); + m_Attachments.emplace_back(CbAttachment(std::move(Compressed), Compressed.DecodeRawHash())); } } |