Skip to content

Commit e22ffef

Browse files
author
Matt Loring
committed
deps: cherry-pick a76d133 from v8 upstream
Original commit message: Fix incorrect parameter to HasSufficientCapacity It takes the number of additional elements, not the total target capacity. Also, avoid right-shifting a negative integer as this is undefined in general BUG=v8:4909 [email protected] Review-Url: https://codereview.chromium.org/2162333002 Cr-Commit-Position: refs/heads/master@{#37901} Fixes: #6180 PR-URL: #7689 Reviewed-By: Matt Loring <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent a3d62bd commit e22ffef

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

deps/v8/src/objects.cc

+9-9
Original file line numberDiff line numberDiff line change
@@ -17389,7 +17389,7 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1738917389
int capacity = table->Capacity();
1739017390
int nof = table->NumberOfElements() + n;
1739117391

17392-
if (table->HasSufficientCapacity(n)) return table;
17392+
if (table->HasSufficientCapacityToAdd(n)) return table;
1739317393

1739417394
const int kMinCapacityForPretenure = 256;
1739517395
bool should_pretenure = pretenure == TENURED ||
@@ -17405,16 +17405,16 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1740517405
return new_table;
1740617406
}
1740717407

17408-
1740917408
template <typename Derived, typename Shape, typename Key>
17410-
bool HashTable<Derived, Shape, Key>::HasSufficientCapacity(int n) {
17409+
bool HashTable<Derived, Shape, Key>::HasSufficientCapacityToAdd(
17410+
int number_of_additional_elements) {
1741117411
int capacity = Capacity();
17412-
int nof = NumberOfElements() + n;
17412+
int nof = NumberOfElements() + number_of_additional_elements;
1741317413
int nod = NumberOfDeletedElements();
1741417414
// Return true if:
17415-
// 50% is still free after adding n elements and
17415+
// 50% is still free after adding number_of_additional_elements elements and
1741617416
// at most 50% of the free elements are deleted elements.
17417-
if (nod <= (capacity - nof) >> 1) {
17417+
if ((nof < capacity) && ((nod <= (capacity - nof) >> 1))) {
1741817418
int needed_free = nof >> 1;
1741917419
if (nof + needed_free <= capacity) return true;
1742017420
}
@@ -18332,7 +18332,7 @@ void Dictionary<Derived, Shape, Key>::SetRequiresCopyOnCapacityChange() {
1833218332
DCHECK_EQ(0, DerivedHashTable::NumberOfDeletedElements());
1833318333
// Make sure that HashTable::EnsureCapacity will create a copy.
1833418334
DerivedHashTable::SetNumberOfDeletedElements(DerivedHashTable::Capacity());
18335-
DCHECK(!DerivedHashTable::HasSufficientCapacity(1));
18335+
DCHECK(!DerivedHashTable::HasSufficientCapacityToAdd(1));
1833618336
}
1833718337

1833818338

@@ -18742,8 +18742,8 @@ Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
1874218742
}
1874318743
// If we're out of luck, we didn't get a GC recently, and so rehashing
1874418744
// isn't enough to avoid a crash.
18745-
int nof = table->NumberOfElements() + 1;
18746-
if (!table->HasSufficientCapacity(nof)) {
18745+
if (!table->HasSufficientCapacityToAdd(1)) {
18746+
int nof = table->NumberOfElements() + 1;
1874718747
int capacity = ObjectHashTable::ComputeCapacity(nof * 2);
1874818748
if (capacity > ObjectHashTable::kMaxCapacity) {
1874918749
for (size_t i = 0; i < 2; ++i) {

deps/v8/src/objects.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3331,7 +3331,7 @@ class HashTable : public HashTableBase {
33313331
PretenureFlag pretenure = NOT_TENURED);
33323332

33333333
// Returns true if this table has sufficient capacity for adding n elements.
3334-
bool HasSufficientCapacity(int n);
3334+
bool HasSufficientCapacityToAdd(int number_of_additional_elements);
33353335

33363336
// Sets the capacity of the hash table.
33373337
void SetCapacity(int capacity) {

0 commit comments

Comments
 (0)