diff --git a/hphp/runtime/base/array/array_data.cpp b/hphp/runtime/base/array/array_data.cpp index 433553fbb..75d770dab 100644 --- a/hphp/runtime/base/array/array_data.cpp +++ b/hphp/runtime/base/array/array_data.cpp @@ -360,7 +360,7 @@ Variant ArrayData::key() const { return uninit_null(); } -Variant ArrayData::value(int32_t &pos) const { +Variant ArrayData::value(ssize_t &pos) const { if (size_t(pos) < size_t(size())) { return getValue(pos); } diff --git a/hphp/runtime/base/array/array_data.h b/hphp/runtime/base/array/array_data.h index c015c61ac..7bdd54e43 100644 --- a/hphp/runtime/base/array/array_data.h +++ b/hphp/runtime/base/array/array_data.h @@ -55,26 +55,14 @@ class ArrayData : public Countable { public: static const ssize_t invalid_index = -1; - explicit ArrayData(ArrayKind kind, bool nonsmart = false) - : m_size(-1) - , m_kind(kind) - , m_nonsmart(nonsmart) - , m_pos(0) - , m_strongIterators(nullptr) { - } - explicit ArrayData(ArrayKind kind, bool nonsmart, uint size) - : m_size(size) - , m_kind(kind) - , m_nonsmart(nonsmart) - , m_pos(size ? 0 : ArrayData::invalid_index) - , m_strongIterators(nullptr) { + explicit ArrayData(ArrayKind kind, bool nonsmart = false) : + m_size(-1), m_pos(0), m_strongIterators(0), m_kind(kind), + m_nonsmart(nonsmart) { } ArrayData(const ArrayData *src, ArrayKind kind, - bool nonsmart = false) - : m_kind(src->m_kind) - , m_nonsmart(nonsmart) - , m_pos(src->m_pos) - , m_strongIterators(nullptr) { + bool nonsmart = false) : + m_pos(src->m_pos), m_strongIterators(0), m_kind(src->m_kind), + m_nonsmart(nonsmart) { } static HphpArray* Make(uint capacity); @@ -194,7 +182,7 @@ class ArrayData : public Countable { virtual Variant next(); virtual Variant end(); virtual Variant key() const; - virtual Variant value(int32_t &pos) const; + virtual Variant value(ssize_t &pos) const; virtual Variant each(); bool isHead() const { return m_pos == iter_begin(); } @@ -492,13 +480,13 @@ class ArrayData : public Countable { protected: uint m_size; - const ArrayKind m_kind; - AllocMode m_allocMode; - const bool m_nonsmart; // never use smartalloc to allocate Elms - int32_t m_pos; + ssize_t m_pos; private: FullPos* m_strongIterators; // head of linked list protected: + const ArrayKind m_kind; + AllocMode m_allocMode; + const bool m_nonsmart; // never use smartalloc to allocate Elms /* The 4 bytes of padding here are available to subclasses if their * first field is also <= 4 bytes. */ diff --git a/hphp/runtime/base/array/hphp_array.cpp b/hphp/runtime/base/array/hphp_array.cpp index 618ffe94b..e4dca6f29 100644 --- a/hphp/runtime/base/array/hphp_array.cpp +++ b/hphp/runtime/base/array/hphp_array.cpp @@ -66,13 +66,7 @@ static const Trace::Module TRACEMOD = Trace::runtime; * separately. Even larger tables allocate the hashtable and slots * contiguously. */ -void *HphpArray::SmaAllocatorInitSetup = SmartAllocatorInitSetup(); - -void HphpArray::release() { - assert(typeid(*this) == typeid(HphpArray)); - this->HphpArray::~HphpArray(); - HphpArray::AllocatorType::getNoCheck()->dealloc(this); -} +IMPLEMENT_SMART_ALLOCATION(HphpArray); //============================================================================= // Static members. @@ -82,57 +76,55 @@ HphpArray HphpArray::s_theEmptyArray(StaticEmptyArray); //============================================================================= // Helpers. -static inline size_t computeMaskFromNumElms(const uint32_t n) { - if (n <= HphpArray::SmallSize) { - return HphpArray::SmallMask; +static inline size_t computeMaskFromNumElms(uint32_t numElms) { + assert(numElms <= 0x7fffffffU); + size_t lgSize = HphpArray::MinLgTableSize; + size_t maxElms = (size_t(3U)) << (lgSize-2); + assert(lgSize >= 2); + while (maxElms < numElms) { + ++lgSize; + maxElms <<= 1; } - uint32_t cap = (3U << (HphpArray::MinLgTableSize - 2)) * 2; - while (cap < n) { - cap *= 2; - } - return ((size_t(cap) << 2UL) & (size_t(cap) << 1UL)) - 1; + assert(lgSize <= 32); + // return 2^lgSize - 1 + return ((size_t(1U)) << lgSize) - 1; + static_assert(HphpArray::MinLgTableSize >= 2, + "lower limit for 0.75 load factor"); } //============================================================================= // Construction/destruction. -inline uint32_t HphpArray::initWithoutHash(uint capacity) { - assert(m_size == 0); - m_tableMask = computeMaskFromNumElms(capacity); - auto const tableSize = computeTableSize(m_tableMask); - allocData(computeMaxElms(m_tableMask), tableSize); +inline void HphpArray::init(uint size) { + m_size = 0; + m_tableMask = computeMaskFromNumElms(size); + size_t tableSize = computeTableSize(m_tableMask); + size_t maxElms = computeMaxElms(m_tableMask); + allocData(maxElms, tableSize); initHash(m_hash, tableSize); - assert(m_pos == ArrayData::invalid_index); - return tableSize; + m_pos = ArrayData::invalid_index; } -inline void HphpArray::init(uint capacity) { - assert(m_size == 0); - const auto tableSize = initWithoutHash(capacity); - initHash(m_hash, tableSize); -} - -HphpArray::HphpArray(uint size) : ArrayData(kHphpArray, false, 0), - m_lastE(ElmIndEmpty), m_hLoad(0), m_nextKI(0) { +HphpArray::HphpArray(uint size) : ArrayData(kHphpArray), + m_lastE(ElmIndEmpty), m_data(nullptr), m_nextKI(0), m_hLoad(0) { #ifdef PEDANTIC if (size > 0x7fffffffU) { raise_error("Cannot create an array with more than 2^31 - 1 elements"); } #endif - assert(m_size == 0); init(size); } HphpArray::HphpArray(uint size, const TypedValue* values) : - ArrayData(kHphpArray, false, 0), m_lastE(ElmIndEmpty), - m_hLoad(0), m_nextKI(0) + ArrayData(kHphpArray), m_lastE(ElmIndEmpty), m_data(nullptr), + m_nextKI(0), m_hLoad(0) { #ifdef PEDANTIC if (size > 0x7fffffffU) { raise_error("Cannot create an array with more than 2^31 - 1 elements"); } #endif - initWithoutHash(size); + init(size); assert(size <= m_tableMask + 1); // append values by moving -- Caller assumes we update refcount. Values // are in reverse order since they come from the stack, which grows down. @@ -140,18 +132,14 @@ HphpArray::HphpArray(uint size, const TypedValue* values) : assert(m_size == 0 && m_hLoad == 0 && m_nextKI == 0); ElmInd* hash = m_hash; Elm* data = m_data; - uint i = 0; - for (; i < size; i++) { + for (uint i = 0; i < size; i++) { + assert(hash[i] == HphpArray::ElmIndEmpty); const TypedValue& tv = values[size - i - 1]; data[i].data.m_data = tv.m_data; data[i].data.m_type = tv.m_type; data[i].setIntKey(i); hash[i] = i; } - // Initialize the leftover hash - for (; i < size; i++) { - hash[i] = ElmIndEmpty; - } m_size = size; m_hLoad = size; m_lastE = size - 1; @@ -159,8 +147,8 @@ HphpArray::HphpArray(uint size, const TypedValue* values) : if (size > 0) m_pos = 0; } -HphpArray::HphpArray(EmptyMode) : ArrayData(kHphpArray, false, 0), - m_lastE(ElmIndEmpty), m_hLoad(0), m_nextKI(0) { +HphpArray::HphpArray(EmptyMode) : ArrayData(kHphpArray), + m_lastE(ElmIndEmpty), m_data(nullptr), m_nextKI(0), m_hLoad(0) { init(0); setStatic(); } @@ -204,7 +192,7 @@ void HphpArray::dumpDebugInfo() const { "m_tableMask = %u\tm_size = %d\tm_hLoad = %d\n" "m_nextKI = %" PRId64 "\t\tm_lastE = %d\tm_pos = %zd\n", m_data, m_hash, m_tableMask, m_size, m_hLoad, - m_nextKI, m_lastE, size_t(m_pos)); + m_nextKI, m_lastE, m_pos); fprintf(stderr, "Elements:\n"); ssize_t lastE = m_lastE; Elm* elms = m_data; @@ -406,7 +394,7 @@ Variant HphpArray::key() const { return uninit_null(); } -Variant HphpArray::value(int32_t& pos) const { +Variant HphpArray::value(ssize_t& pos) const { if (pos != ArrayData::invalid_index) { Elm* e = &m_data[pos]; assert(e->data.m_type != KindOfTombstone); @@ -715,6 +703,7 @@ void HphpArray::newElmStr(ElmInd* ei, strhash_t h, StringData* key, } void HphpArray::allocData(size_t maxElms, size_t tableSize) { + assert(!m_data); if (maxElms <= SmallSize) { m_data = m_inline_data.slots; m_hash = m_inline_data.hash; diff --git a/hphp/runtime/base/array/hphp_array.h b/hphp/runtime/base/array/hphp_array.h index 96984fd25..304753d74 100644 --- a/hphp/runtime/base/array/hphp_array.h +++ b/hphp/runtime/base/array/hphp_array.h @@ -111,7 +111,7 @@ public: Variant next(); Variant end(); Variant key() const; - Variant value(int32_t& pos) const; + Variant value(ssize_t& pos) const; Variant each(); // implements ArrayData @@ -297,7 +297,6 @@ public: // Use a minimum of an 4-element hash table. Valid range: [2..32] static const uint32_t MinLgTableSize = 2; static const uint32_t SmallHashSize = 1 << MinLgTableSize; - static const uint32_t SmallMask = SmallHashSize - 1; static const uint32_t SmallSize = SmallHashSize - SmallHashSize / LoadScale; struct InlineSlots { @@ -359,11 +358,11 @@ private: // +--------------------+ ElmInd m_lastE; // Index of last used element. - uint32_t m_tableMask; // Bitmask used when indexing into the hash table. - uint32_t m_hLoad; // Hash table load (# of non-empty slots). - int64_t m_nextKI; // Next integer key to use for append. Elm* m_data; // Contains elements and hash table. ElmInd* m_hash; // Hash table. + int64_t m_nextKI; // Next integer key to use for append. + uint32_t m_tableMask; // Bitmask used when indexing into the hash table. + uint32_t m_hLoad; // Hash table load (# of non-empty slots). union { InlineSlots m_inline_data; ElmInd m_inline_hash[sizeof(m_inline_data) / sizeof(ElmInd)]; @@ -440,11 +439,9 @@ private: void reallocData(size_t maxElms, size_t tableSize, uint oldMask); /** - * init(size) allocates space for size elements but initializes as - * an empty array. The "WithoutHash" version does not initialize the - * hash table and returns the hash table size. + * init(size) allocates space for size elements but initializes + * as an empty array */ - uint32_t initWithoutHash(uint size); void init(uint size); /** diff --git a/hphp/runtime/base/array/policy_array.cpp b/hphp/runtime/base/array/policy_array.cpp index 2e3bd45a3..bb55c9b0d 100644 --- a/hphp/runtime/base/array/policy_array.cpp +++ b/hphp/runtime/base/array/policy_array.cpp @@ -284,7 +284,7 @@ Variant ArrayShell::key() const { return key(toPos(m_pos)); } -Variant ArrayShell::value(int32_t &pos) const { +Variant ArrayShell::value(ssize_t &pos) const { NOT_IMPLEMENTED(); } diff --git a/hphp/runtime/base/array/policy_array.h b/hphp/runtime/base/array/policy_array.h index 0b706cc34..4d33154ad 100644 --- a/hphp/runtime/base/array/policy_array.h +++ b/hphp/runtime/base/array/policy_array.h @@ -54,10 +54,6 @@ static uint32_t toUint32(PosType pos) { } // Signed to pos, no change in representation. -inline PosType toPos(int32_t n) { - static_assert(sizeof(int64_t) == sizeof(PosType), ""); - return static_cast(n); -} static PosType toPos(int64_t n) { static_assert(sizeof(int64_t) == sizeof(PosType), "can't"); return static_cast(n); @@ -383,7 +379,7 @@ public: virtual Variant next() FOLLY_OVERRIDE; virtual Variant end() FOLLY_OVERRIDE; virtual Variant key() const FOLLY_OVERRIDE; - virtual Variant value(int32_t &pos) const FOLLY_OVERRIDE; + virtual Variant value(ssize_t &pos) const FOLLY_OVERRIDE; virtual Variant each() FOLLY_OVERRIDE; virtual bool isInvalid() const FOLLY_OVERRIDE { diff --git a/hphp/runtime/vm/name_value_table_wrapper.cpp b/hphp/runtime/vm/name_value_table_wrapper.cpp index 5f5621c05..58eff0905 100644 --- a/hphp/runtime/vm/name_value_table_wrapper.cpp +++ b/hphp/runtime/vm/name_value_table_wrapper.cpp @@ -252,7 +252,7 @@ Variant NameValueTableWrapper::key() const { return null_variant; } -Variant NameValueTableWrapper::value(int32_t& pos) const { +Variant NameValueTableWrapper::value(ssize_t& pos) const { return current(); } diff --git a/hphp/runtime/vm/name_value_table_wrapper.h b/hphp/runtime/vm/name_value_table_wrapper.h index 8f275bd01..b57ee6811 100644 --- a/hphp/runtime/vm/name_value_table_wrapper.h +++ b/hphp/runtime/vm/name_value_table_wrapper.h @@ -130,7 +130,7 @@ public: // ArrayData implementation virtual Variant end(); virtual CVarRef endRef(); virtual Variant key() const; - virtual Variant value(int32_t &pos) const; + virtual Variant value(ssize_t &pos) const; virtual Variant each(); virtual bool validFullPos(const FullPos & fp) const; virtual bool advanceFullPos(FullPos&); @@ -149,7 +149,7 @@ private: class GlobalNameValueTableWrapper : public NameValueTableWrapper { public: - explicit GlobalNameValueTableWrapper(NameValueTable* tab); + GlobalNameValueTableWrapper(NameValueTable* tab); Variant gvm_argc; Variant gvm_argv; Variant gvm__SERVER;