| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 Vector<std::pair<StringImpl*, AtomicString>, 3> attributesAndValues; | 50 Vector<std::pair<StringImpl*, AtomicString>, 3> attributesAndValues; |
| 51 }; | 51 }; |
| 52 | 52 |
| 53 static bool operator!=(const PresentationAttributeCacheKey& a, const Presentatio
nAttributeCacheKey& b) | 53 static bool operator!=(const PresentationAttributeCacheKey& a, const Presentatio
nAttributeCacheKey& b) |
| 54 { | 54 { |
| 55 if (a.tagName != b.tagName) | 55 if (a.tagName != b.tagName) |
| 56 return true; | 56 return true; |
| 57 return a.attributesAndValues != b.attributesAndValues; | 57 return a.attributesAndValues != b.attributesAndValues; |
| 58 } | 58 } |
| 59 | 59 |
| 60 struct PresentationAttributeCacheEntry final : public NoBaseWillBeGarbageCollect
edFinalized<PresentationAttributeCacheEntry> { | 60 struct PresentationAttributeCacheEntry final : public GarbageCollectedFinalized<
PresentationAttributeCacheEntry> { |
| 61 USING_FAST_MALLOC_WILL_BE_REMOVED(PresentationAttributeCacheEntry); | |
| 62 public: | 61 public: |
| 63 DEFINE_INLINE_TRACE() { visitor->trace(value); } | 62 DEFINE_INLINE_TRACE() { visitor->trace(value); } |
| 64 | 63 |
| 65 PresentationAttributeCacheKey key; | 64 PresentationAttributeCacheKey key; |
| 66 RefPtrWillBeMember<StylePropertySet> value; | 65 Member<StylePropertySet> value; |
| 67 }; | 66 }; |
| 68 | 67 |
| 69 using PresentationAttributeCache = WillBeHeapHashMap<unsigned, OwnPtrWillBeMembe
r<PresentationAttributeCacheEntry>, AlreadyHashed>; | 68 using PresentationAttributeCache = HeapHashMap<unsigned, Member<PresentationAttr
ibuteCacheEntry>, AlreadyHashed>; |
| 70 static PresentationAttributeCache& presentationAttributeCache() | 69 static PresentationAttributeCache& presentationAttributeCache() |
| 71 { | 70 { |
| 72 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<PresentationAttributeCache>, cach
e, (adoptPtrWillBeNoop(new PresentationAttributeCache()))); | 71 DEFINE_STATIC_LOCAL(Persistent<PresentationAttributeCache>, cache, (new Pres
entationAttributeCache())); |
| 73 return *cache; | 72 return *cache; |
| 74 } | 73 } |
| 75 | 74 |
| 76 class PresentationAttributeCacheCleaner { | 75 class PresentationAttributeCacheCleaner { |
| 77 WTF_MAKE_NONCOPYABLE(PresentationAttributeCacheCleaner); USING_FAST_MALLOC(P
resentationAttributeCacheCleaner); | 76 WTF_MAKE_NONCOPYABLE(PresentationAttributeCacheCleaner); USING_FAST_MALLOC(P
resentationAttributeCacheCleaner); |
| 78 public: | 77 public: |
| 79 PresentationAttributeCacheCleaner() | 78 PresentationAttributeCacheCleaner() |
| 80 : m_hitCount(0) | 79 : m_hitCount(0) |
| 81 , m_cleanTimer(this, &PresentationAttributeCacheCleaner::cleanCache) | 80 , m_cleanTimer(this, &PresentationAttributeCacheCleaner::cleanCache) |
| 82 { | 81 { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 146 |
| 148 static unsigned computePresentationAttributeCacheHash(const PresentationAttribut
eCacheKey& key) | 147 static unsigned computePresentationAttributeCacheHash(const PresentationAttribut
eCacheKey& key) |
| 149 { | 148 { |
| 150 if (!key.tagName) | 149 if (!key.tagName) |
| 151 return 0; | 150 return 0; |
| 152 ASSERT(key.attributesAndValues.size()); | 151 ASSERT(key.attributesAndValues.size()); |
| 153 unsigned attributeHash = StringHasher::hashMemory(key.attributesAndValues.da
ta(), key.attributesAndValues.size() * sizeof(key.attributesAndValues[0])); | 152 unsigned attributeHash = StringHasher::hashMemory(key.attributesAndValues.da
ta(), key.attributesAndValues.size() * sizeof(key.attributesAndValues[0])); |
| 154 return WTF::hashInts(key.tagName->existingHash(), attributeHash); | 153 return WTF::hashInts(key.tagName->existingHash(), attributeHash); |
| 155 } | 154 } |
| 156 | 155 |
| 157 PassRefPtrWillBeRawPtr<StylePropertySet> computePresentationAttributeStyle(Eleme
nt& element) | 156 RawPtr<StylePropertySet> computePresentationAttributeStyle(Element& element) |
| 158 { | 157 { |
| 159 DEFINE_STATIC_LOCAL(PresentationAttributeCacheCleaner, cacheCleaner, ()); | 158 DEFINE_STATIC_LOCAL(PresentationAttributeCacheCleaner, cacheCleaner, ()); |
| 160 | 159 |
| 161 ASSERT(element.isStyledElement()); | 160 ASSERT(element.isStyledElement()); |
| 162 | 161 |
| 163 PresentationAttributeCacheKey cacheKey; | 162 PresentationAttributeCacheKey cacheKey; |
| 164 makePresentationAttributeCacheKey(element, cacheKey); | 163 makePresentationAttributeCacheKey(element, cacheKey); |
| 165 | 164 |
| 166 unsigned cacheHash = computePresentationAttributeCacheHash(cacheKey); | 165 unsigned cacheHash = computePresentationAttributeCacheHash(cacheKey); |
| 167 | 166 |
| 168 PresentationAttributeCache::ValueType* cacheValue; | 167 PresentationAttributeCache::ValueType* cacheValue; |
| 169 if (cacheHash) { | 168 if (cacheHash) { |
| 170 cacheValue = presentationAttributeCache().add(cacheHash, nullptr).stored
Value; | 169 cacheValue = presentationAttributeCache().add(cacheHash, nullptr).stored
Value; |
| 171 if (cacheValue->value && cacheValue->value->key != cacheKey) | 170 if (cacheValue->value && cacheValue->value->key != cacheKey) |
| 172 cacheHash = 0; | 171 cacheHash = 0; |
| 173 } else { | 172 } else { |
| 174 cacheValue = nullptr; | 173 cacheValue = nullptr; |
| 175 } | 174 } |
| 176 | 175 |
| 177 RefPtrWillBeRawPtr<StylePropertySet> style = nullptr; | 176 RawPtr<StylePropertySet> style = nullptr; |
| 178 if (cacheHash && cacheValue->value) { | 177 if (cacheHash && cacheValue->value) { |
| 179 style = cacheValue->value->value; | 178 style = cacheValue->value->value; |
| 180 cacheCleaner.didHitPresentationAttributeCache(); | 179 cacheCleaner.didHitPresentationAttributeCache(); |
| 181 } else { | 180 } else { |
| 182 style = MutableStylePropertySet::create(element.isSVGElement() ? SVGAttr
ibuteMode : HTMLAttributeMode); | 181 style = MutableStylePropertySet::create(element.isSVGElement() ? SVGAttr
ibuteMode : HTMLAttributeMode); |
| 183 AttributeCollection attributes = element.attributesWithoutUpdate(); | 182 AttributeCollection attributes = element.attributesWithoutUpdate(); |
| 184 for (const Attribute& attr : attributes) | 183 for (const Attribute& attr : attributes) |
| 185 element.collectStyleForPresentationAttribute(attr.name(), attr.value
(), toMutableStylePropertySet(style)); | 184 element.collectStyleForPresentationAttribute(attr.name(), attr.value
(), toMutableStylePropertySet(style)); |
| 186 } | 185 } |
| 187 | 186 |
| 188 if (!cacheHash || cacheValue->value) | 187 if (!cacheHash || cacheValue->value) |
| 189 return style.release(); | 188 return style.release(); |
| 190 | 189 |
| 191 OwnPtrWillBeRawPtr<PresentationAttributeCacheEntry> newEntry = adoptPtrWillB
eNoop(new PresentationAttributeCacheEntry); | 190 RawPtr<PresentationAttributeCacheEntry> newEntry = adoptPtrWillBeNoop(new Pr
esentationAttributeCacheEntry); |
| 192 newEntry->key = cacheKey; | 191 newEntry->key = cacheKey; |
| 193 newEntry->value = style; | 192 newEntry->value = style; |
| 194 | 193 |
| 195 static const unsigned presentationAttributeCacheMaximumSize = 4096; | 194 static const unsigned presentationAttributeCacheMaximumSize = 4096; |
| 196 if (presentationAttributeCache().size() > presentationAttributeCacheMaximumS
ize) { | 195 if (presentationAttributeCache().size() > presentationAttributeCacheMaximumS
ize) { |
| 197 // FIXME: Discarding the entire cache when it gets too big is probably b
ad | 196 // FIXME: Discarding the entire cache when it gets too big is probably b
ad |
| 198 // since it creates a perf "cliff". Perhaps we should use an LRU? | 197 // since it creates a perf "cliff". Perhaps we should use an LRU? |
| 199 presentationAttributeCache().clear(); | 198 presentationAttributeCache().clear(); |
| 200 presentationAttributeCache().set(cacheHash, newEntry.release()); | 199 presentationAttributeCache().set(cacheHash, newEntry.release()); |
| 201 } else { | 200 } else { |
| 202 cacheValue->value = newEntry.release(); | 201 cacheValue->value = newEntry.release(); |
| 203 } | 202 } |
| 204 | 203 |
| 205 return style.release(); | 204 return style.release(); |
| 206 } | 205 } |
| 207 | 206 |
| 208 } // namespace blink | 207 } // namespace blink |
| OLD | NEW |