From 03a5ea07f90a22d695b77d5a08a1561a3ae0a68c Mon Sep 17 00:00:00 2001 From: Peter Caspers Date: Fri, 4 Oct 2024 10:34:48 +0200 Subject: [PATCH] QPR-12849 avoid copying the time series of fixings --- ql/index.hpp | 3 +-- ql/indexes/indexmanager.cpp | 4 ++++ ql/indexes/indexmanager.hpp | 2 ++ ql/utilities/observablevalue.hpp | 6 ++++++ 4 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ql/index.hpp b/ql/index.hpp index 6419700ff64..57da0ff14e4 100644 --- a/ql/index.hpp +++ b/ql/index.hpp @@ -101,7 +101,7 @@ namespace QuantLib { bool forceOverwrite = false) { checkNativeFixingsAllowed(); std::string tag = name(); - TimeSeries h = IndexManager::instance().getHistory(tag); + TimeSeries& h = IndexManager::instance().getHistoryRef(tag); bool noInvalidFixing = true, noDuplicatedFixing = true; Date invalidDate, duplicatedDate; Real nullValue = Null(); @@ -128,7 +128,6 @@ namespace QuantLib { invalidValue = *(vBegin++); } } - IndexManager::instance().setHistory(tag, h); QL_REQUIRE(noInvalidFixing, "At least one invalid fixing provided: " << invalidDate.weekday() << " " << invalidDate << ", " << invalidValue); diff --git a/ql/indexes/indexmanager.cpp b/ql/indexes/indexmanager.cpp index 19f4c4f77de..39bb14d0b26 100644 --- a/ql/indexes/indexmanager.cpp +++ b/ql/indexes/indexmanager.cpp @@ -29,6 +29,10 @@ namespace QuantLib { return data_[name].value(); } + TimeSeries& IndexManager::getHistoryRef(const std::string& name) { + return data_[name].ref(); + } + void IndexManager::setHistory(const std::string& name, TimeSeries history) { data_[name] = std::move(history); } diff --git a/ql/indexes/indexmanager.hpp b/ql/indexes/indexmanager.hpp index 0115aabfe93..2e18fbc3ed6 100644 --- a/ql/indexes/indexmanager.hpp +++ b/ql/indexes/indexmanager.hpp @@ -45,6 +45,8 @@ namespace QuantLib { bool hasHistory(const std::string& name) const; //! returns the (possibly empty) history of the index fixings const TimeSeries& getHistory(const std::string& name) const; + //! returns a ref to the (possibly empty) history of the index fixings + TimeSeries& getHistoryRef(const std::string& name); //! stores the historical fixings of the index void setHistory(const std::string& name, TimeSeries history); //! observer notifying of changes in the index fixings diff --git a/ql/utilities/observablevalue.hpp b/ql/utilities/observablevalue.hpp index aa54ce7846f..a35fa972bb7 100644 --- a/ql/utilities/observablevalue.hpp +++ b/ql/utilities/observablevalue.hpp @@ -57,6 +57,8 @@ namespace QuantLib { operator ext::shared_ptr() const; //! explicit inspector const T& value() const; + //! explicit reference + T& ref(); private: T value_; ext::shared_ptr observable_; @@ -118,6 +120,10 @@ namespace QuantLib { return value_; } + template + T& ObservableValue::ref() { + return value_; + } } #endif