Skip to content

Commit 385ae4b

Browse files
committed
[llvm][mustache] Use BumpPtrAllocator to save ASTNodes
We make the Mustache ASTNodes usable in the arena by first removing all of the memory owning data structures, like std::vector, std::unique_ptr, and SmallVector. We use standard LLVM list types to hold this data instead, and make use of a UniqueStringSaver to hold the various templates strings. Additionally, update clang-doc APIs to use the new interfaces. Future work can make better use of Twine interfaces to help avoid any intermediate copies or allocations.
1 parent df32a14 commit 385ae4b

File tree

5 files changed

+697
-247
lines changed

5 files changed

+697
-247
lines changed

clang-tools-extra/clang-doc/HTMLMustacheGenerator.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,22 @@ class MustacheHTMLGenerator : public Generator {
4646
const ClangDocContext &CDCtx) override;
4747
};
4848

49-
class MustacheTemplateFile : public Template {
49+
class MustacheTemplateFile {
50+
BumpPtrAllocator Allocator;
51+
StringSaver Saver;
52+
MustacheContext Ctx;
53+
Template T;
54+
std::unique_ptr<MemoryBuffer> Buffer;
55+
5056
public:
5157
static Expected<std::unique_ptr<MustacheTemplateFile>>
5258
createMustacheFile(StringRef FileName) {
5359
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrError =
5460
MemoryBuffer::getFile(FileName);
5561
if (auto EC = BufferOrError.getError())
5662
return createFileOpenError(FileName, EC);
57-
58-
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
59-
StringRef FileContent = Buffer->getBuffer();
60-
return std::make_unique<MustacheTemplateFile>(FileContent);
63+
return std::make_unique<MustacheTemplateFile>(
64+
std::move(BufferOrError.get()));
6165
}
6266

6367
Error registerPartialFile(StringRef Name, StringRef FileName) {
@@ -68,11 +72,15 @@ class MustacheTemplateFile : public Template {
6872

6973
std::unique_ptr<MemoryBuffer> Buffer = std::move(BufferOrError.get());
7074
StringRef FileContent = Buffer->getBuffer();
71-
registerPartial(Name.str(), FileContent.str());
75+
T.registerPartial(Name.str(), FileContent.str());
7276
return Error::success();
7377
}
7478

75-
MustacheTemplateFile(StringRef TemplateStr) : Template(TemplateStr) {}
79+
void render(json::Value &V, raw_ostream &OS) { T.render(V, OS); }
80+
81+
MustacheTemplateFile(std::unique_ptr<MemoryBuffer> &&B)
82+
: Saver(Allocator), Ctx(Allocator, Saver), T(B->getBuffer(), Ctx),
83+
Buffer(std::move(B)) {}
7684
};
7785

7886
static std::unique_ptr<MustacheTemplateFile> NamespaceTemplate = nullptr;

llvm/include/llvm/Support/Mustache.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171

7272
#include "Error.h"
7373
#include "llvm/ADT/StringMap.h"
74+
#include "llvm/ADT/ilist.h"
75+
#include "llvm/ADT/ilist_node.h"
7476
#include "llvm/Support/Allocator.h"
7577
#include "llvm/Support/Compiler.h"
7678
#include "llvm/Support/JSON.h"
@@ -84,10 +86,15 @@ using Lambda = std::function<llvm::json::Value()>;
8486
using SectionLambda = std::function<llvm::json::Value(std::string)>;
8587

8688
class ASTNode;
87-
using AstPtr = std::unique_ptr<ASTNode>;
89+
using AstPtr = ASTNode *;
8890
using EscapeMap = DenseMap<char, std::string>;
91+
using ASTNodeList = iplist<ASTNode>;
8992

9093
struct MustacheContext {
94+
MustacheContext(BumpPtrAllocator &Allocator, StringSaver &Saver)
95+
: Allocator(Allocator), Saver(Saver) {}
96+
BumpPtrAllocator &Allocator;
97+
StringSaver &Saver;
9198
StringMap<AstPtr> Partials;
9299
StringMap<Lambda> Lambdas;
93100
StringMap<SectionLambda> SectionLambdas;
@@ -98,7 +105,7 @@ struct MustacheContext {
98105
// and Lambdas that are registered with it.
99106
class Template {
100107
public:
101-
LLVM_ABI Template(StringRef TemplateStr);
108+
LLVM_ABI Template(StringRef TemplateStr, MustacheContext &Ctx);
102109

103110
Template(const Template &) = delete;
104111

@@ -110,7 +117,7 @@ class Template {
110117
// type.
111118
LLVM_ABI ~Template();
112119

113-
LLVM_ABI Template &operator=(Template &&Other) noexcept;
120+
Template &operator=(Template &&) = delete;
114121

115122
LLVM_ABI void render(const llvm::json::Value &Data, llvm::raw_ostream &OS);
116123

@@ -126,7 +133,7 @@ class Template {
126133
LLVM_ABI void overrideEscapeCharacters(DenseMap<char, std::string> Escapes);
127134

128135
private:
129-
MustacheContext Ctx;
136+
MustacheContext &Ctx;
130137
AstPtr Tree;
131138
};
132139
} // namespace llvm::mustache

0 commit comments

Comments
 (0)