From 56f39208a56ce3d28a463084afffedb7a4c24ca4 Mon Sep 17 00:00:00 2001 From: hyperxor Date: Tue, 31 May 2022 23:41:00 +0300 Subject: [PATCH] Small perfomance optimization in Directives and Tag methods --- src/directives.cpp | 12 ++++++++---- src/directives.h | 2 +- src/tag.cpp | 23 ++++++++++++++++------- src/tag.h | 2 +- 4 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/directives.cpp b/src/directives.cpp index f6e9587..8653ccf 100644 --- a/src/directives.cpp +++ b/src/directives.cpp @@ -3,15 +3,19 @@ namespace YAML { Directives::Directives() : version{true, 1, 2}, tags{} {} -const std::string Directives::TranslateTagHandle( +std::string Directives::TranslateTagHandle( const std::string& handle) const { + std::string result; auto it = tags.find(handle); if (it == tags.end()) { if (handle == "!!") - return "tag:yaml.org,2002:"; - return handle; + result = "tag:yaml.org,2002:"; + else + result = handle; + } else { + result = it->second; } - return it->second; + return result; } } // namespace YAML diff --git a/src/directives.h b/src/directives.h index 333af26..18d14c9 100644 --- a/src/directives.h +++ b/src/directives.h @@ -19,7 +19,7 @@ struct Version { struct Directives { Directives(); - const std::string TranslateTagHandle(const std::string& handle) const; + std::string TranslateTagHandle(const std::string& handle) const; Version version; std::map tags; diff --git a/src/tag.cpp b/src/tag.cpp index df8a2cf..0a8168e 100644 --- a/src/tag.cpp +++ b/src/tag.cpp @@ -29,22 +29,31 @@ Tag::Tag(const Token& token) } } -const std::string Tag::Translate(const Directives& directives) { +std::string Tag::Translate(const Directives& directives) { + std::string result; + switch (type) { case VERBATIM: - return value; + result = value; + break; case PRIMARY_HANDLE: - return directives.TranslateTagHandle("!") + value; + result = directives.TranslateTagHandle("!") + value; + break; case SECONDARY_HANDLE: - return directives.TranslateTagHandle("!!") + value; + result = directives.TranslateTagHandle("!!") + value; + break; case NAMED_HANDLE: - return directives.TranslateTagHandle("!" + handle + "!") + value; + result = directives.TranslateTagHandle("!" + handle + "!") + value; + break; case NON_SPECIFIC: // TODO: - return "!"; + result = "!"; + break; default: assert(false); + throw std::runtime_error("yaml-cpp: internal error, bad tag type"); } - throw std::runtime_error("yaml-cpp: internal error, bad tag type"); + + return result; } } // namespace YAML diff --git a/src/tag.h b/src/tag.h index ac30673..c811f39 100644 --- a/src/tag.h +++ b/src/tag.h @@ -23,7 +23,7 @@ struct Tag { }; Tag(const Token& token); - const std::string Translate(const Directives& directives); + std::string Translate(const Directives& directives); TYPE type; std::string handle, value;