Skip to content

Commit

Permalink
distinguished estimate_memory_usage and `calculate_memory_for_copyi…
Browse files Browse the repository at this point in the history
…ng` for arrays
  • Loading branch information
DrDet committed Sep 15, 2023
1 parent 4fd655f commit 967945c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
15 changes: 15 additions & 0 deletions runtime/array.inl
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,16 @@ size_t array<T>::array_inner::estimate_memory_usage() const noexcept {
return is_vector() ? sizeof_vector(buf_size) : sizeof_map(buf_size);
}

template<class T>
size_t array<T>::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<class T>
bool array<T>::is_vector() const {
return p->is_vector();
Expand Down Expand Up @@ -780,6 +790,11 @@ size_t array<T>::estimate_memory_usage() const noexcept {
return p->estimate_memory_usage();
}

template<class T>
size_t array<T>::calculate_memory_for_copying() const noexcept {
return p->calculate_memory_for_copying();
}

template<class T>
typename array<T>::const_iterator array<T>::cbegin() const {
return const_iterator::make_begin(*this);
Expand Down
2 changes: 2 additions & 0 deletions runtime/array_decl.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<typename U>
static array<T> convert_from(const array<U> &);
Expand Down
2 changes: 2 additions & 0 deletions runtime/instance-copy-processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion runtime/instance-copy-processor.h
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class InstanceDeepCopyVisitor : impl_::InstanceDeepBasicVisitor<InstanceDeepCopy
if (arr.is_reference_counter(ExtraRefCnt::for_global_const)) {
return true;
}
if (unlikely(!is_enough_memory_for(arr.estimate_memory_usage()))) {
if (unlikely(!is_enough_memory_for(arr.calculate_memory_for_copying()))) {
arr = array<T>();
memory_limit_exceeded_ = true;
return false;
Expand Down

0 comments on commit 967945c

Please sign in to comment.