浏览代码

Merge pull request #103698 from AThousandShips/fix_a_hash_map_construct

[Core] Fix `AHashMap` constructors reserving too few elements
Thaddeus Crews 5 月之前
父节点
当前提交
4c9086312d
共有 1 个文件被更改,包括 6 次插入2 次删除
  1. 6 2
      core/templates/a_hash_map.h

+ 6 - 2
core/templates/a_hash_map.h

@@ -665,7 +665,9 @@ public:
 	}
 
 	AHashMap(const HashMap<TKey, TValue> &p_other) {
-		reserve(p_other.size());
+		if (p_other.size() > get_capacity()) {
+			reserve(p_other.size());
+		}
 		for (const KeyValue<TKey, TValue> &E : p_other) {
 			uint32_t hash = _hash(E.key);
 			_insert_element(E.key, E.value, hash);
@@ -703,7 +705,9 @@ public:
 	}
 
 	AHashMap(std::initializer_list<KeyValue<TKey, TValue>> p_init) {
-		reserve(p_init.size());
+		if (p_init.size() > get_capacity()) {
+			reserve(p_init.size());
+		}
 		for (const KeyValue<TKey, TValue> &E : p_init) {
 			insert(E.key, E.value);
 		}