Skip to content

Commit c0cdc00

Browse files
authored
Merge pull request #141 from wravery/field_path
Various memory optimizations
2 parents bd725ac + f7fc628 commit c0cdc00

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+2403
-1195
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ endfunction()
4545

4646
find_package(Threads MODULE REQUIRED)
4747

48-
find_package(pegtl 3.1.0 QUIET CONFIG)
48+
find_package(pegtl 3.1.1 QUIET CONFIG)
4949
if(NOT pegtl_FOUND)
5050
# If a compatible version of PEGTL is not already installed, build and install it from the submodule directory.
5151
set(PEGTL_BUILD_TESTS OFF CACHE BOOL "Disable PEGTL tests")

include/SchemaGenerator.h

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@
77
#define SCHEMAGENERATOR_H
88

99
#include "graphqlservice/GraphQLGrammar.h"
10+
#include "graphqlservice/GraphQLParse.h"
1011
#include "graphqlservice/GraphQLService.h"
1112

1213
#include <array>
1314
#include <cstdio>
15+
#include <unordered_map>
16+
#include <unordered_set>
1417

1518
namespace graphql::schema {
1619

@@ -24,10 +27,10 @@ enum class BuiltinType
2427
ID,
2528
};
2629

27-
using BuiltinTypeMap = std::map<std::string, BuiltinType>;
30+
using BuiltinTypeMap = std::map<std::string_view, BuiltinType>;
2831

2932
// These are the C++ types we'll use for them.
30-
using CppTypeMap = std::array<std::string, static_cast<size_t>(BuiltinType::ID) + 1>;
33+
using CppTypeMap = std::array<std::string_view, static_cast<size_t>(BuiltinType::ID) + 1>;
3134

3235
// Types that we understand and use to generate the skeleton of a service.
3336
enum class SchemaType
@@ -41,14 +44,14 @@ enum class SchemaType
4144
Operation,
4245
};
4346

44-
using SchemaTypeMap = std::map<std::string, SchemaType>;
47+
using SchemaTypeMap = std::map<std::string_view, SchemaType>;
4548

4649
// Keep track of the positions of each type declaration in the file.
47-
using PositionMap = std::unordered_map<std::string, tao::graphqlpeg::position>;
50+
using PositionMap = std::unordered_map<std::string_view, tao::graphqlpeg::position>;
4851

4952
// For all of the named types we track, we want to keep them in order in a vector but
5053
// be able to lookup their offset quickly by name.
51-
using TypeNameMap = std::unordered_map<std::string, size_t>;
54+
using TypeNameMap = std::unordered_map<std::string_view, size_t>;
5255

5356
// Any type can also have a list and/or non-nullable wrapper, and those can be nested.
5457
// Since it's easier to express nullability than non-nullability in C++, we'll invert
@@ -60,28 +63,28 @@ using TypeModifierStack = std::vector<service::TypeModifier>;
6063
// scalar type names have been declared so we recognize the references.
6164
struct ScalarType
6265
{
63-
std::string type;
64-
std::string description;
66+
std::string_view type;
67+
std::string_view description;
6568
};
6669

6770
using ScalarTypeList = std::vector<ScalarType>;
6871

6972
// Enum types map a type name to a collection of valid string values.
7073
struct EnumValueType
7174
{
72-
std::string value;
73-
std::string cppValue;
74-
std::string description;
75-
std::optional<std::string> deprecationReason;
75+
std::string_view value;
76+
std::string_view cppValue;
77+
std::string_view description;
78+
std::optional<std::string_view> deprecationReason;
7679
std::optional<tao::graphqlpeg::position> position;
7780
};
7881

7982
struct EnumType
8083
{
81-
std::string type;
82-
std::string cppType;
84+
std::string_view type;
85+
std::string_view cppType;
8386
std::vector<EnumValueType> values;
84-
std::string description;
87+
std::string_view description;
8588
};
8689

8790
using EnumTypeList = std::vector<EnumType>;
@@ -99,47 +102,47 @@ enum class InputFieldType
99102

100103
struct InputField
101104
{
102-
std::string type;
103-
std::string name;
104-
std::string cppName;
105-
std::string defaultValueString;
105+
std::string_view type;
106+
std::string_view name;
107+
std::string_view cppName;
108+
std::string_view defaultValueString;
106109
response::Value defaultValue;
107110
InputFieldType fieldType = InputFieldType::Builtin;
108111
TypeModifierStack modifiers;
109-
std::string description;
112+
std::string_view description;
110113
std::optional<tao::graphqlpeg::position> position;
111114
};
112115

113116
using InputFieldList = std::vector<InputField>;
114117

115118
struct InputType
116119
{
117-
std::string type;
118-
std::string cppType;
120+
std::string_view type;
121+
std::string_view cppType;
119122
InputFieldList fields;
120-
std::string description;
123+
std::string_view description;
121124
};
122125

123126
using InputTypeList = std::vector<InputType>;
124127

125128
// Directives are defined with arguments and a list of valid locations.
126129
struct Directive
127130
{
128-
std::string name;
129-
std::vector<std::string> locations;
131+
std::string_view name;
132+
std::vector<std::string_view> locations;
130133
InputFieldList arguments;
131-
std::string description;
134+
std::string_view description;
132135
};
133136

134137
using DirectiveList = std::vector<Directive>;
135138

136139
// Union types map a type name to a set of potential concrete type names.
137140
struct UnionType
138141
{
139-
std::string type;
140-
std::string cppType;
141-
std::vector<std::string> options;
142-
std::string description;
142+
std::string_view type;
143+
std::string_view cppType;
144+
std::vector<std::string_view> options;
145+
std::string_view description;
143146
};
144147

