Skip to content

Commit 3cf136f

Browse files
author
rafalh
committed
Merge branch 'benchmark' into HEAD
2 parents b1a4d0a + 8cb8c71 commit 3cf136f

File tree

12 files changed

+133
-7
lines changed

12 files changed

+133
-7
lines changed

include/graphqlservice/GraphQLService.h

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,13 +1109,45 @@ struct ModifiedResult
11091109
co_await params.launch;
11101110

11111111
auto awaitedResult = co_await std::move(result);
1112+
using vector_type = std::decay_t<decltype(awaitedResult)>;
1113+
1114+
// Shortcut for std::vector of non-awaitable scalars. No need to go async. Results are ready.
1115+
if constexpr (std::is_floating_point_v<typename vector_type::value_type>
1116+
|| std::is_integral_v<typename vector_type::value_type >//also for bool
1117+
|| std::is_same<std::string, typename vector_type::value_type>::value
1118+
|| std::is_same<response::IdType, typename vector_type::value_type>::value)
1119+
{
1120+
ResolverResult document;
1121+
document.data.push_back(response::ValueToken::StartArray {});
1122+
document.data.push_back(response::ValueToken::Reserve { awaitedResult.size() });
1123+
if constexpr (std::is_same<bool, typename vector_type::value_type>::value)
1124+
{
1125+
for (auto r : awaitedResult)
1126+
document.data.append(response::ValueToken::BoolValue { std::move(r) });
1127+
}
1128+
else
1129+
{
1130+
for (auto& r : awaitedResult)
1131+
{
1132+
if constexpr (std::is_integral_v<typename vector_type::value_type>)
1133+
document.data.append(response::ValueToken::IntValue { std::move(r) });
1134+
else if constexpr (std::is_floating_point_v<typename vector_type::value_type>)
1135+
document.data.append(response::ValueToken::FloatValue { std::move(r) });
1136+
else if constexpr (std::is_same<response::IdType,
1137+
typename vector_type::value_type>::value)
1138+
document.data.append(response::ValueToken::IdValue { std::move(r) });
1139+
else document.data.append(response::ValueToken::StringValue { std::move(r) });
1140+
}
1141+
}
1142+
document.data.push_back(response::ValueToken::EndArray {});
1143+
co_return document;
1144+
}
11121145

11131146
children.reserve(awaitedResult.size());
11141147
params.errorPath = std::make_optional(
11151148
field_path { parentPath ? std::make_optional(std::cref(*parentPath)) : std::nullopt,
11161149
path_segment { std::size_t { 0 } } });
11171150

1118-
using vector_type = std::decay_t<decltype(awaitedResult)>;
11191151

11201152
if constexpr (!std::is_same_v<std::decay_t<typename vector_type::reference>,
11211153
typename vector_type::value_type>)

include/graphqlservice/JSONResponse.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
#define JSONRESPONSE_H
88

99
#include "GraphQLResponse.h"
10+
#include "GraphQLService.h"
1011

1112
#include "internal/DllExports.h"
1213

1314
namespace graphql::response {
1415

1516
[[nodiscard("unnecessary conversion")]] JSONRESPONSE_EXPORT std::string toJSON(Value&& response);
17+
[[nodiscard("unnecessary conversion")]] JSONRESPONSE_EXPORT std::string toJSON(service::ResolverResult&& response);
1618

1719
[[nodiscard("unnecessary conversion")]] JSONRESPONSE_EXPORT Value parseJSON(
1820
const std::string& json);

samples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ if(GRAPHQL_BUILD_MODULES)
1414
add_subdirectory(stitched)
1515
endif()
1616

17+
1718
add_subdirectory(validation)
1819

1920
if(GRAPHQL_BUILD_HTTP_SAMPLE)

samples/proxy/schema/schema.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ enum OperationType {
1111
SUBSCRIPTION
1212
}
1313

14+
15+
1416
input QueryInput {
1517
type: OperationType!
1618
query: String!

samples/today/TodayMock.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,17 @@ std::optional<std::vector<std::shared_ptr<object::AppointmentEdge>>> Appointment
229229
return result;
230230
}
231231

232+
std::vector<double> AppointmentConnection::getBigArray() const noexcept
233+
{
234+
std::vector<double> ret(1000);
235+
int i = 0;
236+
for (auto& r : ret)
237+
{
238+
r = i++;
239+
}
240+
return ret;
241+
}
242+
232243
Task::Task(response::IdType&& id, std::string&& title, bool isComplete)
233244
: _id(std::move(id))
234245
, _title(std::make_shared<response::Value>(std::move(title)))

samples/today/TodayMock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class AppointmentConnection
174174

175175
std::shared_ptr<object::PageInfo> getPageInfo() const noexcept;
176176
std::optional<std::vector<std::shared_ptr<object::AppointmentEdge>>> getEdges() const noexcept;
177+
std::vector<double> getBigArray() const noexcept;
177178

178179
private:
179180
std::shared_ptr<PageInfo> _pageInfo;

samples/today/benchmark.cpp

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ int main(int argc, char** argv)
106106
isNow
107107
}
108108
}
109+
bigArray
109110
}
110111
})gql"sv);
111112
const auto startValidate = std::chrono::steady_clock::now();
@@ -117,14 +118,60 @@ int main(int argc, char** argv)
117118
}
118119

