From e80920ecd19ed6601fb9e6126a824f41dac8e722 Mon Sep 17 00:00:00 2001 From: alesapin Date: Wed, 27 Nov 2024 19:11:21 +0100 Subject: [PATCH] Devirtualize serialize call in AggregateFunction --- src/AggregateFunctions/IAggregateFunction.h | 8 ++++++++ .../Serializations/SerializationAggregateFunction.cpp | 9 ++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/AggregateFunctions/IAggregateFunction.h b/src/AggregateFunctions/IAggregateFunction.h index bd1a8f876a4..66330465a39 100644 --- a/src/AggregateFunctions/IAggregateFunction.h +++ b/src/AggregateFunctions/IAggregateFunction.h @@ -176,6 +176,8 @@ public: /// Serializes state (to transmit it over the network, for example). virtual void serialize(ConstAggregateDataPtr __restrict place, WriteBuffer & buf, std::optional version = std::nullopt) const = 0; /// NOLINT + virtual void serializeBatch(const PaddedPODArray & data, size_t start, size_t size, WriteBuffer & buf, std::optional version = std::nullopt) const = 0; /// NOLINT + /// Deserializes state. This function is called only for empty (just created) states. virtual void deserialize(AggregateDataPtr __restrict place, ReadBuffer & buf, std::optional version = std::nullopt, Arena * arena = nullptr) const = 0; /// NOLINT @@ -471,6 +473,12 @@ public: } } + void serializeBatch(const PaddedPODArray & data, size_t start, size_t size, WriteBuffer & buf, std::optional version) const override // NOLINT + { + for (size_t i = start; i < size; ++i) + static_cast(this)->serialize(data[i], buf, version); + } + void addBatchSparse( size_t row_begin, size_t row_end, diff --git a/src/DataTypes/Serializations/SerializationAggregateFunction.cpp b/src/DataTypes/Serializations/SerializationAggregateFunction.cpp index 41b198890e4..5712f1b3de3 100644 --- a/src/DataTypes/Serializations/SerializationAggregateFunction.cpp +++ b/src/DataTypes/Serializations/SerializationAggregateFunction.cpp @@ -60,12 +60,11 @@ void SerializationAggregateFunction::serializeBinaryBulk(const IColumn & column, const ColumnAggregateFunction & real_column = typeid_cast(column); const ColumnAggregateFunction::Container & vec = real_column.getData(); - ColumnAggregateFunction::Container::const_iterator it = vec.begin() + offset; - ColumnAggregateFunction::Container::const_iterator end = limit ? it + limit : vec.end(); + size_t end = vec.size(); + if (limit) + end = std::min(end, limit); - end = std::min(end, vec.end()); - for (; it != end; ++it) - function->serialize(*it, ostr, version); + function->serializeBatch(vec, offset, end, ostr, version); } void SerializationAggregateFunction::deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, size_t limit, double /*avg_value_size_hint*/) const