Skip to content

Commit e6887e2

Browse files
Matt Loringcjihrig
Matt Loring
authored andcommitted
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 R=verwaest@chromium.org Review-Url: https://codereview.chromium.org/2162333002 Cr-Commit-Position: refs/heads/master@{#37901} Fixes: #6180 PR-URL: #7689 Reviewed-By: Matt Loring <mattloring@google.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent a03e3d3 commit e6887e2

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
@@ -17437,7 +17437,7 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1743717437
int capacity = table->Capacity();
1743817438
int nof = table->NumberOfElements() + n;
1743917439

17440-
if (table->HasSufficientCapacity(n)) return table;
17440+
if (table->HasSufficientCapacityToAdd(n)) return table;
1744117441

1744217442
const int kMinCapacityForPretenure = 256;
1744317443
bool should_pretenure = pretenure == TENURED ||
@@ -17453,16 +17453,16 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1745317453
return new_table;
1745417454
}
1745517455

17456-
1745717456
template <typename Derived, typename Shape, typename Key>
17458-
bool HashTable<Derived, Shape, Key>::HasSufficientCapacity(int n) {
17457+
bool HashTable<Derived, Shape, Key>::HasSufficientCapacityToAdd(
17458+
int number_of_additional_elements) {
1745917459
int capacity = Capacity();
17460-
int nof = NumberOfElements() + n;
17460+
int nof = NumberOfElements() + number_of_additional_elements;
1746117461
int nod = NumberOfDeletedElements();
1746217462
// Return true if:
17463-
// 50% is still free after adding n elements and
17463+
// 50% is still free after adding number_of_additional_elements elements and
1746417464
// at most 50% of the free elements are deleted elements.
17465-
if (nod <= (capacity - nof) >> 1) {
17465+
if ((nof < capacity) && ((nod <= (capacity - nof) >> 1))) {
1746617466
int needed_free = nof >> 1;
1746717467
if (nof + needed_free <= capacity) return true;
1746817468
}
@@ -18378,7 +18378,7 @@ void Dictionary<Derived, Shape, Key>::SetRequiresCopyOnCapacityChange() {
1837818378
DCHECK_EQ(0, DerivedHashTable::NumberOfDeletedElements());
1837918379
// Make sure that HashTable::EnsureCapacity will create a copy.
1838018380
DerivedHashTable::SetNumberOfDeletedElements(DerivedHashTable::Capacity());
18381-
DCHECK(!DerivedHashTable::HasSufficientCapacity(1));
18381+
DCHECK(!DerivedHashTable::HasSufficientCapacityToAdd(1));
1838218382
}
1838318383

1838418384

@@ -18791,8 +18791,8 @@ Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
1879118791
}
1879218792
// If we're out of luck, we didn't get a GC recently, and so rehashing
1879318793
// isn't enough to avoid a crash.
18794-
int nof = table->NumberOfElements() + 1;
18795-
if (!table->HasSufficientCapacity(nof)) {
18794+
if (!table->HasSufficientCapacityToAdd(1)) {
18795+
int nof = table->NumberOfElements() + 1;
1879618796
int capacity = ObjectHashTable::ComputeCapacity(nof * 2);
1879718797
if (capacity > ObjectHashTable::kMaxCapacity) {
1879818798
for (size_t i = 0; i < 2; ++i) {

deps/v8/src/objects.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3307,7 +3307,7 @@ class HashTable : public HashTableBase {
33073307
PretenureFlag pretenure = NOT_TENURED);
33083308

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

33123312
// Sets the capacity of the hash table.
33133313
void SetCapacity(int capacity) {

0 commit comments

Comments
 (0)