You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2018/07/25 03:24:21 UTC

[05/10] mesos git commit: Avoid performance cost of ostringstream in http::OK json constructors.

Avoid performance cost of ostringstream in http::OK json constructors.

In the common case where no jsonp is passed this avoids an extra copy
out from the ostringstream. In the jsonp case, the performance should
be near identical or better as we now pre-reserve the space needed
for the body.

Review: https://reviews.apache.org/r/67992


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/e9d88fa5
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/e9d88fa5
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/e9d88fa5

Branch: refs/heads/master
Commit: e9d88fa5925ca0c583f467b570995cb138db787c
Parents: c866437
Author: Benjamin Mahler <bm...@apache.org>
Authored: Thu Jul 19 19:54:34 2018 -0700
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Tue Jul 24 18:33:24 2018 -0700

----------------------------------------------------------------------
 3rdparty/libprocess/src/http.cpp | 36 +++++++++++++++++------------------
 1 file changed, 17 insertions(+), 19 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e9d88fa5/3rdparty/libprocess/src/http.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/src/http.cpp b/3rdparty/libprocess/src/http.cpp
index cbb9102..e9d4392 100644
--- a/3rdparty/libprocess/src/http.cpp
+++ b/3rdparty/libprocess/src/http.cpp
@@ -690,23 +690,22 @@ OK::OK(const JSON::Value& value, const Option<string>& jsonp)
 {
   type = BODY;
 
-  std::ostringstream out;
-
   if (jsonp.isSome()) {
-    out << jsonp.get() << "(";
-  }
+    headers["Content-Type"] = "text/javascript";
 
-  out << value;
+    string stringified = stringify(value);
 
-  if (jsonp.isSome()) {
-    out << ");";
-    headers["Content-Type"] = "text/javascript";
+    body.reserve(jsonp->size() + 1 + stringified.size() + 1);
+    body += jsonp.get();
+    body += "(";
+    body += stringified;
+    body += ")";
   } else {
     headers["Content-Type"] = "application/json";
+    body = stringify(value);
   }
 
-  headers["Content-Length"] = stringify(out.str().size());
-  body = out.str();
+  headers["Content-Length"] = stringify(body.size());
 }
 
 
@@ -715,22 +714,21 @@ OK::OK(JSON::Proxy&& value, const Option<string>& jsonp)
 {
   type = BODY;
 
-  std::ostringstream out;
-
   if (jsonp.isSome()) {
-    out << jsonp.get() << "(";
-  }
+    headers["Content-Type"] = "text/javascript";
 
-  out << std::move(value);
+    string stringified = std::move(value);
 
-  if (jsonp.isSome()) {
-    out << ");";
-    headers["Content-Type"] = "text/javascript";
+    body.reserve(jsonp->size() + 1 + stringified.size() + 1);
+    body += jsonp.get();
+    body += "(";
+    body += stringified;
+    body += ")";
   } else {
     headers["Content-Type"] = "application/json";
+    body = std::move(value);
   }
 
-  body = out.str();
   headers["Content-Length"] = stringify(body.size());
 }