You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by vi...@apache.org on 2016/04/25 23:35:58 UTC

[17/48] mesos git commit: Avoid construction of temporary strings in `NumberWriter` for doubles.

Avoid construction of temporary strings in `NumberWriter` for doubles.

With this + https://reviews.apache.org/r/43024/, the number of calls to
`operator new` and `operator delete` were reduced by roughly 1/3.

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


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

Branch: refs/heads/0.27.x
Commit: b64851f22e459232eec1f11eef15ad8f8610ccdd
Parents: 00441c1
Author: Michael Park <mp...@apache.org>
Authored: Sun Jan 31 20:37:42 2016 -0800
Committer: Joris Van Remoortere <jo...@gmail.com>
Committed: Mon Feb 15 16:15:23 2016 -0500

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/jsonify.hpp       | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/b64851f2/3rdparty/libprocess/3rdparty/stout/include/stout/jsonify.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/jsonify.hpp b/3rdparty/libprocess/3rdparty/stout/include/stout/jsonify.hpp
index f9d7224..8220d00 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/jsonify.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/jsonify.hpp
@@ -148,8 +148,8 @@ public:
         // Prints a floating point value, with the specified precision, see:
         // http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2005.pdf
         // Additionally ensures that a decimal point is in the output.
-        char buffer[50] = {}; // More than enough for the specified precision.
-        snprintf(
+        char buffer[50]; // More than enough for the specified precision.
+        const int size = snprintf(
             buffer,
             sizeof(buffer),
             "%#.*g",
@@ -158,10 +158,19 @@ public:
 
         // Get rid of excess trailing zeroes before outputting.
         // Otherwise, printing 1.0 would result in "1.00000000000000".
-        std::string trimmed = strings::trim(buffer, strings::SUFFIX, "0");
+        //
+        // NOTE: We intentionally do not use `strings::trim` here in order to
+        // avoid construction of temporary strings.
+        int back = size - 1;
+        for (; back > 0; --back) {
+          if (buffer[back] != '0') {
+            break;
+          }
+          buffer[back] = '\0';
+        }
 
         // NOTE: valid JSON numbers cannot end with a '.'.
-        *stream_ << trimmed << (trimmed.back() == '.' ? "0" : "");
+        *stream_ << buffer << (buffer[back] == '.' ? "0" : "");
         break;
       }
     }