145148
using UnionTypeList = std::vector<UnionType>;
@@ -163,14 +166,14 @@ constexpr std::string_view strApply = "apply";
163166

164167
struct OutputField
165168
{
166-
std::string type;
167-
std::string name;
168-
std::string cppName;
169+
std::string_view type;
170+
std::string_view name;
171+
std::string_view cppName;
169172
InputFieldList arguments;
170173
OutputFieldType fieldType = OutputFieldType::Builtin;
171174
TypeModifierStack modifiers;
172-
std::string description;
173-
std::optional<std::string> deprecationReason;
175+
std::string_view description;
176+
std::optional<std::string_view> deprecationReason;
174177
std::optional<tao::graphqlpeg::position> position;
175178
bool interfaceField = false;
176179
bool inheritedField = false;
@@ -185,10 +188,10 @@ using OutputFieldList = std::vector<OutputField>;
185188
// conditions. The fields can include any output type.
186189
struct InterfaceType
187190
{
188-
std::string type;
189-
std::string cppType;
191+
std::string_view type;
192+
std::string_view cppType;
190193
OutputFieldList fields;
191-
std::string description;
194+
std::string_view description;
192195
};
193196

194197
using InterfaceTypeList = std::vector<InterfaceType>;
@@ -197,22 +200,22 @@ using InterfaceTypeList = std::vector<InterfaceType>;
197200
// may inherit multiple interfaces.
198201
struct ObjectType
199202
{
200-
std::string type;
201-
std::string cppType;
202-
std::vector<std::string> interfaces;
203-
std::vector<std::string> unions;
203+
std::string_view type;
204+
std::string_view cppType;
205+
std::vector<std::string_view> interfaces;
206+
std::vector<std::string_view> unions;
204207
OutputFieldList fields;
205-
std::string description;
208+
std::string_view description;
206209
};
207210

208211
using ObjectTypeList = std::vector<ObjectType>;
209212

210213
// The schema maps operation types to named types.
211214
struct OperationType
212215
{
213-
std::string type;
214-
std::string cppType;
215-
std::string operation;
216+
std::string_view type;
217+
std::string_view cppType;
218+
std::string_view operation;
216219
};
217220

218221
using OperationTypeList = std::vector<OperationType>;
@@ -318,17 +321,16 @@ class Generator
318321
void visitObjectTypeExtension(const peg::ast_node& objectTypeExtension);
319322
void visitDirectiveDefinition(const peg::ast_node& directiveDefinition);
320323

321-
static const std::string& getSafeCppName(const std::string& type) noexcept;
322-
static OutputFieldList getOutputFields(
323-
const std::vector<std::unique_ptr<peg::ast_node>>& fields);
324-
static InputFieldList getInputFields(const std::vector<std::unique_ptr<peg::ast_node>>& fields);
324+
static std::string_view getSafeCppName(std::string_view type) noexcept;
325+
static OutputFieldList getOutputFields(const peg::ast_node::children_t& fields);
326+
static InputFieldList getInputFields(const peg::ast_node::children_t& fields);
325327

326328
// Recursively visit a Type node until we reach a NamedType and we've
327329
// taken stock of all of the modifier wrappers.
328330
class TypeVisitor
329331
{
330332
public:
331-
std::pair<std::string, TypeModifierStack> getType();
333+
std::pair<std::string_view, TypeModifierStack> getType();
332334

333335
void visit(const peg::ast_node& typeName);
334336

@@ -337,7 +339,7 @@ class Generator
337339
void visitListType(const peg::ast_node& listType);
338340
void visitNonNullType(const peg::ast_node& nonNullType);
339341

340-
std::string _type;
342+
std::string_view _type;
341343
TypeModifierStack _modifiers;
342344
bool _nonNull = false;
343345
};
@@ -366,11 +368,11 @@ class Generator
366368

367369
void validateSchema();
368370
void fixupOutputFieldList(OutputFieldList& fields,
369-
const std::optional<std::unordered_set<std::string>>& interfaceFields,
371+
const std::optional<std::unordered_set<std::string_view>>& interfaceFields,
370372
const std::optional<std::string_view>& accessor);
371373
void fixupInputFieldList(InputFieldList& fields);
372374

373-
const std::string& getCppType(const std::string& type) const noexcept;
375+
std::string_view getCppType(std::string_view type) const noexcept;
374376
std::string getInputCppType(const InputField& field) const noexcept;
375377
std::string getOutputCppType(const OutputField& field) const noexcept;
376378

@@ -393,14 +395,14 @@ class Generator
393395
std::string getResultAccessType(const OutputField& result) const noexcept;
394396
std::string getTypeModifiers(const TypeModifierStack& modifiers) const noexcept;
395397
std::string getIntrospectionType(
396-
const std::string& type, const TypeModifierStack& modifiers) const noexcept;
398+
std::string_view type, const TypeModifierStack& modifiers) const noexcept;
397399

398400
std::vector<std::string> outputSeparateFiles() const noexcept;
399401

400-
static const std::string s_introspectionNamespace;
402+
static const std::string_view s_introspectionNamespace;
401403
static const BuiltinTypeMap s_builtinTypes;
402404
static const CppTypeMap s_builtinCppTypes;
403-
static const std::string s_scalarCppType;
405+
static const std::string_view s_scalarCppType;
404406
static const std::string s_currentDirectory;
405407

406408
const GeneratorOptions _options;
@@ -411,6 +413,7 @@ class Generator
411413
const std::string _headerPath;
412414
const std::string _objectHeaderPath;
413415
const std::string _sourcePath;
416+
peg::ast _ast;
414417

415418
SchemaTypeMap _schemaTypes;
416419
PositionMap _typePositions;

0 commit comments

Comments
 (0)