Skip to content

Commit

Permalink
Use std::string_view for key type in json::Object (#7062)
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiFedartsou authored Nov 3, 2024
1 parent 48e8382 commit 3614af7
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion include/nodejs/json_v8_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion include/util/json_container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ using Value = std::variant<String, Number, Object, Array, True, False, Null>;
*/
struct Object
{
std::unordered_map<std::string, Value> values;
std::unordered_map<std::string_view, Value> values;
};

/**
Expand Down
17 changes: 9 additions & 8 deletions include/util/json_deep_compare.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ struct Comparator

bool operator()(const Object &lhs, const Object &rhs) const
{
std::set<std::string> lhs_keys;
std::set<std::string_view> lhs_keys;
for (const auto &key_value : lhs.values)
{
lhs_keys.insert(key_value.first);
}

std::set<std::string> rhs_keys;
std::set<std::string_view> rhs_keys;
for (const auto &key_value : rhs.values)
{
rhs_keys.insert(key_value.first);
Expand All @@ -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;
}
}
Expand All @@ -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;
}
}
Expand All @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions include/util/json_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ template <typename Out> 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);

Expand All @@ -110,7 +110,7 @@ template <typename Out> struct Renderer
Out &out;
};

template <> void Renderer<std::vector<char>>::write(const std::string &str)
template <> void Renderer<std::vector<char>>::write(std::string_view str)
{
out.insert(out.end(), str.begin(), str.end());
}
Expand All @@ -122,7 +122,7 @@ template <> void Renderer<std::vector<char>>::write(const char *str, size_t size

template <> void Renderer<std::vector<char>>::write(char ch) { out.push_back(ch); }

template <> void Renderer<std::ostream>::write(const std::string &str) { out << str; }
template <> void Renderer<std::ostream>::write(std::string_view str) { out << str; }

template <> void Renderer<std::ostream>::write(const char *str, size_t size)
{
Expand All @@ -131,7 +131,7 @@ template <> void Renderer<std::ostream>::write(const char *str, size_t size)

template <> void Renderer<std::ostream>::write(char ch) { out << ch; }

template <> void Renderer<std::string>::write(const std::string &str) { out += str; }
template <> void Renderer<std::string>::write(std::string_view str) { out += str; }

template <> void Renderer<std::string>::write(const char *str, size_t size)
{
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/run_benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
9 changes: 8 additions & 1 deletion src/benchmarks/json_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@
#include <rapidjson/document.h>
#include <sstream>
#include <stdexcept>
#include <unordered_set>

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<std::string> gKeysHolder;

void convert(const rapidjson::Value &value, json::Value &result)
{
if (value.IsString())
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3614af7

Please sign in to comment.