Skip to content

Commit

Permalink
Slightly improve performance for pack/unpack
Browse files Browse the repository at this point in the history
  • Loading branch information
abitmore committed Mar 13, 2018
1 parent 86e1866 commit 43ac0b0
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 111 deletions.
30 changes: 18 additions & 12 deletions include/fc/container/flat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,64 +10,69 @@ namespace fc {
template<typename Stream, typename T>
inline void pack( Stream& s, const flat_set<T>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
pack( s, unsigned_int((uint32_t)value.size()), _max_depth - 1 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
fc::raw::pack( s, *itr, _max_depth - 1 );
fc::raw::pack( s, *itr, _max_depth );
++itr;
}
}
template<typename Stream, typename T>
inline void unpack( Stream& s, flat_set<T>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
unsigned_int size; unpack( s, size, _max_depth - 1 );
--_max_depth;
unsigned_int size; unpack( s, size, _max_depth );
value.clear();
FC_ASSERT( size.value*sizeof(T) < MAX_ARRAY_ALLOC_SIZE );
value.reserve(size.value);
for( uint32_t i = 0; i < size.value; ++i )
{
T tmp;
fc::raw::unpack( s, tmp, _max_depth - 1 );
fc::raw::unpack( s, tmp, _max_depth );
value.insert( std::move(tmp) );
}
}
template<typename Stream, typename K, typename... V>
inline void pack( Stream& s, const flat_map<K,V...>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
pack( s, unsigned_int((uint32_t)value.size()), _max_depth - 1 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
fc::raw::pack( s, *itr, _max_depth - 1 );
fc::raw::pack( s, *itr, _max_depth );
++itr;
}
}
template<typename Stream, typename K, typename V, typename... A>
inline void unpack( Stream& s, flat_map<K,V,A...>& value, uint32_t _max_depth )
{
FC_ASSERT( _max_depth > 0 );
unsigned_int size; unpack( s, size, _max_depth - 1 );
--_max_depth;
unsigned_int size; unpack( s, size, _max_depth );
value.clear();
FC_ASSERT( size.value*(sizeof(K)+sizeof(V)) < MAX_ARRAY_ALLOC_SIZE );
value.reserve(size.value);
for( uint32_t i = 0; i < size.value; ++i )
{
std::pair<K,V> tmp;
fc::raw::unpack( s, tmp, _max_depth - 1 );
fc::raw::unpack( s, tmp, _max_depth );
value.insert( std::move(tmp) );
}
}

template<typename Stream, typename T, typename A>
void pack( Stream& s, const bip::vector<T,A>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
pack( s, unsigned_int((uint32_t)value.size()), _max_depth - 1 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
if( !std::is_fundamental<T>::value ) {
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
fc::raw::pack( s, *itr, _max_depth - 1 );
fc::raw::pack( s, *itr, _max_depth );
++itr;
}
} else {
Expand All @@ -78,12 +83,13 @@ namespace fc {
template<typename Stream, typename T, typename A>
void unpack( Stream& s, bip::vector<T,A>& value, uint32_t _max_depth ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
unsigned_int size;
unpack( s, size, _max_depth - 1 );
unpack( s, size, _max_depth );
value.resize( size );
if( !std::is_fundamental<T>::value ) {
for( auto& item : value )
unpack( s, item, _max_depth - 1 );
unpack( s, item, _max_depth );
} else {
s.read( (char*)value.data(), value.size() );
}
Expand Down
10 changes: 6 additions & 4 deletions include/fc/interprocess/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,24 @@ namespace fc {
template<typename Stream, typename T, typename... A>
inline void pack( Stream& s, const bip::vector<T,A...>& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) {
FC_ASSERT( _max_depth > 0 );
pack( s, unsigned_int((uint32_t)value.size()), _max_depth - 1 );
--_max_depth;
pack( s, unsigned_int((uint32_t)value.size()), _max_depth );
auto itr = value.begin();
auto end = value.end();
while( itr != end ) {
fc::raw::pack( s, *itr, _max_depth - 1 );
fc::raw::pack( s, *itr, _max_depth );
++itr;
}
}
template<typename Stream, typename T, typename... A>
inline void unpack( Stream& s, bip::vector<T,A...>& value, uint32_t _max_depth=FC_PACK_MAX_DEPTH ) {
FC_ASSERT( _max_depth > 0 );
--_max_depth;
unsigned_int size;
unpack( s, size, _max_depth - 1 );
unpack( s, size, _max_depth );
value.clear(); value.resize(size);
for( auto& item : value )
fc::raw::unpack( s, item, _max_depth - 1 );
fc::raw::unpack( s, item, _max_depth );
}
}
}
Loading

0 comments on commit 43ac0b0

Please sign in to comment.