Optimized `UUID::fromString()` and `UUID::toString()` in stout.

Rather than using `std::istringstream` and `std::ostringstream`,
instead use the facilities provided by Boost's UUID for input
and output respectively. This improves a simple benchmark that
uses `fromString()` and `toString()` by ~7x.

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


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

Branch: refs/heads/master
Commit: a853cc238757066b16090f571c54e4a114a6db69
Parents: dc002e9
Author: Neil Conway <neil.con...@gmail.com>
Authored: Thu Jul 21 13:35:17 2016 -0700
Committer: Anand Mazumdar <an...@apache.org>
Committed: Thu Jul 21 13:35:17 2016 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/uuid.hpp | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a853cc23/3rdparty/stout/include/stout/uuid.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/uuid.hpp 
b/3rdparty/stout/include/stout/uuid.hpp
index a384022..e638d45 100644
--- a/3rdparty/stout/include/stout/uuid.hpp
+++ b/3rdparty/stout/include/stout/uuid.hpp
@@ -15,10 +15,10 @@
 
 #include <assert.h>
 
-#include <sstream>
 #include <string>
 
 #include <boost/uuid/random_generator.hpp>
+#include <boost/uuid/string_generator.hpp>
 #include <boost/uuid/uuid.hpp>
 #include <boost/uuid/uuid_io.hpp>
 
@@ -71,9 +71,11 @@ public:
 
   static UUID fromString(const std::string& s)
   {
-    boost::uuids::uuid uuid;
-    std::istringstream in(s);
-    in >> uuid;
+    // NOTE: We don't use THREAD_LOCAL for the `string_generator`
+    // (unlike for the `random_generator` above), because it is cheap
+    // to construct one each time.
+    boost::uuids::string_generator gen;
+    boost::uuids::uuid uuid = gen(s);
     return UUID(uuid);
   }
 
@@ -85,9 +87,7 @@ public:
 
   std::string toString() const
   {
-    std::ostringstream out;
-    out << *this;
-    return out.str();
+    return to_string(*this);
   }
 
 private:

Reply via email to