# HG changeset patch # User Sebastien Jodogne # Date 1780409599 -7200 # Node ID 89677acd8f8d5842aa229680ff7620559dbc679e # Parent 3417bcf097a0ff1da905fee285e4a1adcd59bfbe optimization: avoid reading from storage area if extracting range from a cached attachment diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/DataSourceReader.cpp --- a/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DataSourceReader.cpp Tue Jun 02 16:13:19 2026 +0200 @@ -180,7 +180,7 @@ preReservation.reset(new Internals::DataSourceMemoryBudget::Lock(*budget_, estimatedSize)); } - value.reset(source_.Load(*id_)); + value.reset(source_.Load(*id_, cache_)); if (!value) { diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/DicomDataSource.cpp --- a/OrthancFramework/Sources/DataSource/DicomDataSource.cpp Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DicomDataSource.cpp Tue Jun 02 16:13:19 2026 +0200 @@ -183,7 +183,8 @@ } - IDynamicObject* DicomDataSource::Load(const IDataIdentifier& obj) + IDynamicObject* DicomDataSource::Load(const IDataIdentifier& obj, + const boost::shared_ptr& readerCache) { const Identifier& id = dynamic_cast(obj); diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/DicomDataSource.h --- a/OrthancFramework/Sources/DataSource/DicomDataSource.h Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/DicomDataSource.h Tue Jun 02 16:13:19 2026 +0200 @@ -60,7 +60,8 @@ public: explicit DicomDataSource(const boost::shared_ptr& storageAreaReader); - virtual IDynamicObject* Load(const IDataIdentifier& obj) ORTHANC_OVERRIDE; + virtual IDynamicObject* Load(const IDataIdentifier& obj, + const boost::shared_ptr& readerCache /* could be NULL */) ORTHANC_OVERRIDE; virtual size_t GetValueSize(const IDynamicObject& obj) const ORTHANC_OVERRIDE; diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/IDataSource.h --- a/OrthancFramework/Sources/DataSource/IDataSource.h Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/IDataSource.h Tue Jun 02 16:13:19 2026 +0200 @@ -27,8 +27,13 @@ #include "../IDynamicObject.h" #include "IDataIdentifier.h" +#include + + namespace Orthanc { + class SharedObjectCache; + class ORTHANC_PUBLIC IDataSource : public boost::noncopyable { public: @@ -36,7 +41,8 @@ { } - virtual IDynamicObject* Load(const IDataIdentifier& identifier) = 0; + virtual IDynamicObject* Load(const IDataIdentifier& identifier, + const boost::shared_ptr& readerCache /* could be NULL */) = 0; virtual size_t GetValueSize(const IDynamicObject& value) const = 0; }; diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp --- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.cpp Tue Jun 02 16:13:19 2026 +0200 @@ -25,6 +25,7 @@ #include "../PrecompiledHeaders.h" #include "StorageAreaDataSource.h" +#include "../Cache/SharedObjectCache.h" #include "../MetricsRegistry.h" #include "../OrthancException.h" #include "../StringMemoryBuffer.h" @@ -42,6 +43,19 @@ namespace Orthanc { + static std::string ComputeCacheKey(std::string uuid, + FileContentType type, + uint64_t start, + uint64_t end) + { + // custom data is not part of the cache key, as it is for internal use by the plugin + // (it is opaque to the Orthanc core) + return (uuid + "|" + + boost::lexical_cast(type) + "|" + + boost::lexical_cast(start) + "|" + + boost::lexical_cast(end)); + } + class StorageAreaDataSource::Value : public IDynamicObject { private: @@ -70,6 +84,21 @@ { return checkMD5_; } + + void ExtractRange(std::string& target, + uint64_t start, + uint64_t end) const + { + if (start >= buffer_->GetSize() || + end > buffer_->GetSize()) + { + throw OrthancException(ErrorCode_BadRange); + } + else + { + target.assign(reinterpret_cast(buffer_->GetData()) + start, end - start); + } + } }; @@ -94,6 +123,8 @@ uint64_t end_; std::string customData_; std::unique_ptr postProcessing_; + bool hasWholeKey_; + std::string wholeKey_; public: Identifier(const std::string& uuid, @@ -105,7 +136,8 @@ type_(type), start_(start), end_(end), - customData_(customData) + customData_(customData), + hasWholeKey_(false) { if (start > end) { @@ -119,6 +151,54 @@ } } + uint64_t GetStart() const + { + return start_; + } + + uint64_t GetEnd() const + { + return end_; + } + + /** + * WARNING: SetWholeKey() must only be used to retrieve compressed + * date from the storage area (or if there is no compression). + **/ + void SetWholeKey(const std::string& key) + { + if (hasWholeKey_) + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + else if (key.empty()) + { + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + else + { + hasWholeKey_ = true; + wholeKey_ = key; + } + } + + bool HasWholeKey() const + { + return hasWholeKey_; + } + + const std::string GetWholeKey() const + { + if (hasWholeKey_) + { + return wholeKey_; + } + else + { + throw OrthancException(ErrorCode_BadSequenceOfCalls); + } + } + IMemoryBuffer* Read(IPluginStorageArea& area) const { return area.ReadRange(uuid_, type_, start_, end_, customData_); @@ -126,12 +206,7 @@ virtual bool GetCacheKey(std::string& key) const ORTHANC_OVERRIDE { - // custom data is not part of the cache key, as it is for internal use by the plugin - // (it is opaque to the Orthanc core) - key = (uuid_ + "|" + - boost::lexical_cast(type_) + "|" + - boost::lexical_cast(start_) + "|" + - boost::lexical_cast(end_)); + key = ComputeCacheKey(uuid_, type_, start_, end_); return true; } @@ -305,10 +380,27 @@ } - IDynamicObject* StorageAreaDataSource::Load(const IDataIdentifier& identifier) + IDynamicObject* StorageAreaDataSource::Load(const IDataIdentifier& identifier, + const boost::shared_ptr& readerCache) { const Identifier& id = dynamic_cast(identifier); + if (id.HasWholeKey() && + readerCache) + { + // Extract the request range from the whole compressed attachment if the latter is already present in the cache + boost::shared_ptr wholeCached = readerCache->GetCachedValue(id.GetWholeKey()); + if (wholeCached) + { + const Value& wholeRange = dynamic_cast(*wholeCached); + + std::string content; + wholeRange.ExtractRange(content, id.GetStart(), id.GetEnd()); + + return new Value(StringMemoryBuffer::CreateFromSwap(content), false); + } + } + std::unique_ptr buffer; { @@ -430,8 +522,14 @@ throw OrthancException(ErrorCode_ParameterOutOfRange); } - return CreateRangeRequest(attachment.GetUuid(), attachment.GetContentType(), - 0, untilPosition, attachment.GetCustomData()); + std::unique_ptr id(new Identifier(attachment.GetUuid(), attachment.GetContentType(), + 0, untilPosition, attachment.GetCustomData())); + + // Using "SetWholeKey()" allows to extract a range if the whole attachment is already in the reader cache + assert(attachment.GetCompressionType() == CompressionType_None); + id->SetWholeKey(ComputeCacheKey(attachment.GetUuid(), attachment.GetContentType(), 0, attachment.GetCompressedSize())); + + return id.release(); } @@ -468,8 +566,14 @@ } else { - return Execute(reader, CreateRangeRequest(attachment.GetUuid(), attachment.GetContentType(), - start, endExclusive, attachment.GetCustomData())); + std::unique_ptr id(new Identifier(attachment.GetUuid(), attachment.GetContentType(), + start, endExclusive, attachment.GetCustomData())); + + // Using "SetWholeKey()" allows to extract a range if the whole compressed attachment is already in the reader cache + assert(!uncompress || attachment.GetCompressionType() == CompressionType_None); + id->SetWholeKey(ComputeCacheKey(attachment.GetUuid(), attachment.GetContentType(), 0, attachment.GetCompressedSize())); + + return Execute(reader, id.release()); } } } diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/StorageAreaDataSource.h --- a/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/StorageAreaDataSource.h Tue Jun 02 16:13:19 2026 +0200 @@ -69,7 +69,8 @@ virtual size_t GetValueSize(const IDynamicObject& value) const ORTHANC_OVERRIDE; - virtual IDynamicObject* Load(const IDataIdentifier& identifier) ORTHANC_OVERRIDE; + virtual IDynamicObject* Load(const IDataIdentifier& identifier, + const boost::shared_ptr& readerCache /* could be NULL */) ORTHANC_OVERRIDE; class ORTHANC_PUBLIC Range : public boost::noncopyable { diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp --- a/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.cpp Tue Jun 02 16:13:19 2026 +0200 @@ -245,7 +245,8 @@ } - IDynamicObject* TranscoderDataSource::Load(const IDataIdentifier& id) + IDynamicObject* TranscoderDataSource::Load(const IDataIdentifier& id, + const boost::shared_ptr& readerCache) { return dynamic_cast(id).Load(*transcoder_, *storageAreaReader_); } diff -r 3417bcf097a0 -r 89677acd8f8d OrthancFramework/Sources/DataSource/TranscoderDataSource.h --- a/OrthancFramework/Sources/DataSource/TranscoderDataSource.h Tue Jun 02 15:03:11 2026 +0200 +++ b/OrthancFramework/Sources/DataSource/TranscoderDataSource.h Tue Jun 02 16:13:19 2026 +0200 @@ -49,7 +49,8 @@ TranscoderDataSource(const boost::shared_ptr& transcoder, const boost::shared_ptr& storageAreaReader); - virtual IDynamicObject* Load(const IDataIdentifier& id) ORTHANC_OVERRIDE; + virtual IDynamicObject* Load(const IDataIdentifier& id, + const boost::shared_ptr& readerCache /* could be NULL */) ORTHANC_OVERRIDE; virtual size_t GetValueSize(const IDynamicObject& value) const ORTHANC_OVERRIDE;