From dc9e3eeb15a601713bf88b3cbfc68030a58bd855 Mon Sep 17 00:00:00 2001 From: Glebskii Nikita Date: Wed, 11 Nov 2026 11:02:39 +0300 Subject: [PATCH] perf(json): avoid quadratic copies in ToJsonList and ToJsonMap ToJsonList and ToJsonMap folded with std::accumulate over a nlohmann::json init value while the lambda took array_t/object_t by value, so every step converted the accumulated json back to array_t/object_t and deep-copied all elements serialized so far. Serializing n items was O(n^2), which made ToJson(TableMetadata) quadratic in the number of retained snapshots. Build the array/object directly instead. The output is unchanged. --- src/iceberg/util/json_util_internal.h | 42 ++++++++++++++------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/iceberg/util/json_util_internal.h b/src/iceberg/util/json_util_internal.h index f49a20e818..c6509972eb 100644 --- a/src/iceberg/util/json_util_internal.h +++ b/src/iceberg/util/json_util_internal.h @@ -230,21 +230,23 @@ Result> IntegerVectorFromJson(const nlohmann::json& json, /// Note that ToJson(const T&) is required for this function to work. template nlohmann::json::array_t ToJsonList(const std::vector& list) { - return std::accumulate(list.cbegin(), list.cend(), nlohmann::json::array(), - [](nlohmann::json::array_t arr, const T& item) { - arr.push_back(ToJson(item)); - return arr; - }); + nlohmann::json::array_t arr; + arr.reserve(list.size()); + for (const auto& item : list) { + arr.push_back(ToJson(item)); + } + return arr; } /// \brief Overload of the above function for a list of shared pointers. template nlohmann::json::array_t ToJsonList(const std::vector>& list) { - return std::accumulate(list.cbegin(), list.cend(), nlohmann::json::array(), - [](nlohmann::json::array_t arr, const std::shared_ptr& item) { - arr.push_back(ToJson(*item)); - return arr; - }); + nlohmann::json::array_t arr; + arr.reserve(list.size()); + for (const auto& item : list) { + arr.push_back(ToJson(*item)); + } + return arr; } /// \brief Parse a list of items from a JSON object. @@ -302,11 +304,11 @@ Result>> FromJsonList( /// Note that ToJson(const T&) is required for this function to work. template nlohmann::json::object_t ToJsonMap(const std::unordered_map& map) { - return std::accumulate(map.cbegin(), map.cend(), nlohmann::json::object(), - [](nlohmann::json::object_t obj, const auto& item) { - obj[item.first] = ToJson(item.second); - return obj; - }); + nlohmann::json::object_t obj; + for (const auto& [key, value] : map) { + obj[key] = ToJson(value); + } + return obj; } /// \brief Overload of the above function for a map of type & map template nlohmann::json::object_t ToJsonMap( const std::unordered_map>& map) { - return std::accumulate(map.cbegin(), map.cend(), nlohmann::json::object(), - [](nlohmann::json::object_t obj, const auto& item) { - obj[item.first] = ToJson(*item.second); - return obj; - }); + nlohmann::json::object_t obj; + for (const auto& [key, value] : map) { + obj[key] = ToJson(*value); + } + return obj; } /// \brief Parse a map of type from a JSON object.