1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
|
/*
* Copyright (C) 2004-2022 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#pragma once
#include "JSExportMacros.h"
#include "PropertyOffset.h"
#include "Structure.h"
#include "WriteBarrier.h"
#include <wtf/HashTable.h>
#include <wtf/MathExtras.h>
#include <wtf/StdLibExtras.h>
#include <wtf/Vector.h>
#include <wtf/text/AtomStringImpl.h>
#define DUMP_PROPERTYMAP_STATS 0
#define DUMP_PROPERTYMAP_COLLISIONS 0
#define PROPERTY_MAP_DELETED_ENTRY_KEY ((UniquedStringImpl*)1)
WTF_ALLOW_UNSAFE_BUFFER_USAGE_BEGIN
namespace JSC {
DECLARE_ALLOCATOR_WITH_HEAP_IDENTIFIER(PropertyTable);
#if DUMP_PROPERTYMAP_STATS
struct PropertyTableStats {
std::atomic<unsigned> numFinds;
std::atomic<unsigned> numCollisions;
std::atomic<unsigned> numLookups;
std::atomic<unsigned> numLookupProbing;
std::atomic<unsigned> numAdds;
std::atomic<unsigned> numRemoves;
std::atomic<unsigned> numRehashes;
std::atomic<unsigned> numReinserts;
};
JS_EXPORT_PRIVATE extern PropertyTableStats* propertyTableStats;
#endif
inline constexpr bool isPowerOf2(unsigned v)
{
return hasOneBitSet(v);
}
inline constexpr unsigned nextPowerOf2(unsigned v)
{
// Taken from http://www.cs.utk.edu/~vose/c-stuff/bithacks.html
// Devised by Sean Anderson, Sepember 14, 2001
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return v;
}
// compact <-> non-compact PropertyTable
// We need to maintain two things, one is PropertyOffset and one is unsigned index in index buffer of PropertyTable.
// But both are typically small. It is possible that we can get optimized table if both are fit in uint8_t, that's
// compact PropertyTable.
//
// PropertyOffset can be offseted with firstOutOfLineOffset since we can get out-of-line property easily, but this
// offset is small enough (64 currently), so that we can still assume that most of property offsets are < 256.
//
// 1. If property offset gets larger than 255, then we get non-compact PropertyTable. It requires at least 191 (255 - 64) properties.
// In that case, PropertyTable size should be 256 since it is power-of-two.
// 2. If index gets larger than 255, then we get non-compact PropertyTable. But we are using 0 and 255 for markers. Thus, if we get 253
// used counts, then we need to change the table.
//
// So, typical scenario is that, once 128th property is added, then we extend the table via rehashing. At that time, we change the
// table from compact to non-compact mode.
//
// index-size table-capacity compact v.s. non-compact
// 16 8 80 192
// 32 16 160 384
// 64 32 320 768
// 128 64 640 1536
// 256 128 1280 3072
// 512 256 N/A 6144 // After 512 size, compact PropertyTable does not work. All table gets non-compact.
class PropertyTable final : public JSCell {
public:
using Base = JSCell;
static constexpr unsigned StructureFlags = Base::StructureFlags | StructureIsImmortal;
template<typename CellType, SubspaceAccess>
static GCClient::IsoSubspace* subspaceFor(VM& vm)
{
return &vm.propertyTableSpace();
}
static constexpr DestructionMode needsDestruction = NeedsDestruction;
static void destroy(JSCell*);
DECLARE_VISIT_CHILDREN;
DECLARE_EXPORT_INFO;
inline static Structure* createStructure(VM&, JSGlobalObject*, JSValue);
using KeyType = UniquedStringImpl*;
using ValueType = PropertyTableEntry;
// Constructor is passed an initial capacity, a PropertyTable to copy, or both.
static PropertyTable* create(VM&, unsigned initialCapacity);
static PropertyTable* clone(VM&, const PropertyTable&);
static PropertyTable* clone(VM&, unsigned initialCapacity, const PropertyTable&);
~PropertyTable();
// Find a value in the table.
std::tuple<PropertyOffset, unsigned> get(const KeyType&);
// Add a value to the table
std::tuple<PropertyOffset, unsigned, bool> WARN_UNUSED_RETURN add(VM&, const ValueType& entry);
// Remove a value from the table.
std::tuple<PropertyOffset, unsigned> take(VM&, const KeyType&);
PropertyOffset updateAttributeIfExists(const KeyType&, unsigned attributes);
PropertyOffset renumberPropertyOffsets(JSObject*, unsigned inlineCapacity, Vector<JSValue>&);
struct FindResult {
unsigned entryIndex;
unsigned index;
PropertyOffset offset;
unsigned attributes;
};
FindResult find(const KeyType&);
std::tuple<PropertyOffset, unsigned, bool> addAfterFind(VM&, const ValueType& entry, FindResult&&);
void seal();
void freeze();
bool isSealed() const;
bool isFrozen() const;
// Returns the number of values in the hashtable.
unsigned size() const;
// Checks if there are any values in the hashtable.
bool isEmpty() const;
// Number of slots in the property storage array in use, included deletedOffsets.
unsigned propertyStorageSize() const;
// Used to maintain a list of unused entries in the property storage.
void clearDeletedOffsets();
bool hasDeletedOffset();
PropertyOffset takeDeletedOffset();
void addDeletedOffset(PropertyOffset);
PropertyOffset nextOffset(PropertyOffset inlineCapacity);
// Copy this PropertyTable, ensuring the copy has at least the capacity provided.
PropertyTable* copy(VM&, unsigned newCapacity);
#ifndef NDEBUG
size_t sizeInMemory();
void checkConsistency();
#endif
template<typename Functor>
void forEachProperty(const Functor&) const;
static constexpr unsigned EmptyEntryIndex = 0;
private:
PropertyTable(VM&, unsigned initialCapacity);
PropertyTable(VM&, const PropertyTable&);
PropertyTable(VM&, unsigned initialCapacity, const PropertyTable&);
PropertyTable(const PropertyTable&);
void finishCreation(VM&);
// Used to insert a value known not to be in the table, and where we know capacity to be available.
template<typename Index, typename Entry>
void reinsert(Index*, Entry*, const ValueType& entry);
static bool canFitInCompact(const ValueType& entry) { return entry.offset() <= UINT8_MAX; }
// Rehash the table. Used to grow, or to recover deleted slots.
void rehash(VM&, unsigned newCapacity, bool canStayCompact);
// The capacity of the table of values is half of the size of the index.
unsigned tableCapacity() const;
// We keep an extra deleted slot after the array to make iteration work,
// and to use for deleted values. Index values into the array are 1-based,
// so this is tableCapacity() + 1.
// For example, if m_tableSize is 16, then tableCapacity() is 8 - but the
// values array is actually 9 long (the 9th used for the deleted value/
// iteration guard). The 8 valid entries are numbered 1..8, so the
// deleted index is 9 (0 being reserved for empty).
unsigned deletedEntryIndex() const;
// Used in iterator creation/progression.
template<typename T>
static T* skipDeletedEntries(T* valuePtr, T* endValuePtr);
// total number of used entries in the values array - by either valid entries, or deleted ones.
unsigned usedCount() const;
// The size in bytes of data needed for by the table.
size_t dataSize(bool isCompact);
static size_t dataSize(bool isCompact, unsigned indexSize);
// Calculates the appropriate table size (rounds up to a power of two).
static unsigned sizeForCapacity(unsigned capacity);
// Check if capacity is available.
bool canInsert(const ValueType&);
void remove(VM&, KeyType, unsigned entryIndex, unsigned index);
template<typename Index, typename Entry>
ALWAYS_INLINE FindResult findImpl(const Index*, const Entry*, const KeyType&);
bool isCompact() const { return m_indexVector & isCompactFlag; }
template<typename Functor>
void forEachPropertyMutable(const Functor&);
// The table of values lies after the hash index.
static CompactPropertyTableEntry* tableFromIndexVector(uint8_t* index, unsigned indexSize)
{
return std::bit_cast<CompactPropertyTableEntry*>(index + indexSize);
}
static const CompactPropertyTableEntry* tableFromIndexVector(const uint8_t* index, unsigned indexSize)
{
return std::bit_cast<const CompactPropertyTableEntry*>(index + indexSize);
}
static PropertyTableEntry* tableFromIndexVector(uint32_t* index, unsigned indexSize)
{
return std::bit_cast<PropertyTableEntry*>(index + indexSize);
}
static const PropertyTableEntry* tableFromIndexVector(const uint32_t* index, unsigned indexSize)
{
return std::bit_cast<const PropertyTableEntry*>(index + indexSize);
}
CompactPropertyTableEntry* tableFromIndexVector(uint8_t* index) { return tableFromIndexVector(index, m_indexSize); }
const CompactPropertyTableEntry* tableFromIndexVector(const uint8_t* index) const { return tableFromIndexVector(index, m_indexSize); }
PropertyTableEntry* tableFromIndexVector(uint32_t* index) { return tableFromIndexVector(index, m_indexSize); }
const PropertyTableEntry* tableFromIndexVector(const uint32_t* index) const { return tableFromIndexVector(index, m_indexSize); }
CompactPropertyTableEntry* tableEndFromIndexVector(uint8_t* index)
{
return tableFromIndexVector(index) + usedCount();
}
const CompactPropertyTableEntry* tableEndFromIndexVector(const uint8_t* index) const
{
return tableFromIndexVector(index) + usedCount();
}
PropertyTableEntry* tableEndFromIndexVector(uint32_t* index)
{
return tableFromIndexVector(index) + usedCount();
}
const PropertyTableEntry* tableEndFromIndexVector(const uint32_t* index) const
{
return tableFromIndexVector(index) + usedCount();
}
static uintptr_t allocateIndexVector(bool isCompact, unsigned indexSize);
static uintptr_t allocateZeroedIndexVector(bool isCompact, unsigned indexSize);
static void destroyIndexVector(uintptr_t indexVector);
template<typename Func>
static ALWAYS_INLINE auto withIndexVector(uintptr_t indexVector, Func&& function) -> decltype(auto)
{
if (indexVector & isCompactFlag)
return function(std::bit_cast<uint8_t*>(indexVector & indexVectorMask));
return function(std::bit_cast<uint32_t*>(indexVector & indexVectorMask));
}
template<typename Func>
ALWAYS_INLINE auto withIndexVector(Func&& function) const -> decltype(auto)
{
return withIndexVector(m_indexVector, std::forward<Func>(function));
}
static constexpr uintptr_t isCompactFlag = 0x1;
static constexpr uintptr_t indexVectorMask = ~isCompactFlag;
unsigned m_indexSize;
unsigned m_indexMask;
uintptr_t m_indexVector;
unsigned m_keyCount;
unsigned m_deletedCount;
std::unique_ptr<Vector<PropertyOffset>> m_deletedOffsets;
static constexpr unsigned MinimumTableSize = 16;
static_assert(MinimumTableSize >= 16, "compact index is uint8_t and we should keep 16 byte aligned entries after this array");
};
template<typename Index, typename Entry>
PropertyTable::FindResult PropertyTable::findImpl(const Index* indexVector, const Entry* table, const KeyType& key)
{
unsigned hash = IdentifierRepHash::hash(key);
unsigned indexMask = m_indexMask;
unsigned probeCount = 0;
unsigned index = hash & indexMask;
#if DUMP_PROPERTYMAP_STATS
++propertyTableStats->numFinds;
#endif
while (true) {
unsigned entryIndex = indexVector[index];
if (entryIndex == EmptyEntryIndex)
return FindResult { entryIndex, index, invalidOffset, 0 };
const auto& entry = table[entryIndex - 1];
if (key == entry.key()) {
ASSERT(!m_deletedOffsets || !m_deletedOffsets->contains(entry.offset()));
return FindResult { entryIndex, index, entry.offset(), entry.attributes() };
}
#if DUMP_PROPERTYMAP_STATS
++propertyTableStats->numCollisions;
#endif
#if DUMP_PROPERTYMAP_COLLISIONS
dataLog("PropertyTable collision for ", key, " (", hash, ")\n");
dataLog("Collided with ", entry.key(), "(", IdentifierRepHash::hash(entry.key()), ")\n");
#endif
++probeCount;
index = (index + probeCount) & indexMask;
}
}
inline PropertyTable::FindResult PropertyTable::find(const KeyType& key)
{
ASSERT(key);
ASSERT(key->isAtom() || key->isSymbol());
return withIndexVector([&](auto* vector) {
return findImpl(vector, tableFromIndexVector(vector), key);
});
}
inline std::tuple<PropertyOffset, unsigned> PropertyTable::get(const KeyType& key)
{
ASSERT(key);
ASSERT(key->isAtom() || key->isSymbol());
ASSERT(key != PROPERTY_MAP_DELETED_ENTRY_KEY);
if (!m_keyCount)
return std::tuple { invalidOffset, 0 };
FindResult result = find(key);
return std::tuple { result.offset, result.attributes };
}
inline std::tuple<PropertyOffset, unsigned, bool> WARN_UNUSED_RETURN PropertyTable::add(VM& vm, const ValueType& entry)
{
ASSERT(!m_deletedOffsets || !m_deletedOffsets->contains(entry.offset()));
// Look for a value with a matching key already in the array.
FindResult result = find(entry.key());
if (result.offset != invalidOffset)
return std::tuple { result.offset, result.attributes, false };
return addAfterFind(vm, entry, WTFMove(result));
}
ALWAYS_INLINE std::tuple<PropertyOffset, unsigned, bool> PropertyTable::addAfterFind(VM& vm, const ValueType& entry, FindResult&& result)
{
#if DUMP_PROPERTYMAP_STATS
++propertyTableStats->numAdds;
#endif
// Ref the key
entry.key()->ref();
// ensure capacity is available.
if (!canInsert(entry)) {
rehash(vm, m_keyCount + 1, canFitInCompact(entry));
result = find(entry.key());
ASSERT(result.offset == invalidOffset);
ASSERT(result.entryIndex == EmptyEntryIndex);
}
// Allocate a slot in the hashtable, and set the index to reference this.
ASSERT(!isCompact() || usedCount() < UINT8_MAX);
unsigned index = result.index;
unsigned entryIndex = usedCount() + 1;
withIndexVector([&](auto* vector) {
vector[index] = entryIndex;
tableFromIndexVector(vector)[entryIndex - 1] = entry;
});
++m_keyCount;
return std::tuple { entry.offset(), entry.attributes(), true };
}
inline void PropertyTable::remove(VM& vm, KeyType key, unsigned entryIndex, unsigned index)
{
#if DUMP_PROPERTYMAP_STATS
++propertyTableStats->numRemoves;
#endif
// Replace this one element with the deleted sentinel. Also clear out
// the entry so we can iterate all the entries as needed.
withIndexVector([&](auto* vector) {
vector[index] = deletedEntryIndex();
tableFromIndexVector(vector)[entryIndex - 1].setKey(PROPERTY_MAP_DELETED_ENTRY_KEY);
});
key->deref();
ASSERT(m_keyCount >= 1);
--m_keyCount;
++m_deletedCount;
if (m_deletedCount * 4 >= m_indexSize)
rehash(vm, m_keyCount, true);
}
inline std::tuple<PropertyOffset, unsigned> PropertyTable::take(VM& vm, const KeyType& key)
{
FindResult result = find(key);
if (result.offset != invalidOffset)
remove(vm, key, result.entryIndex, result.index);
return std::tuple { result.offset, result.attributes };
}
inline PropertyOffset PropertyTable::updateAttributeIfExists(const KeyType& key, unsigned attributes)
{
return withIndexVector([&](auto* vector) -> PropertyOffset {
auto* table = tableFromIndexVector(vector);
FindResult result = findImpl(vector, table, key);
if (result.offset == invalidOffset)
return invalidOffset;
table[result.entryIndex - 1].setAttributes(attributes);
return result.offset;
});
}
// returns the number of values in the hashtable.
inline unsigned PropertyTable::size() const
{
return m_keyCount;
}
inline bool PropertyTable::isEmpty() const
{
return !m_keyCount;
}
inline unsigned PropertyTable::propertyStorageSize() const
{
return size() + (m_deletedOffsets ? m_deletedOffsets->size() : 0);
}
inline void PropertyTable::clearDeletedOffsets()
{
m_deletedOffsets = nullptr;
}
inline bool PropertyTable::hasDeletedOffset()
{
return m_deletedOffsets && !m_deletedOffsets->isEmpty();
}
inline PropertyOffset PropertyTable::takeDeletedOffset()
{
return m_deletedOffsets->takeLast();
}
inline void PropertyTable::addDeletedOffset(PropertyOffset offset)
{
if (!m_deletedOffsets)
m_deletedOffsets = makeUnique<Vector<PropertyOffset>>();
ASSERT(!m_deletedOffsets->contains(offset));
m_deletedOffsets->append(offset);
}
inline PropertyOffset PropertyTable::nextOffset(PropertyOffset inlineCapacity)
{
if (hasDeletedOffset())
return takeDeletedOffset();
return offsetForPropertyNumber(size(), inlineCapacity);
}
inline PropertyTable* PropertyTable::copy(VM& vm, unsigned newCapacity)
{
ASSERT(newCapacity >= m_keyCount);
// Fast case; if the new table will be the same m_indexSize as this one, we can memcpy it,
// save rehashing all keys.
if (sizeForCapacity(newCapacity) == m_indexSize)
return PropertyTable::clone(vm, *this);
return PropertyTable::clone(vm, newCapacity, *this);
}
#ifndef NDEBUG
inline size_t PropertyTable::sizeInMemory()
{
size_t result = sizeof(PropertyTable) + dataSize(isCompact());
if (m_deletedOffsets)
result += (m_deletedOffsets->capacity() * sizeof(PropertyOffset));
return result;
}
#endif
template<typename Index, typename Entry>
inline void PropertyTable::reinsert(Index* indexVector, Entry* table, const ValueType& entry)
{
#if DUMP_PROPERTYMAP_STATS
++propertyTableStats->numReinserts;
#endif
// Used to insert a value known not to be in the table, and where
// we know capacity to be available.
ASSERT(canInsert(entry));
unsigned hash = IdentifierRepHash::hash(entry.key());
unsigned indexMask = m_indexMask;
unsigned probeCount = 0;
unsigned index = hash & indexMask;
// Reinsert must not conflict with the keys since all entries are existing ones.
// Plus, there is no deleted entries too. We should just check emptyness, that's it.
while (true) {
unsigned entryIndex = indexVector[index];
if (entryIndex == EmptyEntryIndex)
break;
ASSERT(table[entryIndex - 1].key() != entry.key());
++probeCount;
index = (index + probeCount) & indexMask;
}
ASSERT(!isCompact() || usedCount() < UINT8_MAX);
unsigned entryIndex = usedCount() + 1;
indexVector[index] = entryIndex;
table[entryIndex - 1] = entry;
++m_keyCount;
}
inline void PropertyTable::rehash(VM& vm, unsigned newCapacity, bool canStayCompact)
{
#if DUMP_PROPERTYMAP_STATS
++propertyTableStats->numRehashes;
#endif
uintptr_t oldIndexVector = m_indexVector;
bool oldIsCompact = oldIndexVector & isCompactFlag;
unsigned oldIndexSize = m_indexSize;
unsigned oldUsedCount = usedCount();
size_t oldDataSize = dataSize(oldIsCompact, oldIndexSize);
m_indexSize = sizeForCapacity(newCapacity);
m_indexMask = m_indexSize - 1;
m_keyCount = 0;
m_deletedCount = 0;
// Once table gets non-compact, we do not change it back to compact again.
// This is because some of property offset can be larger than UINT8_MAX already.
bool isCompact = canStayCompact && oldIsCompact && tableCapacity() < UINT8_MAX;
m_indexVector = allocateZeroedIndexVector(isCompact, m_indexSize);
withIndexVector([&](auto* vector) {
auto* table = tableFromIndexVector(vector);
withIndexVector(oldIndexVector, [&](const auto* oldVector) {
const auto* oldCursor = tableFromIndexVector(oldVector, oldIndexSize);
const auto* oldEnd = oldCursor + oldUsedCount;
for (; oldCursor != oldEnd; ++oldCursor) {
if (oldCursor->key() == PROPERTY_MAP_DELETED_ENTRY_KEY)
continue;
ASSERT(canInsert(*oldCursor));
reinsert(vector, table, *oldCursor);
}
});
});
destroyIndexVector(oldIndexVector);
size_t newDataSize = dataSize(this->isCompact());
if (oldDataSize < newDataSize)
vm.heap.reportExtraMemoryAllocated(this, newDataSize - oldDataSize);
}
inline unsigned PropertyTable::tableCapacity() const { return m_indexSize >> 1; }
inline unsigned PropertyTable::deletedEntryIndex() const { return tableCapacity() + 1; }
template<typename T>
inline T* PropertyTable::skipDeletedEntries(T* valuePtr, T* endValuePtr)
{
while (valuePtr < endValuePtr && valuePtr->key() == PROPERTY_MAP_DELETED_ENTRY_KEY)
++valuePtr;
return valuePtr;
}
inline unsigned PropertyTable::usedCount() const
{
// Total number of used entries in the values array - by either valid entries, or deleted ones.
return m_keyCount + m_deletedCount;
}
inline size_t PropertyTable::dataSize(bool isCompact, unsigned indexSize)
{
if (isCompact)
return indexSize * sizeof(uint8_t) + ((indexSize >> 1) + 1) * sizeof(CompactPropertyTableEntry);
return indexSize * sizeof(uint32_t) + ((indexSize >> 1) + 1) * sizeof(PropertyTableEntry);
}
inline size_t PropertyTable::dataSize(bool isCompact)
{
// The size in bytes of data needed for by the table.
// Ensure that this function can be called concurrently.
return dataSize(isCompact, m_indexSize);
}
ALWAYS_INLINE uintptr_t PropertyTable::allocateIndexVector(bool isCompact, unsigned indexSize)
{
return std::bit_cast<uintptr_t>(PropertyTableMalloc::malloc(PropertyTable::dataSize(isCompact, indexSize))) | (isCompact ? isCompactFlag : 0);
}
ALWAYS_INLINE uintptr_t PropertyTable::allocateZeroedIndexVector(bool isCompact, unsigned indexSize)
{
return std::bit_cast<uintptr_t>(PropertyTableMalloc::zeroedMalloc(PropertyTable::dataSize(isCompact, indexSize))) | (isCompact ? isCompactFlag : 0);
}
ALWAYS_INLINE void PropertyTable::destroyIndexVector(uintptr_t indexVector)
{
PropertyTableMalloc::free(std::bit_cast<void*>(indexVector & indexVectorMask));
}
inline unsigned PropertyTable::sizeForCapacity(unsigned capacity)
{
if (capacity < MinimumTableSize / 2)
return MinimumTableSize;
return nextPowerOf2(capacity + 1) * 2;
}
inline bool PropertyTable::canInsert(const ValueType& entry)
{
if (usedCount() >= tableCapacity())
return false;
if (!isCompact())
return true;
return canFitInCompact(entry);
}
template<typename Functor>
inline void PropertyTable::forEachProperty(const Functor& functor) const
{
withIndexVector([&](const auto* vector) {
const auto* cursor = tableFromIndexVector(vector);
const auto* end = tableEndFromIndexVector(vector);
for (; cursor != end; ++cursor) {
if (cursor->key() == PROPERTY_MAP_DELETED_ENTRY_KEY)
continue;
if (functor(*cursor) == IterationStatus::Done)
return;
}
});
}
} // namespace JSC
WTF_ALLOW_UNSAFE_BUFFER_USAGE_END
|