CachedInheritingStyles.h (1964B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_CachedInheritingStyles_h 8 #define mozilla_CachedInheritingStyles_h 9 10 #include "nsAtom.h" 11 #include "nsCOMPtr.h" 12 #include "nsTArray.h" 13 14 class nsWindowSizes; 15 16 namespace mozilla { 17 18 enum class PseudoStyleType : uint8_t; 19 class ComputedStyle; 20 21 // Cache of anonymous box and lazy pseudo styles that inherit from a given 22 // style. 23 // 24 // To minimize memory footprint, the cache is word-sized with a tagged pointer 25 // If there is only one entry, it's stored inline. If there are more, they're 26 // stored in an out-of-line buffer. See bug 1429126 comment 0 and comment 1 for 27 // the measurements and rationale that influenced the design. 28 class CachedInheritingStyles { 29 public: 30 void Insert(ComputedStyle* aStyle); 31 ComputedStyle* Lookup(PseudoStyleType) const; 32 33 CachedInheritingStyles() : mBits(0) {} 34 ~CachedInheritingStyles() { 35 if (IsIndirect()) { 36 delete AsIndirect(); 37 } else if (!IsEmpty()) { 38 RefPtr<ComputedStyle> ref = dont_AddRef(AsDirect()); 39 } 40 } 41 42 void AddSizeOfIncludingThis(nsWindowSizes& aSizes, size_t* aCVsSize) const; 43 44 private: 45 // See bug 1429126 comment 1 for the choice of four here. 46 typedef AutoTArray<RefPtr<ComputedStyle>, 4> IndirectCache; 47 48 bool IsEmpty() const { return !mBits; } 49 bool IsIndirect() const { return (mBits & 1); } 50 51 ComputedStyle* AsDirect() const { 52 MOZ_ASSERT(!IsIndirect()); 53 return reinterpret_cast<ComputedStyle*>(mBits); 54 } 55 56 IndirectCache* AsIndirect() const { 57 MOZ_ASSERT(IsIndirect()); 58 return reinterpret_cast<IndirectCache*>(mBits & ~1); 59 } 60 61 uintptr_t mBits; 62 }; 63 64 } // namespace mozilla 65 66 #endif // mozilla_CachedInheritingStyles_h