Skip to content

Commit 3614af7

Browse files
Use std::string_view for key type in json::Object (#7062)
1 parent 48e8382 commit 3614af7

File tree

7 files changed

+25
-16
lines changed

7 files changed

+25
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- NodeJS:
2626
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
2727
- Misc:
28+
- CHANGED: Use std::string_view for key type in json::Object. [#7062](https://github.com/Project-OSRM/osrm-backend/pull/7062)
2829
- CHANGED: Use thread_local instead of boost::thread_specific_ptr. [#6991](https://github.com/Project-OSRM/osrm-backend/pull/6991)
2930
- CHANGED: Bump flatbuffers to v24.3.25 version. [#6988](https://github.com/Project-OSRM/osrm-backend/pull/6988)
3031
- CHANGED: Add .reserve(...) to assembleGeometry function. [#6983](https://github.com/Project-OSRM/osrm-backend/pull/6983)

include/nodejs/json_v8_renderer.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct V8Renderer
3030
{
3131
Napi::Value child;
3232
std::visit(V8Renderer(env, child), keyValue.second);
33-
obj.Set(keyValue.first, child);
33+
obj.Set(keyValue.first.data(), child);
3434
}
3535
out = obj;
3636
}

include/util/json_container.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ using Value = std::variant<String, Number, Object, Array, True, False, Null>;
104104
*/
105105
struct Object
106106
{
107-
std::unordered_map<std::string, Value> values;
107+
std::unordered_map<std::string_view, Value> values;
108108
};
109109

110110
/**

include/util/json_deep_compare.hpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ struct Comparator
4444

4545
bool operator()(const Object &lhs, const Object &rhs) const
4646
{
47-
std::set<std::string> lhs_keys;
47+
std::set<std::string_view> lhs_keys;
4848
for (const auto &key_value : lhs.values)
4949
{
5050
lhs_keys.insert(key_value.first);
5151
}
5252

53-
std::set<std::string> rhs_keys;
53+
std::set<std::string_view> rhs_keys;
5454
for (const auto &key_value : rhs.values)
5555
{
5656
rhs_keys.insert(key_value.first);
@@ -60,7 +60,7 @@ struct Comparator
6060
{
6161
if (rhs_keys.find(key) == rhs_keys.end())
6262
{
63-
reason = rhs_path + " doesn't have key \"" + key + "\"";
63+
reason = rhs_path + " doesn't have key \"" + std::string(key) + "\"";
6464
return false;
6565
}
6666
}
@@ -69,7 +69,7 @@ struct Comparator
6969
{
7070
if (lhs_keys.find(key) == lhs_keys.end())
7171
{
72-
reason = lhs_path + " doesn't have key \"" + key + "\"";
72+
reason = lhs_path + " doesn't have key \"" + std::string(key) + "\"";
7373
return false;
7474
}
7575
}
@@ -81,10 +81,11 @@ struct Comparator
8181

8282
const auto &rhs_child = rhs.values.find(key)->second;
8383
const auto &lhs_child = lhs.values.find(key)->second;
84-
auto is_same =
85-
std::visit(Comparator(reason, lhs_path + "." + key, rhs_path + "." + key),
86-
lhs_child,
87-
rhs_child);
84+
auto is_same = std::visit(Comparator(reason,
85+
lhs_path + "." + std::string(key),
86+
rhs_path + "." + std::string(key)),
87+
lhs_child,
88+
rhs_child);
8889
if (!is_same)
8990
{
9091
return false;

include/util/json_renderer.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ template <typename Out> struct Renderer
9797
void operator()(const Null &) { write<>("null"); }
9898

9999
private:
100-
void write(const std::string &str);
100+
void write(std::string_view str);
101101
void write(const char *str, size_t size);
102102
void write(char ch);
103103

@@ -110,7 +110,7 @@ template <typename Out> struct Renderer
110110
Out &out;
111111
};
112112

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

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

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

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

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

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

136136
template <> void Renderer<std::string>::write(const char *str, size_t size)
137137
{

scripts/ci/run_benchmarks.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function run_benchmarks_for_folder {
9393
echo "Took: ${DIFF}s"
9494
done
9595
done
96-
96+
9797
for ALGORITHM in ch mld; do
9898
for BENCH in nearest table trip route match; do
9999
echo "Running random $BENCH $ALGORITHM"

src/benchmarks/json_render.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,17 @@
99
#include <rapidjson/document.h>
1010
#include <sstream>
1111
#include <stdexcept>
12+
#include <unordered_set>
1213

1314
using namespace osrm;
1415

1516
namespace
1617
{
1718

19+
// we use std::string_view as a key in the object, so since here we have dynamic keys we have to
20+
// "hold" them somewhere okay for tests...
21+
static std::unordered_set<std::string> gKeysHolder;
22+
1823
void convert(const rapidjson::Value &value, json::Value &result)
1924
{
2025
if (value.IsString())
@@ -32,7 +37,8 @@ void convert(const rapidjson::Value &value, json::Value &result)
3237
{
3338
json::Value member;
3439
convert(itr->value, member);
35-
object.values.emplace(itr->name.GetString(), std::move(member));
40+
auto keyItr = gKeysHolder.emplace(itr->name.GetString()).first;
41+
object.values.emplace(*keyItr, std::move(member));
3642
}
3743
result = std::move(object);
3844
}
@@ -122,6 +128,7 @@ int main(int argc, char **argv)
122128

123129
if (std::string{out_vec.begin(), out_vec.end()} != out_str || out_str != out_ss_str)
124130
{
131+
std::cerr << "Vector/string results are not equal\n";
125132
throw std::logic_error("Vector/stringstream/string results are not equal");
126133
}
127134
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)