Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1153)

Side by Side Diff: third_party/WebKit/Source/core/fetch/ResourceFetcher.cpp

Issue 2072613002: Make ResourceLoadPriority calculation simpler (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address review comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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, 2009, 2010, 2011 Apple Inc. All rights reserved. 5 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ 6 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
7 7
8 This library is free software; you can redistribute it and/or 8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public 9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either 10 License as published by the Free Software Foundation; either
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
127 // Also async scripts (set explicitly in loadPriority) 127 // Also async scripts (set explicitly in loadPriority)
128 return ResourceLoadPriorityLow; 128 return ResourceLoadPriorityLow;
129 case Resource::LinkPrefetch: 129 case Resource::LinkPrefetch:
130 return ResourceLoadPriorityVeryLow; 130 return ResourceLoadPriorityVeryLow;
131 } 131 }
132 132
133 ASSERT_NOT_REACHED(); 133 ASSERT_NOT_REACHED();
134 return ResourceLoadPriorityUnresolved; 134 return ResourceLoadPriorityUnresolved;
135 } 135 }
136 136
137 ResourceLoadPriority ResourceFetcher::loadPriority(Resource::Type type, const Fe tchRequest& request, ResourcePriority::VisibilityStatus visibility) 137 ResourceLoadPriority ResourceFetcher::computeLoadPriority(Resource::Type type, c onst FetchRequest& request, ResourcePriority::VisibilityStatus visibility)
138 { 138 {
139 // TODO(yoav): Change it here so that priority can be changed even after it was resolved.
140 if (request.priority() != ResourceLoadPriorityUnresolved)
141 return request.priority();
142
143 // Synchronous requests should always be max priority, lest they hang the re nderer.
144 if (request.options().synchronousPolicy == RequestSynchronously)
145 return ResourceLoadPriorityHighest;
146
147 ResourceLoadPriority priority = typeToPriority(type); 139 ResourceLoadPriority priority = typeToPriority(type);
148 140
149 // Visible resources (images in practice) get a boost to High priority. 141 // Visible resources (images in practice) get a boost to High priority.
150 if (visibility == ResourcePriority::Visible) 142 if (visibility == ResourcePriority::Visible)
151 priority = ResourceLoadPriorityHigh; 143 priority = ResourceLoadPriorityHigh;
152 144
153 // Resources before the first image are considered "early" in the document 145 // Resources before the first image are considered "early" in the document
154 // and resources after the first image are "late" in the document. Importan t to 146 // and resources after the first image are "late" in the document. Importan t to
155 // note that this is based on when the preload scanner discovers a resource 147 // note that this is based on when the preload scanner discovers a resource
156 // for the most part so the main parser may not have reached the image eleme nt yet. 148 // for the most part so the main parser may not have reached the image eleme nt yet.
157 if (type == Resource::Image) 149 if (type == Resource::Image)
158 m_imageFetched = true; 150 m_imageFetched = true;
159 151
160 // Special handling for scripts. 152 // Special handling for scripts.
161 // Default/Parser-Blocking/Preload early in document: High (set in typeToPri ority) 153 // Default/Parser-Blocking/Preload early in document: High (set in typeToPri ority)
162 // Async/Defer: Low Priority (applies to both preload and parser-inserted) 154 // Async/Defer: Low Priority (applies to both preload and parser-inserted)
163 // Preload late in document: Medium 155 // Preload late in document: Medium
164 if (type == Resource::Script) { 156 if (type == Resource::Script) {
165 if (FetchRequest::LazyLoad == request.defer()) 157 if (FetchRequest::LazyLoad == request.defer())
166 priority = ResourceLoadPriorityLow; 158 priority = ResourceLoadPriorityLow;
167 else if (request.forPreload() && m_imageFetched) 159 else if (request.forPreload() && m_imageFetched)
168 priority = ResourceLoadPriorityMedium; 160 priority = ResourceLoadPriorityMedium;
161 } else if (FetchRequest::LazyLoad == request.defer()) {
162 // A deferred load of type Raw is a link rel=preload, which should be Lo w instead of VeryLow.
163 priority = type == Resource::Raw ? ResourceLoadPriorityLow : ResourceLoa dPriorityVeryLow;
169 } 164 }
170 165
171 return context().modifyPriorityForExperiments(priority); 166 // A manually set priority acts as a floor. This is used to ensure that sync hronous requests
167 // are always given the highest possible priority, as well as to ensure that there isn't priority
168 // churn if images move in and out of the viewport, or is displayed more tha n once, both in and out
169 // of the viewport.
170 return std::max(context().modifyPriorityForExperiments(priority), request.re sourceRequest().priority());
172 } 171 }
173 172
174 static void populateResourceTiming(ResourceTimingInfo* info, Resource* resource) 173 static void populateResourceTiming(ResourceTimingInfo* info, Resource* resource)
175 { 174 {
176 info->setInitialRequest(resource->resourceRequest()); 175 info->setInitialRequest(resource->resourceRequest());
177 info->setFinalResponse(resource->response()); 176 info->setFinalResponse(resource->response());
178 } 177 }
179 178
180 static WebURLRequest::RequestContext requestContextFromType(bool isMainFrame, Re source::Type type) 179 static WebURLRequest::RequestContext requestContextFromType(bool isMainFrame, Re source::Type type)
181 { 180 {
(...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after
416 415
417 TRACE_EVENT1("blink", "ResourceFetcher::requestResource", "url", urlForTrace Event(request.url())); 416 TRACE_EVENT1("blink", "ResourceFetcher::requestResource", "url", urlForTrace Event(request.url()));
418 417
419 if (!request.url().isValid()) 418 if (!request.url().isValid())
420 return nullptr; 419 return nullptr;
421 420
422 if (!context().canRequest(factory.type(), request.resourceRequest(), MemoryC ache::removeFragmentIdentifierIfNeeded(request.url()), request.options(), reques t.forPreload(), request.getOriginRestriction())) 421 if (!context().canRequest(factory.type(), request.resourceRequest(), MemoryC ache::removeFragmentIdentifierIfNeeded(request.url()), request.options(), reques t.forPreload(), request.getOriginRestriction()))
423 return nullptr; 422 return nullptr;
424 423
425 unsigned long identifier = createUniqueIdentifier(); 424 unsigned long identifier = createUniqueIdentifier();
426 request.setPriority(loadPriority(factory.type(), request, ResourcePriority:: NotVisible)); 425 request.mutableResourceRequest().setPriority(computeLoadPriority(factory.typ e(), request, ResourcePriority::NotVisible));
427 request.mutableResourceRequest().setPriority(request.priority());
428 initializeResourceRequest(request.mutableResourceRequest(), factory.type(), request.defer()); 426 initializeResourceRequest(request.mutableResourceRequest(), factory.type(), request.defer());
429 context().willStartLoadingResource(identifier, request.mutableResourceReques t(), factory.type()); 427 context().willStartLoadingResource(identifier, request.mutableResourceReques t(), factory.type());
430 if (!request.url().isValid()) 428 if (!request.url().isValid())
431 return nullptr; 429 return nullptr;
432 430
433 if (!request.forPreload()) { 431 if (!request.forPreload()) {
434 V8DOMActivityLogger* activityLogger = nullptr; 432 V8DOMActivityLogger* activityLogger = nullptr;
435 if (request.options().initiatorInfo.name == FetchInitiatorTypeNames::xml httprequest) 433 if (request.options().initiatorInfo.name == FetchInitiatorTypeNames::xml httprequest)
436 activityLogger = V8DOMActivityLogger::currentActivityLogger(); 434 activityLogger = V8DOMActivityLogger::currentActivityLogger();
437 else 435 else
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 491
494 if (policy != Use) 492 if (policy != Use)
495 resource->setIdentifier(identifier); 493 resource->setIdentifier(identifier);
496 494
497 if (!request.forPreload() || policy != Use) { 495 if (!request.forPreload() || policy != Use) {
498 // When issuing another request for a resource that is already in-flight make 496 // When issuing another request for a resource that is already in-flight make
499 // sure to not demote the priority of the in-flight request. If the new request 497 // sure to not demote the priority of the in-flight request. If the new request
500 // isn't at the same priority as the in-flight request, only allow promo tions. 498 // isn't at the same priority as the in-flight request, only allow promo tions.
501 // This can happen when a visible image's priority is increased and then another 499 // This can happen when a visible image's priority is increased and then another
502 // reference to the image is parsed (which would be at a lower priority) . 500 // reference to the image is parsed (which would be at a lower priority) .
503 if (request.priority() > resource->resourceRequest().priority()) 501 if (request.resourceRequest().priority() > resource->resourceRequest().p riority())
504 resource->didChangePriority(request.priority(), 0); 502 resource->didChangePriority(request.resourceRequest().priority(), 0) ;
505 } 503 }
506 504
507 // If only the fragment identifiers differ, it is the same resource. 505 // If only the fragment identifiers differ, it is the same resource.
508 DCHECK(equalIgnoringFragmentIdentifier(resource->url(), request.url())); 506 DCHECK(equalIgnoringFragmentIdentifier(resource->url(), request.url()));
509 requestLoadStarted(identifier, resource, request, policy == Use ? ResourceLo adingFromCache : ResourceLoadingFromNetwork, isStaticData); 507 requestLoadStarted(identifier, resource, request, policy == Use ? ResourceLo adingFromCache : ResourceLoadingFromNetwork, isStaticData);
510 m_documentResources.set(MemoryCache::removeFragmentIdentifierIfNeeded(reques t.url()), resource); 508 m_documentResources.set(MemoryCache::removeFragmentIdentifierIfNeeded(reques t.url()), resource);
511 509
512 // Returns with an existing resource if the resource does not need to start 510 // Returns with an existing resource if the resource does not need to start
513 // loading immediately. 511 // loading immediately.
514 // If revalidation policy was determined as |Revalidate|, the resource was 512 // If revalidation policy was determined as |Revalidate|, the resource was
(...skipping 555 matching lines...) Expand 10 before | Expand all | Expand 10 after
1070 1068
1071 void ResourceFetcher::updateAllImageResourcePriorities() 1069 void ResourceFetcher::updateAllImageResourcePriorities()
1072 { 1070 {
1073 TRACE_EVENT0("blink", "ResourceLoadPriorityOptimizer::updateAllImageResource Priorities"); 1071 TRACE_EVENT0("blink", "ResourceLoadPriorityOptimizer::updateAllImageResource Priorities");
1074 for (const auto& documentResource : m_documentResources) { 1072 for (const auto& documentResource : m_documentResources) {
1075 Resource* resource = documentResource.value.get(); 1073 Resource* resource = documentResource.value.get();
1076 if (!resource || !resource->isImage() || !resource->isLoading()) 1074 if (!resource || !resource->isImage() || !resource->isLoading())
1077 continue; 1075 continue;
1078 1076
1079 ResourcePriority resourcePriority = resource->priorityFromObservers(); 1077 ResourcePriority resourcePriority = resource->priorityFromObservers();
1080 ResourceLoadPriority resourceLoadPriority = loadPriority(Resource::Image , FetchRequest(resource->resourceRequest(), FetchInitiatorInfo()), resourcePrior ity.visibility); 1078 ResourceLoadPriority resourceLoadPriority = computeLoadPriority(Resource ::Image, FetchRequest(resource->resourceRequest(), FetchInitiatorInfo()), resour cePriority.visibility);
1081 if (resourceLoadPriority == resource->resourceRequest().priority()) 1079 if (resourceLoadPriority == resource->resourceRequest().priority())
1082 continue; 1080 continue;
1083 1081
1084 resource->didChangePriority(resourceLoadPriority, resourcePriority.intra PriorityValue); 1082 resource->didChangePriority(resourceLoadPriority, resourcePriority.intra PriorityValue);
1085 TRACE_EVENT_ASYNC_STEP_INTO1("blink.net", "Resource", resource->identifi er(), "ChangePriority", "priority", resourceLoadPriority); 1083 TRACE_EVENT_ASYNC_STEP_INTO1("blink.net", "Resource", resource->identifi er(), "ChangePriority", "priority", resourceLoadPriority);
1086 context().dispatchDidChangeResourcePriority(resource->identifier(), reso urceLoadPriority, resourcePriority.intraPriorityValue); 1084 context().dispatchDidChangeResourcePriority(resource->identifier(), reso urceLoadPriority, resourcePriority.intraPriorityValue);
1087 } 1085 }
1088 } 1086 }
1089 1087
1090 void ResourceFetcher::reloadLoFiImages() 1088 void ResourceFetcher::reloadLoFiImages()
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1261 visitor->trace(m_context); 1259 visitor->trace(m_context);
1262 visitor->trace(m_archive); 1260 visitor->trace(m_archive);
1263 visitor->trace(m_loaders); 1261 visitor->trace(m_loaders);
1264 visitor->trace(m_nonBlockingLoaders); 1262 visitor->trace(m_nonBlockingLoaders);
1265 visitor->trace(m_documentResources); 1263 visitor->trace(m_documentResources);
1266 visitor->trace(m_preloads); 1264 visitor->trace(m_preloads);
1267 visitor->trace(m_resourceTimingInfoMap); 1265 visitor->trace(m_resourceTimingInfoMap);
1268 } 1266 }
1269 1267
1270 } // namespace blink 1268 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698