From c2aa0616cdd2b64912e07da8894be3d37e43685d Mon Sep 17 00:00:00 2001 From: Amir Ayupov Date: Thu, 6 Jul 2023 10:16:20 -0700 Subject: [PATCH] [YAML][NFC] Replace if-else with switch in createHNodes BOLT YAML profile reading time gets marginally faster (14.1572->13.9207 s) for a large YAML profile (121MB/31K functions). Not claiming stat significance though. Reviewed By: hintonda Differential Revision: https://reviews.llvm.org/D154553 --- llvm/lib/Support/YAMLTraits.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp index 0273059e7c27..f21b7a0ca699 100644 --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -397,17 +397,23 @@ void Input::reportWarning(const SMRange &range, const Twine &message) { std::unique_ptr Input::createHNodes(Node *N) { SmallString<128> StringStorage; - if (ScalarNode *SN = dyn_cast(N)) { + switch (N->getType()) { + case Node::NK_Scalar: { + ScalarNode *SN = dyn_cast(N); StringRef KeyStr = SN->getValue(StringStorage); if (!StringStorage.empty()) { // Copy string to permanent storage KeyStr = StringStorage.str().copy(StringAllocator); } return std::make_unique(N, KeyStr); - } else if (BlockScalarNode *BSN = dyn_cast(N)) { + } + case Node::NK_BlockScalar: { + BlockScalarNode *BSN = dyn_cast(N); StringRef ValueCopy = BSN->getValue().copy(StringAllocator); return std::make_unique(N, ValueCopy); - } else if (SequenceNode *SQ = dyn_cast(N)) { + } + case Node::NK_Sequence: { + SequenceNode *SQ = dyn_cast(N); auto SQHNode = std::make_unique(N); for (Node &SN : *SQ) { auto Entry = createHNodes(&SN); @@ -416,7 +422,9 @@ std::unique_ptr Input::createHNodes(Node *N) { SQHNode->Entries.push_back(std::move(Entry)); } return std::move(SQHNode); - } else if (MappingNode *Map = dyn_cast(N)) { + } + case Node::NK_Mapping: { + MappingNode *Map = dyn_cast(N); auto mapHNode = std::make_unique(N); for (KeyValueNode &KVN : *Map) { Node *KeyNode = KVN.getKey(); @@ -447,9 +455,10 @@ std::unique_ptr Input::createHNodes(Node *N) { std::make_pair(std::move(ValueHNode), KeyNode->getSourceRange()); } return std::move(mapHNode); - } else if (isa(N)) { + } + case Node::NK_Null: return std::make_unique(N); - } else { + default: setError(N, "unknown node kind"); return nullptr; }