Skip to content

Commit a2d711b

Browse files
committed
Avoid emitting std::move for built-in C++ scalar types
1 parent d1624be commit a2d711b

File tree

7 files changed

+89
-11
lines changed

7 files changed

+89
-11
lines changed

include/SchemaLoader.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
namespace graphql::generator {
2121

2222
// These are the set of built-in types in GraphQL.
23-
enum class [[nodiscard]] BuiltinType {
23+
enum class [[nodiscard]] BuiltinType
24+
{
2425
Int,
2526
Float,
2627
String,
@@ -75,7 +76,8 @@ using EnumTypeList = std::vector<EnumType>;
7576
// Input types are complex types that have a set of named fields. Each field may be
7677
// a scalar type (including lists or non-null wrappers) or another nested input type,
7778
// but it cannot include output object types.
78-
enum class [[nodiscard]] InputFieldType {
79+
enum class [[nodiscard]] InputFieldType
80+
{
7981
Builtin,
8082
Scalar,
8183
Enum,
@@ -136,7 +138,8 @@ using UnionTypeList = std::vector<UnionType>;
136138
// field may be a scalar type (including lists or non-null wrappers) or another nested
137139
// output type, but it cannot include input object types. Each field can also take
138140
// optional arguments which are all input types.
139-
enum class [[nodiscard]] OutputFieldType {
141+
enum class [[nodiscard]] OutputFieldType
142+
{
140143
Builtin,
141144
Scalar,
142145
Enum,
@@ -264,6 +267,8 @@ class [[nodiscard]] SchemaLoader
264267
[[nodiscard]] static std::string getOutputCppAccessor(const OutputField& field) noexcept;
265268
[[nodiscard]] static std::string getOutputCppResolver(const OutputField& field) noexcept;
266269

270+
[[nodiscard]] static bool shouldMoveInputField(const InputField& field) noexcept;
271+
267272
private:
268273
[[nodiscard]] static bool isExtension(const peg::ast_node& definition) noexcept;
269274

samples/learn/schema/StarWarsSchema.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ learn::ReviewInput Argument<learn::ReviewInput>::convert(const response::Value&
8787
auto valueCommentary = service::ModifiedArgument<std::string>::require<service::TypeModifier::Nullable>("commentary", value);
8888

8989
return learn::ReviewInput {
90-
std::move(valueStars),
90+
valueStars,
9191
std::move(valueCommentary)
9292
};
9393
}

samples/today/nointrospection/TodaySchema.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ today::CompleteTaskInput Argument<today::CompleteTaskInput>::convert(const respo
105105

106106
return today::CompleteTaskInput {
107107
std::move(valueId),
108-
std::move(valueTestTaskState),
108+
valueTestTaskState,
109109
std::move(valueIsComplete),
110110
std::move(valueClientMutationId)
111111
};

samples/today/schema/TodaySchema.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ today::CompleteTaskInput Argument<today::CompleteTaskInput>::convert(const respo
105105

106106
return today::CompleteTaskInput {
107107
std::move(valueId),
108-
std::move(valueTestTaskState),
108+
valueTestTaskState,
109109
std::move(valueIsComplete),
110110
std::move(valueClientMutationId)
111111
};

samples/validation/schema/ArgumentsObject.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ service::AwaitableResolver Arguments::resolveOptionalNonNullBooleanArgField(serv
158158

159159
auto pairOptionalBooleanArg = service::ModifiedArgument<bool>::find("optionalBooleanArg", params.arguments);
160160
auto argOptionalBooleanArg = (pairOptionalBooleanArg.second
161-
? std::move(pairOptionalBooleanArg.first)
161+
? pairOptionalBooleanArg.first
162162
: service::ModifiedArgument<bool>::require("optionalBooleanArg", defaultArguments));
163163
std::unique_lock resolverLock(_resolverMutex);
164164
service::SelectionSetParams selectionSetParams { static_cast<const service::SelectionSetParams&>(params) };

src/SchemaGenerator.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,10 +1441,25 @@ void Result<)cpp" << _loader.getSchemaNamespace()
14411441
)cpp";
14421442
}
14431443

1444+
const bool shouldMove = SchemaLoader::shouldMoveInputField(inputField);
1445+
14441446
firstField = false;
14451447
fieldName[0] =
14461448
static_cast<char>(std::toupper(static_cast<unsigned char>(fieldName[0])));
1447-
sourceFile << R"cpp( std::move(value)cpp" << fieldName << R"cpp())cpp";
1449+
1450+
sourceFile << R"cpp( )cpp";
1451+
1452+
if (shouldMove)
1453+
{
1454+
sourceFile << R"cpp(std::move()cpp";
1455+
}
1456+
1457+
sourceFile << R"cpp(value)cpp" << fieldName;
1458+
1459+
if (shouldMove)
1460+
{
1461+
sourceFile << R"cpp())cpp";
1462+
}
14481463
}
14491464

14501465
sourceFile << R"cpp(
@@ -2396,7 +2411,8 @@ service::AwaitableResolver )cpp"
23962411

23972412
if (!_loader.isIntrospection())
23982413
{
2399-
sourceFile << R"cpp( service::SelectionSetParams selectionSetParams { static_cast<const service::SelectionSetParams&>(params) };
2414+
sourceFile
2415+
<< R"cpp( service::SelectionSetParams selectionSetParams { static_cast<const service::SelectionSetParams&>(params) };
24002416
auto directives = std::move(params.fieldDirectives);
24012417
)cpp";
24022418
}
@@ -2775,8 +2791,23 @@ std::string Generator::getArgumentDeclaration(const InputField& argument, const
27752791
<< argument.name << R"cpp(", )cpp" << argumentsToken << R"cpp();
27762792
auto )cpp" << prefixToken
27772793
<< argumentName << R"cpp( = (pair)cpp" << argumentName << R"cpp(.second
2778-
? std::move(pair)cpp"
2779-
<< argumentName << R"cpp(.first)
2794+
? )cpp";
2795+
2796+
const bool shouldMove = SchemaLoader::shouldMoveInputField(argument);
2797+
2798+
if (shouldMove)
2799+
{
2800+
argumentDeclaration << R"cpp(std::move()cpp";
2801+
}
2802+
2803+
argumentDeclaration << R"cpp(pair)cpp" << argumentName << R"cpp(.first)cpp";
2804+
2805+
if (shouldMove)
2806+
{
2807+
argumentDeclaration << R"cpp())cpp";
2808+
}
2809+
2810+
argumentDeclaration << R"cpp(
27802811
: )cpp" << getArgumentAccessType(argument)
27812812
<< R"cpp(::require)cpp" << getTypeModifiers(argument.modifiers)
27822813
<< R"cpp((")cpp" << argument.name << R"cpp(", )cpp" << defaultToken

src/SchemaLoader.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,48 @@ std::string SchemaLoader::getOutputCppResolver(const OutputField& field) noexcep
18731873
return getJoinedCppName(R"cpp(resolve)cpp"sv, field.name);
18741874
}
18751875

