OLD | NEW |
1 /* | 1 /* |
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) | 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) |
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) | 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) |
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) | 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) |
5 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. | 5 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
6 | 6 |
7 This library is free software; you can redistribute it and/or | 7 This library is free software; you can redistribute it and/or |
8 modify it under the terms of the GNU Library General Public | 8 modify it under the terms of the GNU Library General Public |
9 License as published by the Free Software Foundation; either | 9 License as published by the Free Software Foundation; either |
10 version 2 of the License, or (at your option) any later version. | 10 version 2 of the License, or (at your option) any later version. |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 gMemoryCache = memoryCache; | 64 gMemoryCache = memoryCache; |
65 } | 65 } |
66 | 66 |
67 MemoryCache::MemoryCache() | 67 MemoryCache::MemoryCache() |
68 : m_inPruneResources(false) | 68 : m_inPruneResources(false) |
69 , m_capacity(cDefaultCacheCapacity) | 69 , m_capacity(cDefaultCacheCapacity) |
70 , m_minDeadCapacity(0) | 70 , m_minDeadCapacity(0) |
71 , m_maxDeadCapacity(cDefaultCacheCapacity) | 71 , m_maxDeadCapacity(cDefaultCacheCapacity) |
72 , m_liveSize(0) | 72 , m_liveSize(0) |
73 , m_deadSize(0) | 73 , m_deadSize(0) |
| 74 , m_delayBeforeLiveDecodedPrune(cMinDelayBeforeLiveDecodedPrune) |
74 #ifdef MEMORY_CACHE_STATS | 75 #ifdef MEMORY_CACHE_STATS |
75 , m_statsTimer(this, &MemoryCache::dumpStats) | 76 , m_statsTimer(this, &MemoryCache::dumpStats) |
76 #endif | 77 #endif |
77 { | 78 { |
78 #ifdef MEMORY_CACHE_STATS | 79 #ifdef MEMORY_CACHE_STATS |
79 const double statsIntervalInSeconds = 15; | 80 const double statsIntervalInSeconds = 15; |
80 m_statsTimer.startRepeating(statsIntervalInSeconds); | 81 m_statsTimer.startRepeating(statsIntervalInSeconds); |
81 #endif | 82 #endif |
82 } | 83 } |
83 | 84 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 if (capacity && m_liveSize <= capacity) | 154 if (capacity && m_liveSize <= capacity) |
154 return; | 155 return; |
155 | 156 |
156 unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentag
e); // Cut by a percentage to avoid immediately pruning again. | 157 unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentag
e); // Cut by a percentage to avoid immediately pruning again. |
157 | 158 |
158 double currentTime = FrameView::currentPaintTimeStamp(); | 159 double currentTime = FrameView::currentPaintTimeStamp(); |
159 if (!currentTime) // In case prune is called directly, outside of a Frame pa
int. | 160 if (!currentTime) // In case prune is called directly, outside of a Frame pa
int. |
160 currentTime = WTF::currentTime(); | 161 currentTime = WTF::currentTime(); |
161 | 162 |
162 // Destroy any decoded data in live objects that we can. | 163 // Destroy any decoded data in live objects that we can. |
163 // Start from the tail, since this is the least recently accessed of the obj
ects. | 164 // Start from the tail, since this is the lowest priority |
| 165 // and least recently accessed of the objects. |
164 | 166 |
165 // The list might not be sorted by the m_lastDecodedAccessTime. The impact | 167 // The list might not be sorted by the m_lastDecodedAccessTime. The impact |
166 // of this weaker invariant is minor as the below if statement to check the | 168 // of this weaker invariant is minor as the below if statement to check the |
167 // elapsedTime will evaluate to false as the currentTime will be a lot | 169 // elapsedTime will evaluate to false as the currentTime will be a lot |
168 // greater than the current->m_lastDecodedAccessTime. | 170 // greater than the current->m_lastDecodedAccessTime. |
169 // For more details see: https://bugs.webkit.org/show_bug.cgi?id=30209 | 171 // For more details see: https://bugs.webkit.org/show_bug.cgi?id=30209 |
170 CachedResource* current = m_liveDecodedResources.m_tail; | |
171 while (current) { | |
172 CachedResource* prev = current->m_prevInLiveResourcesList; | |
173 ASSERT(current->hasClients()); | |
174 if (current->isLoaded() && current->decodedSize()) { | |
175 // Check to see if the remaining resources are too new to prune. | |
176 double elapsedTime = currentTime - current->m_lastDecodedAccessTime; | |
177 if (elapsedTime < cMinDelayBeforeLiveDecodedPrune) | |
178 return; | |
179 | 172 |
180 // Destroy our decoded data. This will remove us from | 173 // Start pruning from the lowest priority list. |
181 // m_liveDecodedResources, and possibly move us to a different LRU | 174 for (int i = 0; i < 3; i++) { |
182 // list in m_allResources. | 175 CachedResource* current = m_liveDecodedResources[i].m_tail; |
183 current->destroyDecodedData(); | 176 while (current) { |
| 177 CachedResource* prev = current->m_prevInLiveResourcesList; |
| 178 ASSERT(current->hasClients()); |
| 179 if (current->isLoaded() && current->decodedSize()) { |
| 180 // Check to see if the remaining resources are too new to prune. |
| 181 double elapsedTime = currentTime - current->m_lastDecodedAccessT
ime; |
| 182 if (elapsedTime < m_delayBeforeLiveDecodedPrune) |
| 183 return; |
184 | 184 |
185 if (targetSize && m_liveSize <= targetSize) | 185 // Destroy our decoded data. This will remove us from |
186 return; | 186 // m_liveDecodedResources, and possibly move us to a different L
RU |
| 187 // list in m_allResources. |
| 188 current->destroyDecodedData(); |
| 189 |
| 190 if (targetSize && m_liveSize <= targetSize) |
| 191 return; |
| 192 } |
| 193 current = prev; |
187 } | 194 } |
188 current = prev; | |
189 } | 195 } |
190 } | 196 } |
191 | 197 |
192 void MemoryCache::pruneDeadResources() | 198 void MemoryCache::pruneDeadResources() |
193 { | 199 { |
194 unsigned capacity = deadCapacity(); | 200 unsigned capacity = deadCapacity(); |
195 if (capacity && m_deadSize <= capacity) | 201 if (capacity && m_deadSize <= capacity) |
196 return; | 202 return; |
197 | 203 |
198 unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentag
e); // Cut by a percentage to avoid immediately pruning again. | 204 unsigned targetSize = static_cast<unsigned>(capacity * cTargetPrunePercentag
e); // Cut by a percentage to avoid immediately pruning again. |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 | 393 |
388 } | 394 } |
389 | 395 |
390 void MemoryCache::removeFromLiveDecodedResourcesList(CachedResource* resource) | 396 void MemoryCache::removeFromLiveDecodedResourcesList(CachedResource* resource) |
391 { | 397 { |
392 // If we've never been accessed, then we're brand new and not in any list. | 398 // If we've never been accessed, then we're brand new and not in any list. |
393 if (!resource->m_inLiveDecodedResourcesList) | 399 if (!resource->m_inLiveDecodedResourcesList) |
394 return; | 400 return; |
395 resource->m_inLiveDecodedResourcesList = false; | 401 resource->m_inLiveDecodedResourcesList = false; |
396 | 402 |
| 403 LRUList* list = &m_liveDecodedResources[resource->cachePriority()]; |
| 404 |
397 #if !ASSERT_DISABLED | 405 #if !ASSERT_DISABLED |
398 // Verify that we are in fact in this list. | 406 // Verify that we are in fact in this list. |
399 bool found = false; | 407 bool found = false; |
400 for (CachedResource* current = m_liveDecodedResources.m_head; current; curre
nt = current->m_nextInLiveResourcesList) { | 408 for (CachedResource* current = list->m_head; current; current = current->m_n
extInLiveResourcesList) { |
401 if (current == resource) { | 409 if (current == resource) { |
402 found = true; | 410 found = true; |
403 break; | 411 break; |
404 } | 412 } |
405 } | 413 } |
406 ASSERT(found); | 414 ASSERT(found); |
407 #endif | 415 #endif |
408 | 416 |
409 CachedResource* next = resource->m_nextInLiveResourcesList; | 417 CachedResource* next = resource->m_nextInLiveResourcesList; |
410 CachedResource* prev = resource->m_prevInLiveResourcesList; | 418 CachedResource* prev = resource->m_prevInLiveResourcesList; |
411 | 419 |
412 if (next == 0 && prev == 0 && m_liveDecodedResources.m_head != resource) | 420 if (!next && !prev && list->m_head != resource) |
413 return; | 421 return; |
414 | 422 |
415 resource->m_nextInLiveResourcesList = 0; | 423 resource->m_nextInLiveResourcesList = 0; |
416 resource->m_prevInLiveResourcesList = 0; | 424 resource->m_prevInLiveResourcesList = 0; |
417 | 425 |
418 if (next) | 426 if (next) |
419 next->m_prevInLiveResourcesList = prev; | 427 next->m_prevInLiveResourcesList = prev; |
420 else if (m_liveDecodedResources.m_tail == resource) | 428 else if (list->m_tail == resource) |
421 m_liveDecodedResources.m_tail = prev; | 429 list->m_tail = prev; |
422 | 430 |
423 if (prev) | 431 if (prev) |
424 prev->m_nextInLiveResourcesList = next; | 432 prev->m_nextInLiveResourcesList = next; |
425 else if (m_liveDecodedResources.m_head == resource) | 433 else if (list->m_head == resource) |
426 m_liveDecodedResources.m_head = next; | 434 list->m_head = next; |
427 } | 435 } |
428 | 436 |
429 void MemoryCache::insertInLiveDecodedResourcesList(CachedResource* resource) | 437 void MemoryCache::insertInLiveDecodedResourcesList(CachedResource* resource) |
430 { | 438 { |
431 // Make sure we aren't in the list already. | 439 // Make sure we aren't in the list already. |
432 ASSERT(!resource->m_nextInLiveResourcesList && !resource->m_prevInLiveResour
cesList && !resource->m_inLiveDecodedResourcesList); | 440 ASSERT(!resource->m_nextInLiveResourcesList && !resource->m_prevInLiveResour
cesList && !resource->m_inLiveDecodedResourcesList); |
433 resource->m_inLiveDecodedResourcesList = true; | 441 resource->m_inLiveDecodedResourcesList = true; |
434 | 442 |
435 resource->m_nextInLiveResourcesList = m_liveDecodedResources.m_head; | 443 LRUList* list = &m_liveDecodedResources[resource->cachePriority()]; |
436 if (m_liveDecodedResources.m_head) | 444 resource->m_nextInLiveResourcesList = list->m_head; |
437 m_liveDecodedResources.m_head->m_prevInLiveResourcesList = resource; | 445 if (list->m_head) |
438 m_liveDecodedResources.m_head = resource; | 446 list->m_head->m_prevInLiveResourcesList = resource; |
439 | 447 list->m_head = resource; |
| 448 |
440 if (!resource->m_nextInLiveResourcesList) | 449 if (!resource->m_nextInLiveResourcesList) |
441 m_liveDecodedResources.m_tail = resource; | 450 list->m_tail = resource; |
442 | 451 |
443 #if !ASSERT_DISABLED | 452 #if !ASSERT_DISABLED |
444 // Verify that we are in now in the list like we should be. | 453 // Verify that we are in now in the list like we should be. |
445 bool found = false; | 454 bool found = false; |
446 for (CachedResource* current = m_liveDecodedResources.m_head; current; curre
nt = current->m_nextInLiveResourcesList) { | 455 for (CachedResource* current = list->m_head; current; current = current->m_n
extInLiveResourcesList) { |
447 if (current == resource) { | 456 if (current == resource) { |
448 found = true; | 457 found = true; |
449 break; | 458 break; |
450 } | 459 } |
451 } | 460 } |
452 ASSERT(found); | 461 ASSERT(found); |
453 #endif | 462 #endif |
454 | 463 |
455 } | 464 } |
456 | 465 |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
599 printf("(%.1fK, %.1fK, %uA, %dR, %d, %d); ", current->decodedSiz
e() / 1024.0f, (current->encodedSize() + current->overheadSize()) / 1024.0f, cur
rent->accessCount(), current->hasClients(), current->isPurgeable(), current->was
Purged()); | 608 printf("(%.1fK, %.1fK, %uA, %dR, %d, %d); ", current->decodedSiz
e() / 1024.0f, (current->encodedSize() + current->overheadSize()) / 1024.0f, cur
rent->accessCount(), current->hasClients(), current->isPurgeable(), current->was
Purged()); |
600 | 609 |
601 current = prev; | 610 current = prev; |
602 } | 611 } |
603 } | 612 } |
604 } | 613 } |
605 | 614 |
606 #endif // MEMORY_CACHE_STATS | 615 #endif // MEMORY_CACHE_STATS |
607 | 616 |
608 } // namespace WebCore | 617 } // namespace WebCore |
OLD | NEW |