From b9957bb711c6987e361be6d8b74c70d1759a4f23 Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Fri, 30 Sep 2022 15:36:19 +0200 Subject: [PATCH] Get rid of copy --- include/util/json_renderer.hpp | 22 +++++++-------- include/util/string_util.hpp | 2 +- src/benchmarks/json_render.cpp | 49 ---------------------------------- 3 files changed, 12 insertions(+), 61 deletions(-) diff --git a/include/util/json_renderer.hpp b/include/util/json_renderer.hpp index 8081eec53..b17b9bb19 100644 --- a/include/util/json_renderer.hpp +++ b/include/util/json_renderer.hpp @@ -35,10 +35,13 @@ template struct Renderer { write('"'); auto size = SizeOfEscapedJSONString(string.value); - if (size == string.value.size()) { + if (size == string.value.size()) + { // we don't need to escape anything write(string.value); - } else { + } + else + { std::string escaped; escaped.reserve(size); EscapeJSONString(string.value, escaped); @@ -134,37 +137,34 @@ template <> void Renderer>::write(const char *str) } template <> void Renderer>::write(char ch) { out.push_back(ch); } - template <> void Renderer::write(const std::string &str) { out << str; } template <> void Renderer::write(const char *str) { out << str; } template <> void Renderer::write(char ch) { out << ch; } - template <> void Renderer::write(const std::string &str) { out += str; } template <> void Renderer::write(const char *str) { out += str; } template <> void Renderer::write(char ch) { out += ch; } - inline void render(std::ostream &out, const Object &object) { - Value value = object; - mapbox::util::apply_visitor(Renderer(out), value); + Renderer renderer(out); + renderer(object); } inline void render(std::string &out, const Object &object) { - Value value = object; - mapbox::util::apply_visitor(Renderer(out), value); + Renderer renderer(out); + renderer(object); } inline void render(std::vector &out, const Object &object) { - Value value = object; - mapbox::util::apply_visitor(Renderer(out), value); + Renderer renderer(out); + renderer(object); } } // namespace json diff --git a/include/util/string_util.hpp b/include/util/string_util.hpp index d07753b9c..477ea55e4 100644 --- a/include/util/string_util.hpp +++ b/include/util/string_util.hpp @@ -82,7 +82,7 @@ inline size_t SizeOfEscapedJSONString(const std::string &string) return size; } -inline void EscapeJSONString(const std::string &input, std::string& output) +inline void EscapeJSONString(const std::string &input, std::string &output) { for (const char letter : input) { diff --git a/src/benchmarks/json_render.cpp b/src/benchmarks/json_render.cpp index d76d9e54b..908d54a92 100644 --- a/src/benchmarks/json_render.cpp +++ b/src/benchmarks/json_render.cpp @@ -8,50 +8,6 @@ #include #include -// #ifdef _WIN32 -// #pragma optimize("", off) -// template void dont_optimize_away(T &&datum) { T local = datum; } -// #pragma optimize("", on) -// #else -// template void dont_optimize_away(T &&datum) { asm volatile("" : "+r"(datum)); } -// #endif - -// template -// auto measure_random_access() -// { -// std::vector indices(num_entries); -// std::iota(indices.begin(), indices.end(), 0); -// std::mt19937 g(1337); -// std::shuffle(indices.begin(), indices.end(), g); - -// VectorT vector(num_entries); - -// TIMER_START(write); -// for (auto round : util::irange(0, num_rounds)) -// { -// for (auto idx : util::irange(0, num_entries)) -// { -// vector[indices[idx]] = idx + round; -// } -// } -// TIMER_STOP(write); - -// TIMER_START(read); -// auto sum = 0; -// for (auto round : util::irange(0, num_rounds)) -// { -// sum = round; -// for (auto idx : util::irange(0, num_entries)) -// { -// sum += vector[indices[idx]]; -// } -// dont_optimize_away(sum); -// } -// TIMER_STOP(read); - -// return Measurement{TIMER_MSEC(write), TIMER_MSEC(read)}; -// } - int main(int, char **) { using namespace osrm; @@ -150,10 +106,5 @@ int main(int, char **) 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 and string are not equal"); - } - // (void)s; - // std::cerr << ss << "\n"; return EXIT_SUCCESS; }