119120
const auto startResolve = std::chrono::steady_clock::now();
120-
auto response = service->resolve({ query }).get();
121-
const auto startToJson = std::chrono::steady_clock::now();
122121

123-
if (response::toJSON(std::move(response)).empty())
122+
decltype(std::chrono::steady_clock::now()) startToJson;
123+
124+
125+
126+
/*
127+
Z tego zrobić test
128+
std::string a = response::toJSON(service->visit({ query }).get());
129+
std::string b = response::toJSON(service->resolve({ query }).get());
130+
131+
std::cout << "Visit: " << a << std::endl;
132+
std::cout << "Resolve: " << b << std::endl;
133+
134+
if (a.compare(b) != 0)
124135
{
125-
std::cerr << "Failed to convert to JSON!" << std::endl;
126-
break;
136+
std::cout << "Visit"
137+
<< " is not "
138+
<< "Resolve" << std::endl;
127139
}
140+
else
141+
{
142+
std::cout << "Visit is equal Resolve" << std::endl;
143+
}*/
144+
145+
146+
147+
if (0)
148+
{
149+
auto resolverResult = service->visit({ query }).get();
150+
startToJson = std::chrono::steady_clock::now();
151+
152+
if (response::toJSON(std::move(resolverResult)).empty())
153+
{
154+
std::cerr << "Failed to convert to JSON!" << std::endl;
155+
break;
156+
}
157+
}
158+
else
159+
{
160+
auto response = service->resolve({ query }).get();
161+
startToJson = std::chrono::steady_clock::now();
162+
163+
if (response::toJSON(std::move(response)).empty())
164+
{
165+
std::cerr << "Failed to convert to JSON!" << std::endl;
166+
break;
167+
}
168+
}
169+
170+
171+
172+
173+
174+
128175

129176
const auto endToJson = std::chrono::steady_clock::now();
130177

samples/today/schema.today.graphql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ type Query {
4040

4141
"Test C++ keyword names"
4242
default: String
43+
44+
4345
}
4446

4547
"Node interface for Relay support"
@@ -60,6 +62,7 @@ type AppointmentEdge {
6062
type AppointmentConnection {
6163
pageInfo: PageInfo!
6264
edges: [AppointmentEdge]
65+
bigArray: [Float!]!
6366
}
6467

6568
type TaskEdge {

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ endif()
404404
if(BUILD_GRAPHQLJSON)
405405
add_library(cppgraphqlgen::graphqljson ALIAS graphqljson)
406406
target_compile_features(graphqljson PUBLIC cxx_std_20)
407-
target_link_libraries(graphqljson PUBLIC graphqlresponse)
407+
target_link_libraries(graphqljson PUBLIC graphqlresponse graphqlservice)
408408
target_sources(graphqljson PUBLIC FILE_SET HEADERS
409409
BASE_DIRS ${INCLUDE_ROOT}
410410
FILES ${INCLUDE_ROOT}/graphqlservice/JSONResponse.h)

src/RapidJSONResponse.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ class StreamWriter : public std::enable_shared_from_this<StreamWriter>
103103
rapidjson::Writer<rapidjson::StringBuffer> _writer;
104104
};
105105

106+
std::string toJSON(service::ResolverResult&& response)
107+
{
108+
rapidjson::StringBuffer buffer;
109+
auto writer = std::make_shared<ValueVisitor>(std::make_shared<StreamWriter>(buffer));
110+
111+
std::move(response).visit().visit(writer);
112+
113+
return stream.str();
114+
}
115+
106116
std::string toJSON(Value&& response)
107117
{
108118
rapidjson::StringBuffer buffer;

src/SchemaLoader.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ void SchemaLoader::visitObjectTypeExtension(const peg::ast_node& objectTypeExten
655655
}
656656
}
657657
}
658+
else
659+
{
660+
visitObjectTypeDefinition(objectTypeExtension);
661+
}
658662
}
659663

660664
void SchemaLoader::visitInterfaceTypeDefinition(const peg::ast_node& interfaceTypeDefinition)

src/TaoCppJSONResponse.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,19 @@ class StreamWriter : public std::enable_shared_from_this<StreamWriter>
137137
std::vector<Scope> _scopeStack;
138138
};
139139

140+
141+
142+
143+
std::string toJSON(service::ResolverResult&& response)
144+
{
145+
std::ostringstream stream;
146+
auto writer = std::make_shared<ValueVisitor>(std::make_shared<StreamWriter>(stream));
147+
148+
std::move(response).visit().visit(writer);
149+
150+
return stream.str();
151+
}
152+
140153
std::string toJSON(Value&& response)
141154
{
142155
std::ostringstream stream;

0 commit comments

Comments
 (0)