From 967945c69b4f16fc459096cbfbb8519ffc21124a Mon Sep 17 00:00:00 2001 From: Denis Vaksman Date: Fri, 15 Sep 2023 14:22:59 +0300 Subject: [PATCH] distinguished `estimate_memory_usage` and `calculate_memory_for_copying` for arrays --- runtime/array.inl | 15 +++++++++++++++ runtime/array_decl.inl | 2 ++ runtime/instance-copy-processor.cpp | 2 ++ runtime/instance-copy-processor.h | 2 +- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/runtime/array.inl b/runtime/array.inl index 15c92ed4f0..5eb7c895c5 100644 --- a/runtime/array.inl +++ b/runtime/array.inl @@ -615,6 +615,16 @@ size_t array::array_inner::estimate_memory_usage() const noexcept { return is_vector() ? sizeof_vector(buf_size) : sizeof_map(buf_size); } +template +size_t array::array_inner::calculate_memory_for_copying() const noexcept { + int64_t int_elements = size; + const bool vector_structure = is_vector(); + if (vector_structure) { + return estimate_size(int_elements, vector_structure); + } + return estimate_size(++int_elements, vector_structure); +} + template bool array::is_vector() const { return p->is_vector(); @@ -780,6 +790,11 @@ size_t array::estimate_memory_usage() const noexcept { return p->estimate_memory_usage(); } +template +size_t array::calculate_memory_for_copying() const noexcept { + return p->calculate_memory_for_copying(); +} + template typename array::const_iterator array::cbegin() const { return const_iterator::make_begin(*this); diff --git a/runtime/array_decl.inl b/runtime/array_decl.inl index 01f1ba5bff..b74921bd16 100644 --- a/runtime/array_decl.inl +++ b/runtime/array_decl.inl @@ -177,6 +177,7 @@ private: bool has_no_string_keys() const noexcept; size_t estimate_memory_usage() const noexcept; + size_t calculate_memory_for_copying() const noexcept; inline array_inner(const array_inner &other) = delete; inline array_inner &operator=(const array_inner &other) = delete; @@ -418,6 +419,7 @@ public: void reserve(int64_t int_size, bool make_vector_if_possible); size_t estimate_memory_usage() const noexcept; + size_t calculate_memory_for_copying() const noexcept; template static array convert_from(const array &); diff --git a/runtime/instance-copy-processor.cpp b/runtime/instance-copy-processor.cpp index 5ddd7c2fd6..25024adf51 100644 --- a/runtime/instance-copy-processor.cpp +++ b/runtime/instance-copy-processor.cpp @@ -20,6 +20,8 @@ bool InstanceDeepCopyVisitor::process(string &str) noexcept { return true; } + // TODO: it seems we can use `str.estimate_memory_usage(str.size())` here, + // because we don't need to take capacity into the account, only size. if (unlikely(!is_enough_memory_for(str.estimate_memory_usage()))) { str = string(); memory_limit_exceeded_ = true; diff --git a/runtime/instance-copy-processor.h b/runtime/instance-copy-processor.h index 9d5b61ea71..696e74687b 100644 --- a/runtime/instance-copy-processor.h +++ b/runtime/instance-copy-processor.h @@ -249,7 +249,7 @@ class InstanceDeepCopyVisitor : impl_::InstanceDeepBasicVisitor(); memory_limit_exceeded_ = true; return false;