Bug 1331718 - Part 1: Add small pointer array. r=froydnj
authorBas Schouten <bschouten@mozilla.com>
Wed, 10 May 2017 05:07:37 +0200
changeset 357572 c4aec03e97401c5ed8c23988c81383f28edf0c2a
parent 357571 129ab771d0ad9da75e3786bf868161ddf3959e8e
child 357573 e18a94a1ffda717e908abb530bf91ee9f28fe593
push id90158
push userbschouten@mozilla.com
push dateWed, 10 May 2017 20:34:50 +0000
treeherdermozilla-inbound@fa38c52a84a5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfroydnj
bugs1331718
milestone55.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1331718 - Part 1: Add small pointer array. r=froydnj MozReview-Commit-ID: EapU5nBw1d3
mfbt/SmallPointerArray.h
mfbt/moz.build
new file mode 100644
--- /dev/null
+++ b/mfbt/SmallPointerArray.h
@@ -0,0 +1,215 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* vim: set ts=8 sts=2 et sw=2 tw=80: */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+* License, v. 2.0. If a copy of the MPL was not distributed with this
+* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* A vector of pointers space-optimized for a small number of elements. */
+#ifndef mozilla_SmallPointerArray_h
+#define mozilla_SmallPointerArray_h
+
+#include "mozilla/Assertions.h"
+#include <iterator>
+#include <vector>
+
+namespace mozilla {
+
+// Array class for situations where a small number of elements (<= 2) is
+// expected, a large number of elements must be accomodated if necessary,
+// and the size of the class must be minimal. Typical vector implementations
+// will fulfill the first two requirements by simply adding inline storage
+// alongside the rest of their member variables. While this strategy works,
+// it brings unnecessary storage overhead for vectors with an expected small
+// number of elements. This class is intended to deal with that problem.
+//
+// This class is similar in performance to a vector class. Accessing its
+// elements when it has not grown over a size of 2 does not require an extra
+// level of indirection and will therefore be faster.
+//
+// The minimum (inline) size is 2 * sizeof(void*).
+//
+// Any modification of the array invalidates any outstanding iterators.
+template<typename T>
+class SmallPointerArray
+{
+public:
+  SmallPointerArray()
+  {
+    mInlineElements[0] = mInlineElements[1] = nullptr;
+    static_assert(sizeof(SmallPointerArray<T>) == (2 * sizeof(void*)),
+      "SmallPointerArray must compile to the size of 2 pointers");
+    static_assert(offsetof(SmallPointerArray<T>, mArray) ==
+                  offsetof(SmallPointerArray<T>, mInlineElements) + sizeof(T*),
+      "mArray and mInlineElements[1] are expected to overlap in memory");
+    static_assert(offsetof(SmallPointerArray<T>, mPadding) ==
+      offsetof(SmallPointerArray<T>, mInlineElements),
+      "mPadding and mInlineElements[0] are expected to overlap in memory");
+  }
+  ~SmallPointerArray()
+  {
+    if (!mInlineElements[0] && mArray) {
+      delete mArray;
+    }
+  }
+
+  void Clear() {
+    if (!mInlineElements[0] && mArray) {
+      delete mArray;
+      mArray = nullptr;
+      return;
+    }
+    mInlineElements[0] = mInlineElements[1] = nullptr;
+  }
+
+  void AppendElement(T* aElement) {
+    // Storing nullptr as an element is not permitted, but we do check for it
+    // to avoid corruption issues in non-debug builds.
+
+    // In addition to this we assert in debug builds to point out mistakes to
+    // users of the class.
+    MOZ_ASSERT(aElement != nullptr);
+    if (!mInlineElements[0]) {
+      if (!mArray) {
+        mInlineElements[0] = aElement;
+        // Harmless if aElement == nullptr;
+        return;
+      }
+
+      if (!aElement) {
+        return;
+      }
+
+      mArray->push_back(aElement);
+      return;
+    }
+
+    if (!aElement) {
+      return;
+    }
+
+    if (!mInlineElements[1]) {
+      mInlineElements[1] = aElement;
+      return;
+    }
+
+    mArray = new std::vector<T*>({ mInlineElements[0], mInlineElements[1], aElement });
+    mInlineElements[0] = nullptr;
+  }
+
+  void RemoveElement(T* aElement) {
+    MOZ_ASSERT(aElement != nullptr);
+    if (aElement == nullptr) {
+      return;
+    }
+
+    if (mInlineElements[0] == aElement) {
+      // Expectected case.
+      mInlineElements[0] = mInlineElements[1];
+      mInlineElements[1] = nullptr;
+      return;
+    }
+
+    if (mInlineElements[0]) {
+      if (mInlineElements[1] == aElement) {
+        mInlineElements[1] = nullptr;
+      }
+      return;
+    }
+
+    if (mArray) {
+      for (auto iter = mArray->begin(); iter != mArray->end(); iter++) {
+        if (*iter == aElement) {
+          mArray->erase(iter);
+          return;
+        }
+      }
+    }
+  }
+
+  size_t Length() const
+  {
+    if (mInlineElements[0]) {
+      if (!mInlineElements[1]) {
+        return 1;
+      }
+      return 2;
+    }
+
+    if (mArray) {
+      return mArray->size();
+    }
+
+    return 0;
+  }
+
+  T* ElementAt(size_t aIndex) const {
+    MOZ_ASSERT(aIndex < Length());
+    if (mInlineElements[0]) {
+      return mInlineElements[aIndex];
+    }
+
+    return (*mArray)[aIndex];
+  }
+
+  T* operator[](size_t aIndex) const
+  {
+    return ElementAt(aIndex);
+  }
+
+  typedef T**                        iterator;
+  typedef const T**                  const_iterator;
+
+  // Methods for range-based for loops. Manipulation invalidates these.
+  iterator begin() {
+    return beginInternal();
+  }
+  const_iterator begin() const {
+    return beginInternal();
+  }
+  const_iterator cbegin() const { return begin(); }
+  iterator end() {
+    return beginInternal() + Length();
+  }
+  const_iterator end() const {
+    return beginInternal() + Length();
+  }
+  const_iterator cend() const { return end(); }
+
+private:
+  T** beginInternal() const {
+    if (mInlineElements[0] || !mArray) {
+      return const_cast<T**>(&mInlineElements[0]);
+    }
+
+    if (!mArray->size()) {
+      return nullptr;
+    }
+
+    return &(*mArray)[0];
+  }
+
+  // mArray and mInlineElements[1] share the same area in memory.
+  //
+  // When !mInlineElements[0] && !mInlineElements[1] the array is empty.
+  //
+  // When mInlineElements[0] && !mInlineElements[1], mInlineElements[0]
+  // contains the first element. The array is of size 1.
+  //
+  // When mInlineElements[0] && mInlineElements[1], mInlineElements[0]
+  // contains the first element and mInlineElements[1] the second. The
+  // array is of size 2.
+  //
+  // When !mInlineElements[0] && mArray, mArray contains the full contents
+  // of the array and is of arbitrary size.
+  union {
+    T* mInlineElements[2];
+    struct {
+      void* mPadding;
+      std::vector<T*>* mArray;
+    };
+  };
+};
+
+} // namespace mozilla
+
+#endif // mozilla_SmallPointerArray_h
--- a/mfbt/moz.build
+++ b/mfbt/moz.build
@@ -78,16 +78,17 @@ EXPORTS.mozilla = [
     'ReverseIterator.h',
     'RollingMean.h',
     'Saturate.h',
     'Scoped.h',
     'ScopeExit.h',
     'SegmentedVector.h',
     'SHA1.h',
     'SizePrintfMacros.h',
+    'SmallPointerArray.h',
     'Span.h',
     'SplayTree.h',
     'Sprintf.h',
     'StaticAnalysisFunctions.h',
     'TaggedAnonymousMemory.h',
     'TemplateLib.h',
     'ThreadLocal.h',
     'ToString.h',