Skip to content

Commit

Permalink
Optimize YamlParser with move semantics
Browse files Browse the repository at this point in the history
- Improved the efficiency of the `YamlParser` by utilizing move semantics for the `Document` class in `Yaml.h`.
- Updated `YamlParser.cpp` to call `GetRoot` with `std::move(doc).GetRoot()` to avoid unnecessary copying.
- Modified `GetRoot` in `Yaml.h` to return an r-value reference (`Node&&`).
- Also, updated `GetSchemaHeader` to return a constant reference (`const DocumentSchemaHeader&`) to reduce copying.
  • Loading branch information
Madhusudhan-MSFT committed Jan 17, 2025
1 parent 812f547 commit db264b6
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/AppInstallerCommonCore/Manifest/YamlParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ namespace AppInstaller::Manifest::YamlParser

YamlManifestInfo manifestInfo;
YAML::Document doc = YAML::LoadDocument(file.path());
manifestInfo.Root = doc.GetRoot();
manifestInfo.Root = std::move(doc).GetRoot();
manifestInfo.DocumentSchemaHeader = doc.GetSchemaHeader();
manifestInfo.FileName = file.path().filename().u8string();
docList.emplace_back(std::move(manifestInfo));
Expand All @@ -538,7 +538,7 @@ namespace AppInstaller::Manifest::YamlParser
{
YamlManifestInfo manifestInfo;
YAML::Document doc = YAML::LoadDocument(inputPath, manifestInfo.StreamSha256);
manifestInfo.Root = doc.GetRoot();
manifestInfo.Root = std::move(doc).GetRoot();
manifestInfo.DocumentSchemaHeader = doc.GetSchemaHeader();
manifestInfo.FileName = inputPath.filename().u8string();
docList.emplace_back(std::move(manifestInfo));
Expand All @@ -563,7 +563,7 @@ namespace AppInstaller::Manifest::YamlParser
{
YamlManifestInfo manifestInfo;
YAML::Document doc = YAML::LoadDocument(input);
manifestInfo.Root = doc.GetRoot();
manifestInfo.Root = std::move(doc).GetRoot();
manifestInfo.DocumentSchemaHeader = doc.GetSchemaHeader();
docList.emplace_back(std::move(manifestInfo));
}
Expand Down
5 changes: 3 additions & 2 deletions src/AppInstallerSharedLib/Public/winget/Yaml.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,10 @@ namespace AppInstaller::YAML
Document() = default;
Document(Node root, DocumentSchemaHeader schemaHeader) : m_root(std::move(root)), m_schemaHeader(std::move(schemaHeader)) {}

const DocumentSchemaHeader& GetSchemaHeader() const { return m_schemaHeader; }

// Return r-values for move semantics
Node GetRoot() const { return m_root; }
DocumentSchemaHeader GetSchemaHeader() const { return m_schemaHeader; }
Node&& GetRoot() && { return std::move(m_root); }

private:
Node m_root;
Expand Down

0 comments on commit db264b6

Please sign in to comment.