diff --git a/CHANGELOG.md b/CHANGELOG.md index 954dcc6278..7eb545ffeb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - NodeJS: - CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452) - Misc: + - CHANGED: Use std::string_view for key type in json::Object. [#7062](https://github.com/Project-OSRM/osrm-backend/pull/7062) - CHANGED: Use thread_local instead of boost::thread_specific_ptr. [#6991](https://github.com/Project-OSRM/osrm-backend/pull/6991) - CHANGED: Bump flatbuffers to v24.3.25 version. [#6988](https://github.com/Project-OSRM/osrm-backend/pull/6988) - CHANGED: Add .reserve(...) to assembleGeometry function. [#6983](https://github.com/Project-OSRM/osrm-backend/pull/6983) diff --git a/include/nodejs/json_v8_renderer.hpp b/include/nodejs/json_v8_renderer.hpp index 9572744c2a..a6b424da69 100644 --- a/include/nodejs/json_v8_renderer.hpp +++ b/include/nodejs/json_v8_renderer.hpp @@ -30,7 +30,7 @@ struct V8Renderer { Napi::Value child; std::visit(V8Renderer(env, child), keyValue.second); - obj.Set(keyValue.first, child); + obj.Set(keyValue.first.data(), child); } out = obj; } diff --git a/include/util/json_container.hpp b/include/util/json_container.hpp index 14ca9d52f9..728b6e3e98 100644 --- a/include/util/json_container.hpp +++ b/include/util/json_container.hpp @@ -104,7 +104,7 @@ using Value = std::variant; */ struct Object { - std::unordered_map values; + std::unordered_map values; }; /** diff --git a/include/util/json_deep_compare.hpp b/include/util/json_deep_compare.hpp index 24b226ca7f..438386e796 100644 --- a/include/util/json_deep_compare.hpp +++ b/include/util/json_deep_compare.hpp @@ -44,13 +44,13 @@ struct Comparator bool operator()(const Object &lhs, const Object &rhs) const { - std::set lhs_keys; + std::set lhs_keys; for (const auto &key_value : lhs.values) { lhs_keys.insert(key_value.first); } - std::set rhs_keys; + std::set rhs_keys; for (const auto &key_value : rhs.values) { rhs_keys.insert(key_value.first); @@ -60,7 +60,7 @@ struct Comparator { if (rhs_keys.find(key) == rhs_keys.end()) { - reason = rhs_path + " doesn't have key \"" + key + "\""; + reason = rhs_path + " doesn't have key \"" + std::string(key) + "\""; return false; } } @@ -69,7 +69,7 @@ struct Comparator { if (lhs_keys.find(key) == lhs_keys.end()) { - reason = lhs_path + " doesn't have key \"" + key + "\""; + reason = lhs_path + " doesn't have key \"" + std::string(key) + "\""; return false; } } @@ -81,10 +81,11 @@ struct Comparator const auto &rhs_child = rhs.values.find(key)->second; const auto &lhs_child = lhs.values.find(key)->second; - auto is_same = - std::visit(Comparator(reason, lhs_path + "." + key, rhs_path + "." + key), - lhs_child, - rhs_child); + auto is_same = std::visit(Comparator(reason, + lhs_path + "." + std::string(key), + rhs_path + "." + std::string(key)), + lhs_child, + rhs_child); if (!is_same) { return false; diff --git a/include/util/json_renderer.hpp b/include/util/json_renderer.hpp index d1adfcce6c..bdc3dcae0c 100644 --- a/include/util/json_renderer.hpp +++ b/include/util/json_renderer.hpp @@ -97,7 +97,7 @@ template struct Renderer void operator()(const Null &) { write<>("null"); } private: - void write(const std::string &str); + void write(std::string_view str); void write(const char *str, size_t size); void write(char ch); @@ -110,7 +110,7 @@ template struct Renderer Out &out; }; -template <> void Renderer>::write(const std::string &str) +template <> void Renderer>::write(std::string_view str) { out.insert(out.end(), str.begin(), str.end()); } @@ -122,7 +122,7 @@ template <> void Renderer>::write(const char *str, size_t size template <> void Renderer>::write(char ch) { out.push_back(ch); } -template <> void Renderer::write(const std::string &str) { out << str; } +template <> void Renderer::write(std::string_view str) { out << str; } template <> void Renderer::write(const char *str, size_t size) { @@ -131,7 +131,7 @@ template <> void Renderer::write(const char *str, size_t size) template <> void Renderer::write(char ch) { out << ch; } -template <> void Renderer::write(const std::string &str) { out += str; } +template <> void Renderer::write(std::string_view str) { out += str; } template <> void Renderer::write(const char *str, size_t size) { diff --git a/scripts/ci/run_benchmarks.sh b/scripts/ci/run_benchmarks.sh index 5b092471d3..b763b83d61 100755 --- a/scripts/ci/run_benchmarks.sh +++ b/scripts/ci/run_benchmarks.sh @@ -93,7 +93,7 @@ function run_benchmarks_for_folder { echo "Took: ${DIFF}s" done done - + for ALGORITHM in ch mld; do for BENCH in nearest table trip route match; do echo "Running random $BENCH $ALGORITHM" diff --git a/src/benchmarks/json_render.cpp b/src/benchmarks/json_render.cpp index d2c00b51f0..47dea88ca4 100644 --- a/src/benchmarks/json_render.cpp +++ b/src/benchmarks/json_render.cpp @@ -9,12 +9,17 @@ #include #include #include +#include using namespace osrm; namespace { +// we use std::string_view as a key in the object, so since here we have dynamic keys we have to +// "hold" them somewhere okay for tests... +static std::unordered_set gKeysHolder; + void convert(const rapidjson::Value &value, json::Value &result) { if (value.IsString()) @@ -32,7 +37,8 @@ void convert(const rapidjson::Value &value, json::Value &result) { json::Value member; convert(itr->value, member); - object.values.emplace(itr->name.GetString(), std::move(member)); + auto keyItr = gKeysHolder.emplace(itr->name.GetString()).first; + object.values.emplace(*keyItr, std::move(member)); } result = std::move(object); } @@ -122,6 +128,7 @@ int main(int argc, char **argv) if (std::string{out_vec.begin(), out_vec.end()} != out_str || out_str != out_ss_str) { + std::cerr << "Vector/string results are not equal\n"; throw std::logic_error("Vector/stringstream/string results are not equal"); } return EXIT_SUCCESS;