Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize datastream for vector of primitive types. #27

Merged
merged 1 commit into from
Aug 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions libraries/eosiolib/core/eosio/datastream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,17 +699,18 @@ datastream<Stream>& operator >> ( datastream<Stream>& ds, T (&v)[N] ) {
}

/**
* Serialize a vector of char
* Serialize a vector of T, where T is a primitive data type
*
* @param ds - The stream to write
* @param v - The value to serialize
* @tparam Stream - Type of datastream buffer
* @return datastream<Stream>& - Reference to the datastream
*/
template<typename Stream>
datastream<Stream>& operator << ( datastream<Stream>& ds, const std::vector<char>& v ) {
template<typename Stream, typename T,
std::enable_if_t<_datastream_detail::is_primitive<T>()>* = nullptr>
datastream<Stream>& operator << ( datastream<Stream>& ds, const std::vector<T>& v ) {
ds << unsigned_int( v.size() );
ds.write( v.data(), v.size() );
ds.write( (const void*)v.data(), v.size()*sizeof(T) );
return ds;
}

Expand All @@ -722,7 +723,8 @@ datastream<Stream>& operator << ( datastream<Stream>& ds, const std::vector<char
* @tparam T - Type of the object contained in the vector
* @return datastream<Stream>& - Reference to the datastream
*/
template<typename Stream, typename T>
template<typename Stream, typename T,
std::enable_if_t<!_datastream_detail::is_primitive<T>()>* = nullptr>
datastream<Stream>& operator << ( datastream<Stream>& ds, const std::vector<T>& v ) {
ds << unsigned_int( v.size() );
for( const auto& i : v )
Expand All @@ -731,19 +733,20 @@ datastream<Stream>& operator << ( datastream<Stream>& ds, const std::vector<T>&
}

/**
* Deserialize a vector of char
* Deserialize a vector of T, where T is a primitive data type
*
* @param ds - The stream to read
* @param v - The destination for deserialized value
* @tparam Stream - Type of datastream buffer
* @return datastream<Stream>& - Reference to the datastream
*/
template<typename Stream>
datastream<Stream>& operator >> ( datastream<Stream>& ds, std::vector<char>& v ) {
template<typename Stream, typename T,
std::enable_if_t<_datastream_detail::is_primitive<T>()>* = nullptr>
datastream<Stream>& operator >> ( datastream<Stream>& ds, std::vector<T>& v ) {
unsigned_int s;
ds >> s;
v.resize( s.value );
ds.read( v.data(), v.size() );
ds.read( (char*)v.data(), v.size()*sizeof(T) );
return ds;
}

Expand All @@ -756,7 +759,8 @@ datastream<Stream>& operator >> ( datastream<Stream>& ds, std::vector<char>& v )
* @tparam T - Type of the object contained in the vector
* @return datastream<Stream>& - Reference to the datastream
*/
template<typename Stream, typename T>
template<typename Stream, typename T,
std::enable_if_t<!_datastream_detail::is_primitive<T>()>* = nullptr>
datastream<Stream>& operator >> ( datastream<Stream>& ds, std::vector<T>& v ) {
unsigned_int s;
ds >> s;
Expand Down