Skip to content

Commit

Permalink
Optimize uint128_t::getString() (#68)
Browse files Browse the repository at this point in the history
Shows up in memcached keyv::Map, which uses this per key, in one
benchmark as 35s vs 43s.

* Fix and CR #68
  • Loading branch information
eile committed Jan 4, 2017
1 parent 2da95ea commit 85e715b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
3 changes: 3 additions & 0 deletions doc/Changelog.md
Expand Up @@ -3,6 +3,9 @@

# git master

* [68](https://github.com/HBPVis/Servus/pull/68):
Optimize uint128_t::getString()

# Release 1.5 (09-12-2016)

* [64](https://github.com/HBPVis/Servus/pull/64):
Expand Down
3 changes: 2 additions & 1 deletion servus/types.h
@@ -1,4 +1,4 @@
/* Copyright (c) 2015-2016, Human Brain Project
/* Copyright (c) 2015-2017, Human Brain Project
* Stefan.Eilemann@epfl.ch
* Juan Hernando <jhernando@fi.upm.es>
*
Expand Down Expand Up @@ -56,6 +56,7 @@ class Servus;
class URI;
class uint128_t;

typedef unsigned long long ull_t;
typedef std::vector< std::string > Strings;
}

Expand Down
28 changes: 15 additions & 13 deletions servus/uint128_t.h
@@ -1,4 +1,4 @@
/* Copyright (c) 2010-2015, Cedric Stalder <cedric.stalder@gmail.com>
/* Copyright (c) 2010-2017, Cedric Stalder <cedric.stalder@gmail.com>
* Stefan Eilemann <eile@eyescale.ch>
* Daniel Nachbaur <danielnachbaur@gmail.com>
*
Expand All @@ -22,6 +22,7 @@
#define SERVUS_UINT128_H

#include <servus/api.h>
#include <servus/types.h>

#include <sstream>
#ifdef _MSC_VER
Expand Down Expand Up @@ -224,21 +225,22 @@ class uint128_t

/** @return a short, but not necessarily unique, string of the value. */
std::string getShortString() const
{
std::stringstream stream;
stream << std::hex << _high << _low;
const std::string str = stream.str();
return str.substr( 0, 3 ) + ".." +
str.substr( str.length() - 3, std::string::npos );
}
{
std::stringstream stream;
stream << std::hex << _high << _low;
const std::string str = stream.str();
return str.substr( 0, 3 ) + ".." +
str.substr( str.length() - 3, std::string::npos );
}

/** @return the full string representation of the value. */
std::string getString() const
{
std::stringstream stream;
stream << *this;
return stream.str();
}
{
// OPT: snprintf is faster than using std::stringstream
char buffer[ 34 ] /* 2 x 16 bytes + : + \0 */;
snprintf( buffer, 34, "%llx:%llx", ull_t( high( )), ull_t( low( )));
return std::string( buffer );
}

/** Serialize this object to a boost archive. */
template< class Archive >
Expand Down

0 comments on commit 85e715b

Please sign in to comment.