From ed313ecce0479ec128905301ab36bf619ac22e93 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 4 Oct 2020 22:42:29 -0700 Subject: [PATCH] Fixed out of control memory / CPU usage caused by unnecessary track parsing. --- .../library/query/CategoryTrackListQuery.cpp | 2 +- .../library/query/SearchTrackListQuery.cpp | 2 +- src/core/library/query/TrackMetadataQuery.cpp | 2 +- src/core/library/query/util/Serialization.cpp | 32 +++++++++++++------ src/core/library/query/util/Serialization.h | 7 ++-- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/core/library/query/CategoryTrackListQuery.cpp b/src/core/library/query/CategoryTrackListQuery.cpp index f8b5f4dc8..6cec04cb2 100755 --- a/src/core/library/query/CategoryTrackListQuery.cpp +++ b/src/core/library/query/CategoryTrackListQuery.cpp @@ -228,7 +228,7 @@ void CategoryTrackListQuery::DeserializeResult(const std::string& data) { this->SetStatus(IQuery::Failed); nlohmann::json result = nlohmann::json::parse(data)["result"]; this->result = std::make_shared(this->library); - TrackListFromJson(result["trackList"], *this->result, this->library); + TrackListFromJson(result["trackList"], *this->result, this->library, true); JsonArrayToSet, size_t>(result["headers"], *this->headers); this->SetStatus(IQuery::Finished); } diff --git a/src/core/library/query/SearchTrackListQuery.cpp b/src/core/library/query/SearchTrackListQuery.cpp index 52e9803cc..f6a4fe3a9 100755 --- a/src/core/library/query/SearchTrackListQuery.cpp +++ b/src/core/library/query/SearchTrackListQuery.cpp @@ -193,7 +193,7 @@ void SearchTrackListQuery::DeserializeResult(const std::string& data) { this->SetStatus(IQuery::Failed); nlohmann::json result = nlohmann::json::parse(data)["result"]; this->result = std::make_shared(this->library); - TrackListFromJson(result["trackList"], *this->result, this->library); + TrackListFromJson(result["trackList"], *this->result, this->library, true); JsonArrayToSet, size_t>(result["headers"], *this->headers); this->SetStatus(IQuery::Finished); } diff --git a/src/core/library/query/TrackMetadataQuery.cpp b/src/core/library/query/TrackMetadataQuery.cpp index e786485bb..ad2554301 100644 --- a/src/core/library/query/TrackMetadataQuery.cpp +++ b/src/core/library/query/TrackMetadataQuery.cpp @@ -177,7 +177,7 @@ void TrackMetadataQuery::DeserializeResult(const std::string& data) { this->SetStatus(IQuery::Failed); auto input = nlohmann::json::parse(data); auto parsedResult = std::make_shared(-1LL, this->library); - TrackFromJson(input["result"], parsedResult); + TrackFromJson(input["result"], parsedResult, false); this->result = parsedResult; this->SetStatus(IQuery::Finished); } diff --git a/src/core/library/query/util/Serialization.cpp b/src/core/library/query/util/Serialization.cpp index 30de08a5e..7458338ae 100644 --- a/src/core/library/query/util/Serialization.cpp +++ b/src/core/library/query/util/Serialization.cpp @@ -38,6 +38,7 @@ using namespace musik::core; using namespace musik::core::library::query; +using namespace musik::core::sdk; namespace musik { namespace core { namespace library { namespace query { @@ -117,7 +118,7 @@ namespace musik { namespace core { namespace library { namespace query { #define COPY_TRACK_FIELD_TO_JSON(track, json, field) \ json[field] = track->GetString(field); - nlohmann::json TrackToJson(const musik::core::TrackPtr input, bool onlyIds) { + nlohmann::json TrackToJson(const TrackPtr input, bool onlyIds) { nlohmann::json output; output["id"] = input->GetId(); @@ -198,22 +199,35 @@ namespace musik { namespace core { namespace library { namespace query { replayGain.trackPeak = replayGainJson.value("trackPeak", 1.0f); } - output->SetMetadataState(musik::core::sdk::MetadataState::Loaded); + output->SetMetadataState(MetadataState::Loaded); } - nlohmann::json TrackListToJson(const musik::core::TrackList& input, bool onlyIds) { + nlohmann::json TrackListToJson(const TrackList& input, bool onlyIds) { nlohmann::json output; - for (size_t i = 0; i < input.Count(); i++) { - output.push_back(TrackToJson(input.Get(i), onlyIds)); + if (onlyIds) { + for (size_t i = 0; i < input.Count(); i++) { + output.push_back(input.GetId(i)); + } + } + else { + for (size_t i = 0; i < input.Count(); i++) { + output.push_back(TrackToJson(input.Get(i), onlyIds)); + } } return output; } - void TrackListFromJson(const nlohmann::json& input, musik::core::TrackList& output, musik::core::ILibraryPtr library) { + void TrackListFromJson(const nlohmann::json& input, TrackList& output, ILibraryPtr library, bool onlyIds) { output.Clear(); - for (auto& trackJson : input) { - TrackPtr track = std::make_shared(-1LL, library); - output.Add(trackJson["id"].get()); + if (onlyIds) { + for (auto& trackId : input) { + output.Add(trackId.get()); + } + } + else { + for (auto& trackJson : input) { + output.Add(trackJson["id"].get()); + } } } diff --git a/src/core/library/query/util/Serialization.h b/src/core/library/query/util/Serialization.h index e36a8f3e2..bfd92c2ea 100644 --- a/src/core/library/query/util/Serialization.h +++ b/src/core/library/query/util/Serialization.h @@ -74,16 +74,17 @@ namespace musik { namespace core { namespace library { namespace query { void TrackFromJson( const nlohmann::json& input, musik::core::TrackPtr output, - bool onlyIds = false); + bool onlyIds); nlohmann::json TrackListToJson( const musik::core::TrackList& input, - bool onlyIds = false); + bool onlyIds); void TrackListFromJson( const nlohmann::json& input, musik::core::TrackList& output, - musik::core::ILibraryPtr library); + musik::core::ILibraryPtr library, + bool onlyIds); template void JsonArrayToSet(const nlohmann::json& input, SetType& output) {