Commit 23123a01 authored by Ben Maurer's avatar Ben Maurer Committed by Peter Griess

Avoid copy in folly::toJson

Summary: Adding const avoids a copy constuctor.

Test Plan:
Unit tests, new benchmark:
toJson        1.83us  546.15K
toJson        1.54us  649.98K

Reviewed By: tudorb@fb.com

FB internal diff: D1071781
parent bc4b701a
...@@ -149,7 +149,7 @@ struct Printer { ...@@ -149,7 +149,7 @@ struct Printer {
} }
private: private:
void printKV(const std::pair<dynamic, dynamic>& p) const { void printKV(const std::pair<const dynamic, dynamic>& p) const {
if (!opts_.allow_non_string_keys && !p.first.isString()) { if (!opts_.allow_non_string_keys && !p.first.isString()) {
throw std::runtime_error("folly::toJson: JSON object key was not a " throw std::runtime_error("folly::toJson: JSON object key was not a "
"string"); "string");
......
...@@ -419,6 +419,16 @@ BENCHMARK(parseBigString, iters) { ...@@ -419,6 +419,16 @@ BENCHMARK(parseBigString, iters) {
} }
} }
BENCHMARK(toJson, iters) {
dynamic something = parseJson(
"{\"old_value\":40,\"changed\":true,\"opened\":false,\"foo\":[1,2,3,4,5,6]}"
);
for (int i = 0; i < iters; i++) {
toJson(something);
}
}
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true); google::ParseCommandLineFlags(&argc, &argv, true);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment