BLI: add clear-and-shrink method to more data structures

Also renames the existing `clear_and_make_inline` to `clear_and_shrink`
which is more concise.
This commit is contained in:
Jacques Lucke 2022-12-09 12:00:37 +01:00
parent 122d6d67e6
commit bf1791ba92
9 changed files with 51 additions and 10 deletions

View File

@ -519,7 +519,7 @@ void CurvesGeometry::ensure_evaluated_offsets() const
this->runtime->bezier_evaluated_offsets.resize(this->points_num());
}
else {
this->runtime->bezier_evaluated_offsets.clear_and_make_inline();
this->runtime->bezier_evaluated_offsets.clear_and_shrink();
}
calculate_evaluated_offsets(
@ -605,7 +605,7 @@ Span<float3> CurvesGeometry::evaluated_positions() const
this->runtime->position_cache_mutex.ensure([&]() {
if (this->is_single_type(CURVE_TYPE_POLY)) {
this->runtime->evaluated_positions_span = this->positions();
this->runtime->evaluated_position_cache.clear_and_make_inline();
this->runtime->evaluated_position_cache.clear_and_shrink();
return;
}

View File

@ -990,6 +990,15 @@ class Map {
occupied_and_removed_slots_ = 0;
}
/**
* Removes all key-value-pairs from the map and frees any allocated memory.
*/
void clear_and_shrink()
{
std::destroy_at(this);
new (this) Map(NoExceptConstructor{});
}
/**
* Get the number of collisions that the probing strategy has to go through to find the key or
* determine that it is not in the map.

View File

@ -150,6 +150,11 @@ template<typename Key, typename Value> class MultiValueMap {
{
map_.clear();
}
void clear_and_shrink()
{
map_.clear_and_shrink();
}
};
} // namespace blender

View File

@ -542,6 +542,15 @@ class Set {
occupied_and_removed_slots_ = 0;
}
/**
* Removes all keys from the set and frees any allocated memory.
*/
void clear_and_shrink()
{
std::destroy_at(this);
new (this) Set(NoExceptConstructor{});
}
/**
* Creates a new slot array and reinserts all keys inside of that. This method can be used to get
* rid of removed slots. Also this is useful for benchmarking the grow function.

View File

@ -329,6 +329,15 @@ class Stack {
top_ = top_chunk_->begin;
}
/**
* Removes all elements from the stack and frees any allocated memory.
*/
void clear_and_shrink()
{
std::destroy_at(this);
new (this) Stack(NoExceptConstructor{});
}
/* This should only be called by unit tests. */
bool is_invariant_maintained() const
{

View File

@ -410,7 +410,7 @@ class Vector {
* Afterwards the vector has 0 elements and any allocated memory
* will be freed.
*/
void clear_and_make_inline()
void clear_and_shrink()
{
destruct_n(begin_, this->size());
if (!this->is_inline()) {

View File

@ -560,6 +560,15 @@ class VectorSet {
occupied_and_removed_slots_ = 0;
}
/**
* Removes all keys from the set and frees any allocated memory.
*/
void clear_and_shrink()
{
std::destroy_at(this);
new (this) VectorSet(NoExceptConstructor{});
}
/**
* Get the number of collisions that the probing strategy has to go through to find the key or
* determine that it is not in the set.

View File

@ -266,7 +266,7 @@ static void opencl_initialize(const bool use_opencl)
static void opencl_deinitialize()
{
g_work_scheduler.opencl.devices.clear_and_make_inline();
g_work_scheduler.opencl.devices.clear_and_shrink();
if (g_work_scheduler.opencl.program) {
clReleaseProgram(g_work_scheduler.opencl.program);
@ -364,7 +364,7 @@ static void threading_model_queue_deinitialize()
{
/* deinitialize CPU threads */
if (g_work_scheduler.queue.initialized) {
g_work_scheduler.queue.devices.clear_and_make_inline();
g_work_scheduler.queue.devices.clear_and_shrink();
BLI_thread_local_delete(g_thread_device);
g_work_scheduler.queue.initialized = false;

View File

@ -82,11 +82,11 @@ void OBJMesh::clear()
owned_export_mesh_ = nullptr;
}
export_mesh_ = nullptr;
uv_indices_.clear_and_make_inline();
uv_coords_.clear_and_make_inline();
loop_to_normal_index_.clear_and_make_inline();
normal_coords_.clear_and_make_inline();
poly_order_.clear_and_make_inline();
uv_indices_.clear_and_shrink();
uv_coords_.clear_and_shrink();
loop_to_normal_index_.clear_and_shrink();
normal_coords_.clear_and_shrink();
poly_order_.clear_and_shrink();
if (poly_smooth_groups_) {
MEM_freeN(poly_smooth_groups_);
poly_smooth_groups_ = nullptr;