浏览代码

Un-support `force_trivial` parameter for `LocalVector`. Instead, users are reformatted to use `resize_uninitialized` to make it explicit that the resize does not initialize missing elements.

Lukas Tenbrink 2 月之前
父节点
当前提交
37415530d7
共有 5 个文件被更改,包括 20 次插入17 次删除
  1. 1 1
      core/math/bvh.h
  2. 1 1
      core/math/bvh_public.inc
  3. 2 2
      core/math/bvh_structs.inc
  4. 7 8
      core/templates/local_vector.h
  5. 9 5
      core/templates/pooled_list.h

+ 1 - 1
core/math/bvh.h

@@ -769,7 +769,7 @@ private:
 
 	// for collision pairing,
 	// maintain a list of all items moved etc on each frame / tick
-	LocalVector<BVHHandle, uint32_t, true> changed_items;
+	LocalVector<BVHHandle> changed_items;
 	uint32_t _tick = 1; // Start from 1 so items with 0 indicate never updated.
 
 	class BVHLockedFunction {

+ 1 - 1
core/math/bvh_public.inc

@@ -202,7 +202,7 @@ void item_remove(BVHHandle p_handle) {
 
 	// swap back and decrement for fast unordered remove
 	_active_refs[active_ref_id] = ref_id_moved_back;
-	_active_refs.resize(_active_refs.size() - 1);
+	_active_refs.resize_uninitialized(_active_refs.size() - 1);
 
 	// keep the moved active reference up to date
 	_extra[ref_id_moved_back].active_ref_id = active_ref_id;

+ 2 - 2
core/math/bvh_structs.inc

@@ -172,13 +172,13 @@ PooledList<TLeaf, uint32_t, true> _leaves;
 // we can maintain an un-ordered list of which references are active,
 // in order to do a slow incremental optimize of the tree over each frame.
 // This will work best if dynamic objects and static objects are in a different tree.
-LocalVector<uint32_t, uint32_t, true> _active_refs;
+LocalVector<uint32_t> _active_refs;
 uint32_t _current_active_ref = 0;
 
 // instead of translating directly to the userdata output,
 // we keep an intermediate list of hits as reference IDs, which can be used
 // for pairing collision detection
-LocalVector<uint32_t, uint32_t, true> _cull_hits;
+LocalVector<uint32_t> _cull_hits;
 
 // We can now have a user definable number of trees.
 // This allows using e.g. a non-pairable and pairable tree,

+ 7 - 8
core/templates/local_vector.h

@@ -40,10 +40,10 @@
 
 // If tight, it grows strictly as much as needed.
 // Otherwise, it grows exponentially (the default and what you want in most cases).
-// force_trivial is used to avoid T's default value on resize, for improved performance.
-// This requires T to be trivially destructible.
 template <typename T, typename U = uint32_t, bool force_trivial = false, bool tight = false>
 class LocalVector {
+	static_assert(!force_trivial, "force_trivial is no longer supported. Use resize_uninitialized instead.");
+
 private:
 	U count = 0;
 	U capacity = 0;
@@ -182,14 +182,13 @@ public:
 
 	/// Resize the vector.
 	/// Elements are initialized (or not) depending on what the default C++ behavior for T is.
-	/// Note: If force_trivial is set, this will behave like resize_trivial instead.
+	/// Note: If force_trivial is set, this will behave like resize_uninitialized instead.
 	void resize(U p_size) {
-		// Don't init when trivially constructible, or force_trivial is set.
-		_resize<!force_trivial && !std::is_trivially_constructible_v<T>>(p_size);
+		// Don't init when trivially constructible.
+		_resize<!std::is_trivially_constructible_v<T>>(p_size);
 	}
 
 	/// Resize and set all values to 0 / false / nullptr.
-	/// This is only available for zero constructible types.
 	_FORCE_INLINE_ void resize_initialized(U p_size) { _resize<true>(p_size); }
 
 	/// Resize and set all values to 0 / false / nullptr.
@@ -410,8 +409,8 @@ public:
 	}
 };
 
-template <typename T, typename U = uint32_t, bool force_trivial = false>
-using TightLocalVector = LocalVector<T, U, force_trivial, true>;
+template <typename T, typename U = uint32_t>
+using TightLocalVector = LocalVector<T, U, false, true>;
 
 // Zero-constructing LocalVector initializes count, capacity and data to 0 and thus empty.
 template <typename T, typename U, bool force_trivial, bool tight>

+ 9 - 5
core/templates/pooled_list.h

@@ -56,8 +56,8 @@
 
 template <typename T, typename U = uint32_t, bool force_trivial = false, bool zero_on_first_request = false>
 class PooledList {
-	LocalVector<T, U, force_trivial> list;
-	LocalVector<U, U, true> freelist;
+	LocalVector<T, U> list;
+	LocalVector<U, U> freelist;
 
 	// not all list members are necessarily used
 	U _used_size;
@@ -102,13 +102,17 @@ public:
 			// pop from freelist
 			int new_size = freelist.size() - 1;
 			r_id = freelist[new_size];
-			freelist.resize(new_size);
+			freelist.resize_uninitialized(new_size);
 
 			return &list[r_id];
 		}
 
 		r_id = list.size();
-		list.resize(r_id + 1);
+		if constexpr (force_trivial || std::is_trivially_constructible_v<T>) {
+			list.resize_uninitialized(r_id + 1);
+		} else {
+			list.resize_initialized(r_id + 1);
+		}
 
 		static_assert((!zero_on_first_request) || (__is_pod(T)), "zero_on_first_request requires trivial type");
 		if constexpr (zero_on_first_request && __is_pod(T)) {
@@ -169,7 +173,7 @@ public:
 
 		// expand the active map (this should be in sync with the pool list
 		if (_pool.used_size() > _active_map.size()) {
-			_active_map.resize(_pool.used_size());
+			_active_map.resize_uninitialized(_pool.used_size());
 		}
 
 		// store in the active map