From: Ben Maurer Date: Mon, 25 Nov 2013 19:28:40 +0000 (-0800) Subject: Avoid copy in folly::toJson X-Git-Tag: v0.22.0~794 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=23123a0166bd33ce56d1cbea778bdfacf746e7d7;p=folly.git 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 --- 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& p) const { + void printKV(const std::pair& 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);