1876+
bool SchemaLoader::shouldMoveInputField(const InputField& field) noexcept
1877+
{
1878+
bool shouldMove = true;
1879+
1880+
switch (field.fieldType)
1881+
{
1882+
case InputFieldType::Builtin:
1883+
{
1884+
if (field.modifiers.empty() || field.modifiers.front() == service::TypeModifier::None)
1885+
{
1886+
const auto& builtinTypes = getBuiltinTypes();
1887+
1888+
switch (builtinTypes.at(field.type))
1889+
{
1890+
case BuiltinType::Int:
1891+
case BuiltinType::Float:
1892+
case BuiltinType::Boolean:
1893+
shouldMove = false;
1894+
break;
1895+
1896+
default:
1897+
break;
1898+
}
1899+
}
1900+
1901+
break;
1902+
}
1903+
1904+
case InputFieldType::Enum:
1905+
{
1906+
shouldMove =
1907+
field.modifiers.empty() || field.modifiers.front() == service::TypeModifier::None;
1908+
break;
1909+
}
1910+
1911+
default:
1912+
break;
1913+
}
1914+
1915+
return shouldMove;
1916+
}
1917+
18761918
std::string SchemaLoader::getJoinedCppName(
18771919
std::string_view prefix, std::string_view fieldName) noexcept
18781920
{

0 commit comments

Comments
 (0)