@@ -13766,14 +13766,8 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
13766
13766
Isolate* isolate = table->GetIsolate();
13767
13767
int capacity = table->Capacity();
13768
13768
int nof = table->NumberOfElements() + n;
13769
- int nod = table->NumberOfDeletedElements();
13770
- // Return if:
13771
- // 50% is still free after adding n elements and
13772
- // at most 50% of the free elements are deleted elements.
13773
- if (nod <= (capacity - nof) >> 1) {
13774
- int needed_free = nof >> 1;
13775
- if (nof + needed_free <= capacity) return table;
13776
- }
13769
+
13770
+ if (table->HasSufficientCapacityToAdd(n)) return table;
13777
13771
13778
13772
const int kMinCapacityForPretenure = 256;
13779
13773
bool should_pretenure = pretenure == TENURED ||
@@ -13790,6 +13784,23 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
13790
13784
}
13791
13785
13792
13786
13787
+ template <typename Derived, typename Shape, typename Key>
13788
+ bool HashTable<Derived, Shape, Key>::HasSufficientCapacityToAdd(
13789
+ int number_of_additional_elements) {
13790
+ int capacity = Capacity();
13791
+ int nof = NumberOfElements() + number_of_additional_elements;
13792
+ int nod = NumberOfDeletedElements();
13793
+ // Return true if:
13794
+ // 50% is still free after adding number_of_additional_elements elements and
13795
+ // at most 50% of the free elements are deleted elements.
13796
+ if ((nof < capacity) && ((nod <= (capacity - nof) >> 1))) {
13797
+ int needed_free = nof >> 1;
13798
+ if (nof + needed_free <= capacity) return true;
13799
+ }
13800
+ return false;
13801
+ }
13802
+
13803
+
13793
13804
template<typename Derived, typename Shape, typename Key>
13794
13805
Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table,
13795
13806
Key key) {
@@ -14827,7 +14838,7 @@ Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices(
14827
14838
}
14828
14839
14829
14840
14830
- template<typename Derived, typename Shape, typename Key>
14841
+ template <typename Derived, typename Shape, typename Key>
14831
14842
Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity(
14832
14843
Handle<Derived> dictionary, int n, Key key) {
14833
14844
// Check whether there are enough enumeration indices to add n elements.
@@ -15215,8 +15226,8 @@ Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
15215
15226
}
15216
15227
// If we're out of luck, we didn't get a GC recently, and so rehashing
15217
15228
// isn't enough to avoid a crash.
15218
- int nof = table->NumberOfElements() + 1;
15219
- if (! table->HasSufficientCapacity(nof)) {
15229
+ if (! table->HasSufficientCapacityToAdd(1)) {
15230
+ int nof = table->NumberOfElements() + 1;
15220
15231
int capacity = ObjectHashTable::ComputeCapacity(nof * 2);
15221
15232
if (capacity > ObjectHashTable::kMaxCapacity) {
15222
15233
for (size_t i = 0; i < 2; ++i) {
0 commit comments