Improve performance of JSON rendering (#6380)
This commit is contained in:
committed by
GitHub
parent
41fd947ebd
commit
9a4b4648f4
@@ -30,6 +30,18 @@ target_link_libraries(match-bench
|
||||
${TBB_LIBRARIES}
|
||||
${MAYBE_SHAPEFILE})
|
||||
|
||||
add_executable(json-render-bench
|
||||
EXCLUDE_FROM_ALL
|
||||
json_render.cpp
|
||||
$<TARGET_OBJECTS:UTIL>)
|
||||
|
||||
target_link_libraries(json-render-bench
|
||||
osrm
|
||||
${BOOST_BASE_LIBRARIES}
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
${TBB_LIBRARIES}
|
||||
${MAYBE_SHAPEFILE})
|
||||
|
||||
add_executable(alias-bench
|
||||
EXCLUDE_FROM_ALL
|
||||
${AliasBenchmarkSources}
|
||||
@@ -41,6 +53,8 @@ target_link_libraries(alias-bench
|
||||
${TBB_LIBRARIES}
|
||||
${MAYBE_SHAPEFILE})
|
||||
|
||||
|
||||
|
||||
add_executable(packedvector-bench
|
||||
EXCLUDE_FROM_ALL
|
||||
${PackedVectorBenchmarkSources}
|
||||
@@ -58,4 +72,5 @@ add_custom_target(benchmarks
|
||||
rtree-bench
|
||||
packedvector-bench
|
||||
match-bench
|
||||
json-render-bench
|
||||
alias-bench)
|
||||
|
||||
@@ -0,0 +1,128 @@
|
||||
|
||||
#include "osrm/json_container.hpp"
|
||||
#include "util/json_container.hpp"
|
||||
#include "util/json_renderer.hpp"
|
||||
#include "util/timing_util.hpp"
|
||||
#include <cstdlib>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <rapidjson/document.h>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace osrm;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void convert(const rapidjson::Value &value, json::Value &result)
|
||||
{
|
||||
if (value.IsString())
|
||||
{
|
||||
result = json::String{value.GetString()};
|
||||
}
|
||||
else if (value.IsNumber())
|
||||
{
|
||||
result = json::Number{value.GetDouble()};
|
||||
}
|
||||
else if (value.IsObject())
|
||||
{
|
||||
json::Object object;
|
||||
for (auto itr = value.MemberBegin(); itr != value.MemberEnd(); ++itr)
|
||||
{
|
||||
json::Value member;
|
||||
convert(itr->value, member);
|
||||
object.values.emplace(itr->name.GetString(), std::move(member));
|
||||
}
|
||||
result = std::move(object);
|
||||
}
|
||||
else if (value.IsArray())
|
||||
{
|
||||
json::Array array;
|
||||
for (auto itr = value.Begin(); itr != value.End(); ++itr)
|
||||
{
|
||||
json::Value member;
|
||||
convert(*itr, member);
|
||||
array.values.push_back(std::move(member));
|
||||
}
|
||||
result = std::move(array);
|
||||
}
|
||||
else if (value.IsBool())
|
||||
{
|
||||
if (value.GetBool())
|
||||
{
|
||||
result = json::True{};
|
||||
}
|
||||
else
|
||||
{
|
||||
result = json::False{};
|
||||
}
|
||||
}
|
||||
else if (value.IsNull())
|
||||
{
|
||||
result = json::Null{};
|
||||
}
|
||||
else
|
||||
{
|
||||
throw std::runtime_error("unknown type");
|
||||
}
|
||||
}
|
||||
|
||||
json::Object load(const char *filename)
|
||||
{
|
||||
// load file to std string
|
||||
std::ifstream file(filename);
|
||||
std::stringstream buffer;
|
||||
buffer << file.rdbuf();
|
||||
std::string json = buffer.str();
|
||||
|
||||
// load rapidjson document
|
||||
rapidjson::Document document;
|
||||
document.Parse(json.c_str());
|
||||
if (document.HasParseError())
|
||||
{
|
||||
throw std::runtime_error("Failed to parse JSON");
|
||||
}
|
||||
|
||||
json::Value result;
|
||||
convert(document, result);
|
||||
return result.get<json::Object>();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
if (argc < 2)
|
||||
{
|
||||
std::cerr << "Usage: " << argv[0] << " file.json\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
const auto obj = load(argv[1]);
|
||||
|
||||
TIMER_START(string);
|
||||
std::string out_str;
|
||||
json::render(out_str, obj);
|
||||
TIMER_STOP(string);
|
||||
std::cout << "String: " << TIMER_MSEC(string) << "ms" << std::endl;
|
||||
|
||||
TIMER_START(stringstream);
|
||||
std::stringstream ss;
|
||||
json::render(ss, obj);
|
||||
std::string out_ss_str{ss.str()};
|
||||
TIMER_STOP(stringstream);
|
||||
|
||||
std::cout << "Stringstream: " << TIMER_MSEC(stringstream) << "ms" << std::endl;
|
||||
TIMER_START(vector);
|
||||
std::vector<char> out_vec;
|
||||
json::render(out_vec, obj);
|
||||
TIMER_STOP(vector);
|
||||
std::cout << "Vector: " << TIMER_MSEC(vector) << "ms" << std::endl;
|
||||
|
||||
if (std::string{out_vec.begin(), out_vec.end()} != out_str || out_str != out_ss_str)
|
||||
{
|
||||
throw std::logic_error("Vector/stringstream/string results are not equal");
|
||||
}
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -167,13 +167,13 @@ inline void async(const Nan::FunctionCallbackInfo<v8::Value> &info,
|
||||
ParseResult(status, json_result);
|
||||
if (pluginParams.renderToBuffer)
|
||||
{
|
||||
std::ostringstream buf;
|
||||
osrm::util::json::render(buf, json_result);
|
||||
result = buf.str();
|
||||
std::string json_string;
|
||||
osrm::util::json::render(json_string, json_result);
|
||||
result = std::move(json_string);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = json_result;
|
||||
result = std::move(json_result);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user