From 8cd56d8c0889352e410675376534f53961b499b0 Mon Sep 17 00:00:00 2001 From: ltrk2 <107155950+ltrk2@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:14:49 -0700 Subject: [PATCH] Improve existing serialization implementations --- .../SerializationDecimalBase.cpp | 37 ++++++------------- .../Serializations/SerializationNumber.cpp | 9 +---- 2 files changed, 13 insertions(+), 33 deletions(-) diff --git a/src/DataTypes/Serializations/SerializationDecimalBase.cpp b/src/DataTypes/Serializations/SerializationDecimalBase.cpp index 494d0aa9168..37edfffc0d4 100644 --- a/src/DataTypes/Serializations/SerializationDecimalBase.cpp +++ b/src/DataTypes/Serializations/SerializationDecimalBase.cpp @@ -7,6 +7,7 @@ #include #include +#include namespace DB { @@ -29,21 +30,13 @@ template void SerializationDecimalBase::serializeBinaryBulk(const IColumn & column, WriteBuffer & ostr, size_t offset, size_t limit) const { const typename ColumnType::Container & x = typeid_cast(column).getData(); - - size_t size = x.size(); - - if (limit == 0 || offset + limit > size) + if (const size_t size = x.size(); limit == 0 || offset + limit > size) limit = size - offset; - if constexpr (std::endian::native == std::endian::big && sizeof(T) >= 2) + + if constexpr (std::endian::native == std::endian::big) { - for (size_t i = 0; i < limit; i++) - { - auto tmp(x[offset+i]); - char *start = reinterpret_cast(&tmp); - char *end = start + sizeof(FieldType); - std::reverse(start, end); - ostr.write(reinterpret_cast(&tmp), sizeof(FieldType)); - } + std::ranges::for_each( + x | std::views::drop(offset) | std::views::take(limit), [&ostr](const auto & d) { writeBinaryLittleEndian(d, ostr); }); } else ostr.write(reinterpret_cast(&x[offset]), sizeof(FieldType) * limit); @@ -69,20 +62,14 @@ template void SerializationDecimalBase::deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, size_t limit, double) const { typename ColumnType::Container & x = typeid_cast(column).getData(); - size_t initial_size = x.size(); + const size_t initial_size = x.size(); x.resize(initial_size + limit); - size_t size = istr.readBig(reinterpret_cast(&x[initial_size]), sizeof(FieldType) * limit); - if constexpr (std::endian::native == std::endian::big && sizeof(T) >= 2) - { - for (size_t i = 0; i < limit; i++) - { - char *start = reinterpret_cast(&x[initial_size + i]); - char *end = start + sizeof(FieldType); - std::reverse(start, end); - } - } - + const size_t size = istr.readBig(reinterpret_cast(&x[initial_size]), sizeof(FieldType) * limit); x.resize(initial_size + size / sizeof(FieldType)); + + if constexpr (std::endian::native == std::endian::big) + std::ranges::for_each( + x | std::views::drop(initial_size), [](auto & d) { transformEndianness(d); }); } template class SerializationDecimalBase; diff --git a/src/DataTypes/Serializations/SerializationNumber.cpp b/src/DataTypes/Serializations/SerializationNumber.cpp index df6c0848bbe..fc3a5f0db24 100644 --- a/src/DataTypes/Serializations/SerializationNumber.cpp +++ b/src/DataTypes/Serializations/SerializationNumber.cpp @@ -145,15 +145,8 @@ void SerializationNumber::serializeBinaryBulk(const IColumn & column, WriteBu if constexpr (std::endian::native == std::endian::big && sizeof(T) >= 2) { - static constexpr auto to_little_endian = [](auto i) - { - transformEndianness(i); - return i; - }; - std::ranges::for_each( - x | std::views::drop(offset) | std::views::take(limit) | std::views::transform(to_little_endian), - [&ostr](const auto & i) { ostr.write(reinterpret_cast(&i), sizeof(typename ColumnVector::ValueType)); }); + x | std::views::drop(offset) | std::views::take(limit), [&ostr](const auto & i) { writeBinaryLittleEndian(i, ostr); }); } else ostr.write(reinterpret_cast(&x[offset]), sizeof(typename ColumnVector::ValueType) * limit);