2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 2004-2005 Allan Sandfeld Jensen (kde@carewolf.com)
4 * Copyright (C) 2006, 2007 Nicholas Shanks (webkit@nickshanks.com)
5 * Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
6 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
7 * Copyright (C) 2007, 2008 Eric Seidel <eric@webkit.org>
8 * Copyright (C) 2008, 2009 Torch Mobile Inc. All rights reserved. (http://www.torchmobile.com/)
9 * Copyright (c) 2011, Code Aurora Forum. All rights reserved.
10 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
11 * Copyright (C) 2013 Google Inc. All rights reserved.
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Library General Public
15 * License as published by the Free Software Foundation; either
16 * version 2 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Library General Public License for more details.
23 * You should have received a copy of the GNU Library General Public License
24 * along with this library; see the file COPYING.LIB. If not, write to
25 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
30 #include "core/css/resolver/MatchedPropertiesCache.h"
32 #include "core/css/StylePropertySet.h"
33 #include "core/css/resolver/StyleResolverState.h"
34 #include "core/rendering/style/RenderStyle.h"
39 bool CachedMatchedPropertiesHashTraits::traceInCollection(Visitor* visitor, Member<CachedMatchedProperties>& cachedProperties, WTF::ShouldWeakPointersBeMarkedStrongly strongify)
41 // Only honor the cache's weakness semantics if the collection is traced
42 // with WeakPointersActWeak. Otherwise just trace the cachedProperties
43 // strongly, ie. call trace on it.
44 if (cachedProperties && strongify == WTF::WeakPointersActWeak) {
45 // A given cache entry is only kept alive if none of the MatchedProperties
46 // in the CachedMatchedProperties value contain a dead "properties" field.
47 // If there is a dead field the entire cache entry is removed.
48 for (const auto& matchedProperties : cachedProperties->matchedProperties) {
49 if (!visitor->isAlive(matchedProperties.properties)) {
50 // For now report the cache entry as dead. This might not
51 // be the final result if in a subsequent call for this entry,
52 // the "properties" field has been marked via another path.
57 // At this point none of the entries in the matchedProperties vector
58 // had a dead "properties" field so trace CachedMatchedProperties strongly.
59 visitor->trace(cachedProperties);
64 void CachedMatchedProperties::set(const RenderStyle* style, const RenderStyle* parentStyle, const MatchResult& matchResult)
66 matchedProperties.appendVector(matchResult.matchedProperties);
67 ranges = matchResult.ranges;
69 // Note that we don't cache the original RenderStyle instance. It may be further modified.
70 // The RenderStyle in the cache is really just a holder for the substructures and never used as-is.
71 this->renderStyle = RenderStyle::clone(style);
72 this->parentRenderStyle = RenderStyle::clone(parentStyle);
75 void CachedMatchedProperties::clear()
77 matchedProperties.clear();
78 renderStyle = nullptr;
79 parentRenderStyle = nullptr;
82 MatchedPropertiesCache::MatchedPropertiesCache()
84 : m_additionsSinceLastSweep(0)
85 , m_sweepTimer(this, &MatchedPropertiesCache::sweep)
90 const CachedMatchedProperties* MatchedPropertiesCache::find(unsigned hash, const StyleResolverState& styleResolverState, const MatchResult& matchResult)
94 Cache::iterator it = m_cache.find(hash);
95 if (it == m_cache.end())
97 CachedMatchedProperties* cacheItem = it->value.get();
100 size_t size = matchResult.matchedProperties.size();
101 if (size != cacheItem->matchedProperties.size())
103 if (cacheItem->renderStyle->insideLink() != styleResolverState.style()->insideLink())
105 for (size_t i = 0; i < size; ++i) {
106 if (matchResult.matchedProperties[i] != cacheItem->matchedProperties[i])
109 if (cacheItem->ranges != matchResult.ranges)
114 void MatchedPropertiesCache::add(const RenderStyle* style, const RenderStyle* parentStyle, unsigned hash, const MatchResult& matchResult)
117 static const unsigned maxAdditionsBetweenSweeps = 100;
118 if (++m_additionsSinceLastSweep >= maxAdditionsBetweenSweeps
119 && !m_sweepTimer.isActive()) {
120 static const unsigned sweepTimeInSeconds = 60;
121 m_sweepTimer.startOneShot(sweepTimeInSeconds, FROM_HERE);
126 Cache::AddResult addResult = m_cache.add(hash, nullptr);
127 if (addResult.isNewEntry)
128 addResult.storedValue->value = adoptPtrWillBeNoop(new CachedMatchedProperties);
130 CachedMatchedProperties* cacheItem = addResult.storedValue->value.get();
131 if (!addResult.isNewEntry)
134 cacheItem->set(style, parentStyle, matchResult);
137 void MatchedPropertiesCache::clear()
142 void MatchedPropertiesCache::clearViewportDependent()
144 Vector<unsigned, 16> toRemove;
145 for (const auto& cacheEntry : m_cache) {
146 CachedMatchedProperties* cacheItem = cacheEntry.value.get();
147 if (cacheItem->renderStyle->hasViewportUnits())
148 toRemove.append(cacheEntry.key);
150 m_cache.removeAll(toRemove);
154 void MatchedPropertiesCache::sweep(Timer<MatchedPropertiesCache>*)
156 // Look for cache entries containing a style declaration with a single ref and remove them.
157 // This may happen when an element attribute mutation causes it to generate a new inlineStyle()
158 // or presentationAttributeStyle(), potentially leaving this cache with the last ref on the old one.
159 Vector<unsigned, 16> toRemove;
160 for (const auto& cacheEntry : m_cache) {
161 CachedMatchedProperties* cacheItem = cacheEntry.value.get();
162 Vector<MatchedProperties>& matchedProperties = cacheItem->matchedProperties;
163 for (size_t i = 0; i < matchedProperties.size(); ++i) {
164 if (matchedProperties[i].properties->hasOneRef()) {
165 toRemove.append(cacheEntry.key);
170 m_cache.removeAll(toRemove);
171 m_additionsSinceLastSweep = 0;
175 bool MatchedPropertiesCache::isCacheable(const Element* element, const RenderStyle* style, const RenderStyle* parentStyle)
177 // FIXME: CSSPropertyWebkitWritingMode modifies state when applying to document element. We can't skip the applying by caching.
178 if (element == element->document().documentElement() && element->document().writingModeSetOnDocumentElement())
180 if (style->unique() || (style->styleType() != NOPSEUDO && parentStyle->unique()))
182 if (style->hasAppearance())
184 if (style->zoom() != RenderStyle::initialZoom())
186 if (style->writingMode() != RenderStyle::initialWritingMode())
188 // The cache assumes static knowledge about which properties are inherited.
189 if (parentStyle->hasExplicitlyInheritedProperties())
194 void MatchedPropertiesCache::trace(Visitor* visitor)
197 visitor->trace(m_cache);