store and restore individual elements of vectors if the data is not densely packed in the vector

This commit is contained in:
Max Lyon
2021-03-02 02:11:18 +01:00
parent c1f3a4d3d1
commit 657128e6d4
2 changed files with 41 additions and 16 deletions

View File

@@ -347,12 +347,12 @@ struct binary< std::vector< T > > {
for(auto v : _v) for(auto v : _v)
{ {
size += binary<T>::size_of(v); size += binary<T>::size_of(v);
}
if(_store_size) if(_store_size)
{ {
unsigned int N = _v.size(); unsigned int N = _v.size();
size += binary<unsigned int>::size_of(); size += binary<unsigned int>::size_of();
} }
}
return size; return size;
} }
@@ -370,12 +370,25 @@ struct binary< std::vector< T > > {
if (_swap) if (_swap)
bytes += std::accumulate( _v.begin(), _v.end(), static_cast<size_t>(0), bytes += std::accumulate( _v.begin(), _v.end(), static_cast<size_t>(0),
FunctorStore<elem_type>(_os,_swap) ); FunctorStore<elem_type>(_os,_swap) );
else { else
{
auto elem_size = binary<elem_type>::size_of();
if (elem_size != IO::UnknownSize && elem_size == sizeof(elem_type))
{
// size of all elements is known, equal, and densely packed in vector.
// Just store vector data
auto bytes_of_vec = size_of(_v, false); auto bytes_of_vec = size_of(_v, false);
bytes += bytes_of_vec; bytes += bytes_of_vec;
if (_v.size() > 0) if (_v.size() > 0)
_os.write( reinterpret_cast<const char*>(&_v[0]), bytes_of_vec); _os.write( reinterpret_cast<const char*>(&_v[0]), bytes_of_vec);
} }
else
{
// store individual elements
for (const auto& v : _v)
bytes += binary<elem_type>::store(_os, v, _swap);
}
}
return _os.good() ? bytes : 0; return _os.good() ? bytes : 0;
} }
@@ -395,11 +408,23 @@ struct binary< std::vector< T > > {
FunctorRestore<elem_type>(_is, _swap) ); FunctorRestore<elem_type>(_is, _swap) );
else else
{ {
auto elem_size = binary<elem_type>::size_of();
if (elem_size != IO::UnknownSize && elem_size == sizeof(elem_type))
{
// size of all elements is known, equal, and densely packed in vector.
// Just restore vector data
auto bytes_of_vec = size_of(_v, false); auto bytes_of_vec = size_of(_v, false);
bytes += bytes_of_vec; bytes += bytes_of_vec;
if (_v.size() > 0) if (_v.size() > 0)
_is.read( reinterpret_cast<char*>(&_v[0]), bytes_of_vec ); _is.read( reinterpret_cast<char*>(&_v[0]), bytes_of_vec );
} }
else
{
// restore individual elements
for (auto& v : _v)
bytes += binary<elem_type>::restore(_is, v, _swap);
}
}
return _is.good() ? bytes : 0; return _is.good() ? bytes : 0;
} }
}; };