| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2012,2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2012,2013 Google Inc. All rights reserved. |
| 3 * Copyright (C) 2014 Apple Inc. All rights reserved. | 3 * Copyright (C) 2014 Apple Inc. All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 visitor->trace(m_cachedList); | 53 visitor->trace(m_cachedList); |
| 54 Base::trace(visitor); | 54 Base::trace(visitor); |
| 55 } | 55 } |
| 56 | 56 |
| 57 unsigned nodeCount(const Collection&); | 57 unsigned nodeCount(const Collection&); |
| 58 NodeType* nodeAt(const Collection&, unsigned index); | 58 NodeType* nodeAt(const Collection&, unsigned index); |
| 59 void invalidate(); | 59 void invalidate(); |
| 60 | 60 |
| 61 private: | 61 private: |
| 62 ptrdiff_t allocationSize() const { return m_cachedList.capacity() * sizeof(N
odeType*); } | 62 ptrdiff_t allocationSize() const { return m_cachedList.capacity() * sizeof(N
odeType*); } |
| 63 static void reportExtraMemoryCostForCollectionItemsCache(ptrdiff_t diff) | |
| 64 { | |
| 65 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(diff); | |
| 66 } | |
| 67 | 63 |
| 68 bool m_listValid; | 64 bool m_listValid; |
| 69 WillBeHeapVector<RawPtrWillBeMember<NodeType>> m_cachedList; | 65 WillBeHeapVector<RawPtrWillBeMember<NodeType>> m_cachedList; |
| 70 }; | 66 }; |
| 71 | 67 |
| 72 template <typename Collection, typename NodeType> | 68 template <typename Collection, typename NodeType> |
| 73 CollectionItemsCache<Collection, NodeType>::CollectionItemsCache() | 69 CollectionItemsCache<Collection, NodeType>::CollectionItemsCache() |
| 74 : m_listValid(false) | 70 : m_listValid(false) |
| 75 { | 71 { |
| 76 } | 72 } |
| 77 | 73 |
| 78 template <typename Collection, typename NodeType> | 74 template <typename Collection, typename NodeType> |
| 79 CollectionItemsCache<Collection, NodeType>::~CollectionItemsCache() | 75 CollectionItemsCache<Collection, NodeType>::~CollectionItemsCache() |
| 80 { | 76 { |
| 81 if (ptrdiff_t diff = allocationSize()) | |
| 82 reportExtraMemoryCostForCollectionItemsCache(-diff); | |
| 83 } | 77 } |
| 84 | 78 |
| 85 template <typename Collection, typename NodeType> | 79 template <typename Collection, typename NodeType> |
| 86 void CollectionItemsCache<Collection, NodeType>::invalidate() | 80 void CollectionItemsCache<Collection, NodeType>::invalidate() |
| 87 { | 81 { |
| 88 Base::invalidate(); | 82 Base::invalidate(); |
| 89 if (m_listValid) { | 83 if (m_listValid) { |
| 90 m_cachedList.shrink(0); | 84 m_cachedList.shrink(0); |
| 91 m_listValid = false; | 85 m_listValid = false; |
| 92 } | 86 } |
| 93 } | 87 } |
| 94 | 88 |
| 95 template <class Collection, class NodeType> | 89 template <class Collection, class NodeType> |
| 96 unsigned CollectionItemsCache<Collection, NodeType>::nodeCount(const Collection&
collection) | 90 unsigned CollectionItemsCache<Collection, NodeType>::nodeCount(const Collection&
collection) |
| 97 { | 91 { |
| 98 if (this->isCachedNodeCountValid()) | 92 if (this->isCachedNodeCountValid()) |
| 99 return this->cachedNodeCount(); | 93 return this->cachedNodeCount(); |
| 100 | 94 |
| 101 NodeType* currentNode = collection.traverseToFirst(); | 95 NodeType* currentNode = collection.traverseToFirst(); |
| 102 unsigned currentIndex = 0; | 96 unsigned currentIndex = 0; |
| 103 ptrdiff_t oldCapacity = allocationSize(); | |
| 104 while (currentNode) { | 97 while (currentNode) { |
| 105 m_cachedList.append(currentNode); | 98 m_cachedList.append(currentNode); |
| 106 currentNode = collection.traverseForwardToOffset(currentIndex + 1, *curr
entNode, currentIndex); | 99 currentNode = collection.traverseForwardToOffset(currentIndex + 1, *curr
entNode, currentIndex); |
| 107 } | 100 } |
| 108 if (ptrdiff_t diff = allocationSize() - oldCapacity) | |
| 109 reportExtraMemoryCostForCollectionItemsCache(diff); | |
| 110 | 101 |
| 111 this->setCachedNodeCount(m_cachedList.size()); | 102 this->setCachedNodeCount(m_cachedList.size()); |
| 112 m_listValid = true; | 103 m_listValid = true; |
| 113 return this->cachedNodeCount(); | 104 return this->cachedNodeCount(); |
| 114 } | 105 } |
| 115 | 106 |
| 116 template <typename Collection, typename NodeType> | 107 template <typename Collection, typename NodeType> |
| 117 inline NodeType* CollectionItemsCache<Collection, NodeType>::nodeAt(const Collec
tion& collection, unsigned index) | 108 inline NodeType* CollectionItemsCache<Collection, NodeType>::nodeAt(const Collec
tion& collection, unsigned index) |
| 118 { | 109 { |
| 119 if (m_listValid) { | 110 if (m_listValid) { |
| 120 ASSERT(this->isCachedNodeCountValid()); | 111 ASSERT(this->isCachedNodeCountValid()); |
| 121 return index < this->cachedNodeCount() ? m_cachedList[index] : nullptr; | 112 return index < this->cachedNodeCount() ? m_cachedList[index] : nullptr; |
| 122 } | 113 } |
| 123 return Base::nodeAt(collection, index); | 114 return Base::nodeAt(collection, index); |
| 124 } | 115 } |
| 125 | 116 |
| 126 } // namespace blink | 117 } // namespace blink |
| 127 | 118 |
| 128 #endif // CollectionItemsCache_h | 119 #endif // CollectionItemsCache_h |
| OLD | NEW |