Skip to content

Commit

Permalink
Optimize uint128_t::getString()
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.
  • Loading branch information
Stefan Eilemann authored and Stefan Eilemann committed Jan 3, 2017
1 parent 2da95ea commit 92e83b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 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
31 changes: 18 additions & 13 deletions servus/uint128_t.h
@@ -1,4 +1,4 @@
/* Copyright (c) 2010-2015, Cedric Stalder <cedric.stalder@gmail.com>
/* Copyright (c) 2010-2016, Cedric Stalder <cedric.stalder@gmail.com>
* Stefan Eilemann <eile@eyescale.ch>
* Daniel Nachbaur <danielnachbaur@gmail.com>
*
Expand Down Expand Up @@ -224,21 +224,26 @@ 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 then using std::stringstream
char buffer[ 34 ] /* 16 bytes + : + \0 */;
#ifdef _MSC_VER
snprintf( buffer, 34, "%llx:%llx", high(), low( ));
#else
snprintf( buffer, 34, "%lx:%lx", high(), low( ));
#endif
return std::string( buffer );
}

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

0 comments on commit 92e83b9

Please sign in to comment.