From 23123a0166bd33ce56d1cbea778bdfacf746e7d7 Mon Sep 17 00:00:00 2001 From: Ben Maurer <bmaurer@fb.com> Date: Mon, 25 Nov 2013 11:28:40 -0800 Subject: [PATCH] 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 --- folly/json.cpp | 2 +- folly/test/JsonTest.cpp | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/folly/json.cpp b/folly/json.cpp index 7711a203..5fe44a59 100644 --- a/folly/json.cpp +++ b/folly/json.cpp @@ -149,7 +149,7 @@ struct Printer { } 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()) { throw std::runtime_error("folly::toJson: JSON object key was not a " "string"); diff --git a/folly/test/JsonTest.cpp b/folly/test/JsonTest.cpp index 6ab97686..cfa84f4c 100644 --- a/folly/test/JsonTest.cpp +++ b/folly/test/JsonTest.cpp @@ -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) { testing::InitGoogleTest(&argc, argv); google::ParseCommandLineFlags(&argc, &argv, true); -- 2.34.1