Skip to content

Commit 2b01bc8

Browse files
Matt LoringMylesBorins
Matt Loring
authored andcommitted
deps: backport 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 Ref: #7883 PR-URL: #7689 Reviewed-By: Matt Loring <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent e1f12fb commit 2b01bc8

File tree

3 files changed

+26
-12
lines changed

3 files changed

+26
-12
lines changed

deps/v8/include/v8-version.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#define V8_MAJOR_VERSION 4
1212
#define V8_MINOR_VERSION 5
1313
#define V8_BUILD_NUMBER 103
14-
#define V8_PATCH_LEVEL 40
14+
#define V8_PATCH_LEVEL 41
1515

1616
// Use 1 for candidates and 0 otherwise.
1717
// (Boolean macro values are not supported by all preprocessors.)

deps/v8/src/objects.cc

+22-11
Original file line numberDiff line numberDiff line change
@@ -13766,14 +13766,8 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1376613766
Isolate* isolate = table->GetIsolate();
1376713767
int capacity = table->Capacity();
1376813768
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;
1377713771

1377813772
const int kMinCapacityForPretenure = 256;
1377913773
bool should_pretenure = pretenure == TENURED ||
@@ -13790,6 +13784,23 @@ Handle<Derived> HashTable<Derived, Shape, Key>::EnsureCapacity(
1379013784
}
1379113785

1379213786

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+
1379313804
template<typename Derived, typename Shape, typename Key>
1379413805
Handle<Derived> HashTable<Derived, Shape, Key>::Shrink(Handle<Derived> table,
1379513806
Key key) {
@@ -14827,7 +14838,7 @@ Dictionary<Derived, Shape, Key>::GenerateNewEnumerationIndices(
1482714838
}
1482814839

1482914840

14830-
template<typename Derived, typename Shape, typename Key>
14841+
template <typename Derived, typename Shape, typename Key>
1483114842
Handle<Derived> Dictionary<Derived, Shape, Key>::EnsureCapacity(
1483214843
Handle<Derived> dictionary, int n, Key key) {
1483314844
// Check whether there are enough enumeration indices to add n elements.
@@ -15215,8 +15226,8 @@ Handle<ObjectHashTable> ObjectHashTable::Put(Handle<ObjectHashTable> table,
1521515226
}
1521615227
// If we're out of luck, we didn't get a GC recently, and so rehashing
1521715228
// 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;
1522015231
int capacity = ObjectHashTable::ComputeCapacity(nof * 2);
1522115232
if (capacity > ObjectHashTable::kMaxCapacity) {
1522215233
for (size_t i = 0; i < 2; ++i) {

deps/v8/src/objects.h

+3
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,9 @@ class HashTable : public HashTableBase {
31143114
Key key,
31153115
PretenureFlag pretenure = NOT_TENURED);
31163116

3117+
// Returns true if this table has sufficient capacity for adding n elements.
3118+
bool HasSufficientCapacityToAdd(int number_of_additional_elements);
3119+
31173120
// Sets the capacity of the hash table.
31183121
void SetCapacity(int capacity) {
31193122
// To scale a computed hash code to fit within the hash table, we

0 commit comments

Comments
 (0)