clang-tools  15.0.0git
MemoryTree.cpp
Go to the documentation of this file.
1 //===--- MemoryTree.h - A special tree for components and sizes -----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "support/MemoryTree.h"
10 #include "Trace.h"
11 #include "llvm/ADT/StringRef.h"
12 #include <cstddef>
13 
14 namespace clang {
15 namespace clangd {
16 
17 namespace {
18 
19 size_t traverseTree(const MemoryTree &MT, std::string &ComponentName,
20  const trace::Metric &Out) {
21  size_t OriginalLen = ComponentName.size();
22  if (!ComponentName.empty())
23  ComponentName += '.';
24  size_t Total = MT.self();
25  for (const auto &Entry : MT.children()) {
26  ComponentName += Entry.first;
27  Total += traverseTree(Entry.getSecond(), ComponentName, Out);
28  ComponentName.resize(OriginalLen + 1);
29  }
30  ComponentName.resize(OriginalLen);
31  Out.record(Total, ComponentName);
32  return Total;
33 }
34 } // namespace
35 
36 MemoryTree &MemoryTree::createChild(llvm::StringRef Name) {
37  auto &Child = Children.try_emplace(Name, DetailAlloc).first->getSecond();
38  return Child;
39 }
40 
41 const llvm::DenseMap<llvm::StringRef, MemoryTree> &
43  return Children;
44 }
45 
46 size_t MemoryTree::total() const {
47  size_t Total = Size;
48  for (const auto &Entry : Children)
49  Total += Entry.getSecond().total();
50  return Total;
51 }
52 
53 void record(const MemoryTree &MT, std::string RootName,
54  const trace::Metric &Out) {
55  traverseTree(MT, RootName, Out);
56 }
57 } // namespace clangd
58 } // namespace clang
Total
unsigned Total
Definition: FunctionCognitiveComplexityCheck.cpp:145
Trace.h
clang::clangd::MemoryTree::total
size_t total() const
Returns total number of bytes used by this sub-tree. Performs a traversal.
Definition: MemoryTree.cpp:46
clang::clangd::MemoryTree
A tree that can be used to represent memory usage of nested components while preserving the hierarchy...
Definition: MemoryTree.h:30
Name
Token Name
Definition: MacroToEnumCheck.cpp:89
clang::clangd::MemoryTree::children
const llvm::DenseMap< llvm::StringRef, MemoryTree > & children() const
Returns edges to direct children of this node.
Definition: MemoryTree.cpp:42
Entry
Definition: Modularize.cpp:428
clang::clangd::trace::Metric
Represents measurements of clangd events, e.g.
Definition: Trace.h:38
MemoryTree.h
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ApplyReplacements.h:27
clang::clangd::record
void record(const MemoryTree &MT, std::string RootName, const trace::Metric &Out)
Records total memory usage of each node under Out.
Definition: MemoryTree.cpp:53
Out
CompiledFragmentImpl & Out
Definition: ConfigCompile.cpp:99