| 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 { | 60 struct PresentationAttributeCacheEntry FINAL : public NoBaseWillBeGarbageCollect
edFinalized<PresentationAttributeCacheEntry> { |
| 61 WTF_MAKE_FAST_ALLOCATED; | 61 WTF_MAKE_FAST_ALLOCATED_WILL_BE_REMOVED; |
| 62 public: | 62 public: |
| 63 void trace(Visitor* visitor) { visitor->trace(value); } |
| 64 |
| 63 PresentationAttributeCacheKey key; | 65 PresentationAttributeCacheKey key; |
| 64 RefPtr<StylePropertySet> value; | 66 RefPtrWillBeMember<StylePropertySet> value; |
| 65 }; | 67 }; |
| 66 | 68 |
| 67 typedef HashMap<unsigned, OwnPtr<PresentationAttributeCacheEntry>, AlreadyHashed
> PresentationAttributeCache; | 69 typedef WillBeHeapHashMap<unsigned, OwnPtrWillBeMember<PresentationAttributeCach
eEntry>, AlreadyHashed> PresentationAttributeCache; |
| 68 static PresentationAttributeCache& presentationAttributeCache() | 70 static PresentationAttributeCache& presentationAttributeCache() |
| 69 { | 71 { |
| 70 DEFINE_STATIC_LOCAL(PresentationAttributeCache, cache, ()); | 72 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<PresentationAttributeCache>, cach
e, (adoptPtrWillBeNoop(new PresentationAttributeCache()))); |
| 71 return cache; | 73 return *cache; |
| 72 } | 74 } |
| 73 | 75 |
| 74 class PresentationAttributeCacheCleaner { | 76 class PresentationAttributeCacheCleaner { |
| 75 WTF_MAKE_NONCOPYABLE(PresentationAttributeCacheCleaner); WTF_MAKE_FAST_ALLOC
ATED; | 77 WTF_MAKE_NONCOPYABLE(PresentationAttributeCacheCleaner); WTF_MAKE_FAST_ALLOC
ATED; |
| 76 public: | 78 public: |
| 77 PresentationAttributeCacheCleaner() | 79 PresentationAttributeCacheCleaner() |
| 78 : m_hitCount(0) | 80 : m_hitCount(0) |
| 79 , m_cleanTimer(this, &PresentationAttributeCacheCleaner::cleanCache) | 81 , m_cleanTimer(this, &PresentationAttributeCacheCleaner::cleanCache) |
| 80 { | 82 { |
| 81 } | 83 } |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 146 | 148 |
| 147 static unsigned computePresentationAttributeCacheHash(const PresentationAttribut
eCacheKey& key) | 149 static unsigned computePresentationAttributeCacheHash(const PresentationAttribut
eCacheKey& key) |
| 148 { | 150 { |
| 149 if (!key.tagName) | 151 if (!key.tagName) |
| 150 return 0; | 152 return 0; |
| 151 ASSERT(key.attributesAndValues.size()); | 153 ASSERT(key.attributesAndValues.size()); |
| 152 unsigned attributeHash = StringHasher::hashMemory(key.attributesAndValues.da
ta(), key.attributesAndValues.size() * sizeof(key.attributesAndValues[0])); | 154 unsigned attributeHash = StringHasher::hashMemory(key.attributesAndValues.da
ta(), key.attributesAndValues.size() * sizeof(key.attributesAndValues[0])); |
| 153 return WTF::pairIntHash(key.tagName->existingHash(), attributeHash); | 155 return WTF::pairIntHash(key.tagName->existingHash(), attributeHash); |
| 154 } | 156 } |
| 155 | 157 |
| 156 PassRefPtr<StylePropertySet> computePresentationAttributeStyle(Element& element) | 158 PassRefPtrWillBeRawPtr<StylePropertySet> computePresentationAttributeStyle(Eleme
nt& element) |
| 157 { | 159 { |
| 158 DEFINE_STATIC_LOCAL(PresentationAttributeCacheCleaner, cacheCleaner, ()); | 160 DEFINE_STATIC_LOCAL(PresentationAttributeCacheCleaner, cacheCleaner, ()); |
| 159 | 161 |
| 160 ASSERT(element.isStyledElement()); | 162 ASSERT(element.isStyledElement()); |
| 161 | 163 |
| 162 PresentationAttributeCacheKey cacheKey; | 164 PresentationAttributeCacheKey cacheKey; |
| 163 makePresentationAttributeCacheKey(element, cacheKey); | 165 makePresentationAttributeCacheKey(element, cacheKey); |
| 164 | 166 |
| 165 unsigned cacheHash = computePresentationAttributeCacheHash(cacheKey); | 167 unsigned cacheHash = computePresentationAttributeCacheHash(cacheKey); |
| 166 | 168 |
| 167 PresentationAttributeCache::ValueType* cacheValue; | 169 PresentationAttributeCache::ValueType* cacheValue; |
| 168 if (cacheHash) { | 170 if (cacheHash) { |
| 169 cacheValue = presentationAttributeCache().add(cacheHash, nullptr).stored
Value; | 171 cacheValue = presentationAttributeCache().add(cacheHash, nullptr).stored
Value; |
| 170 if (cacheValue->value && cacheValue->value->key != cacheKey) | 172 if (cacheValue->value && cacheValue->value->key != cacheKey) |
| 171 cacheHash = 0; | 173 cacheHash = 0; |
| 172 } else { | 174 } else { |
| 173 cacheValue = 0; | 175 cacheValue = 0; |
| 174 } | 176 } |
| 175 | 177 |
| 176 RefPtr<StylePropertySet> style; | 178 RefPtrWillBeRawPtr<StylePropertySet> style = nullptr; |
| 177 if (cacheHash && cacheValue->value) { | 179 if (cacheHash && cacheValue->value) { |
| 178 style = cacheValue->value->value; | 180 style = cacheValue->value->value; |
| 179 cacheCleaner.didHitPresentationAttributeCache(); | 181 cacheCleaner.didHitPresentationAttributeCache(); |
| 180 } else { | 182 } else { |
| 181 style = MutableStylePropertySet::create(element.isSVGElement() ? SVGAttr
ibuteMode : HTMLAttributeMode); | 183 style = MutableStylePropertySet::create(element.isSVGElement() ? SVGAttr
ibuteMode : HTMLAttributeMode); |
| 182 AttributeCollection attributes = element.attributes(); | 184 AttributeCollection attributes = element.attributes(); |
| 183 AttributeCollection::const_iterator end = attributes.end(); | 185 AttributeCollection::const_iterator end = attributes.end(); |
| 184 for (AttributeCollection::const_iterator it = attributes.begin(); it !=
end; ++it) | 186 for (AttributeCollection::const_iterator it = attributes.begin(); it !=
end; ++it) |
| 185 element.collectStyleForPresentationAttribute(it->name(), it->value()
, toMutableStylePropertySet(style)); | 187 element.collectStyleForPresentationAttribute(it->name(), it->value()
, toMutableStylePropertySet(style)); |
| 186 } | 188 } |
| 187 | 189 |
| 188 if (!cacheHash || cacheValue->value) | 190 if (!cacheHash || cacheValue->value) |
| 189 return style.release(); | 191 return style.release(); |
| 190 | 192 |
| 191 OwnPtr<PresentationAttributeCacheEntry> newEntry = adoptPtr(new Presentation
AttributeCacheEntry); | 193 OwnPtrWillBeRawPtr<PresentationAttributeCacheEntry> newEntry = adoptPtrWillB
eNoop(new PresentationAttributeCacheEntry); |
| 192 newEntry->key = cacheKey; | 194 newEntry->key = cacheKey; |
| 193 newEntry->value = style; | 195 newEntry->value = style; |
| 194 | 196 |
| 195 static const unsigned presentationAttributeCacheMaximumSize = 4096; | 197 static const unsigned presentationAttributeCacheMaximumSize = 4096; |
| 196 if (presentationAttributeCache().size() > presentationAttributeCacheMaximumS
ize) { | 198 if (presentationAttributeCache().size() > presentationAttributeCacheMaximumS
ize) { |
| 197 // FIXME: Discarding the entire cache when it gets too big is probably b
ad | 199 // 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? | 200 // since it creates a perf "cliff". Perhaps we should use an LRU? |
| 199 presentationAttributeCache().clear(); | 201 presentationAttributeCache().clear(); |
| 200 presentationAttributeCache().set(cacheHash, newEntry.release()); | 202 presentationAttributeCache().set(cacheHash, newEntry.release()); |
| 201 } else { | 203 } else { |
| 202 cacheValue->value = newEntry.release(); | 204 cacheValue->value = newEntry.release(); |
| 203 } | 205 } |
| 204 | 206 |
| 205 return style.release(); | 207 return style.release(); |
| 206 } | 208 } |
| 207 | 209 |
| 208 } // namespace WebCore | 210 } // namespace WebCore |
| OLD | NEW |