Chromium Code Reviews| Index: chrome/browser/predictors/resource_prefetch_predictor.cc |
| diff --git a/chrome/browser/predictors/resource_prefetch_predictor.cc b/chrome/browser/predictors/resource_prefetch_predictor.cc |
| index 03e9570a0b1cc020038315f01d97dc01a6468e85..554b3e9781510bd78af157aab50b6d56bc3dfac9 100644 |
| --- a/chrome/browser/predictors/resource_prefetch_predictor.cc |
| +++ b/chrome/browser/predictors/resource_prefetch_predictor.cc |
| @@ -210,6 +210,47 @@ content::ResourceType ResourcePrefetchPredictor::GetResourceTypeFromMimeType( |
| return fallback; |
| } |
| +// static |
| +bool ResourcePrefetchPredictor::GetRedirectEndpoint( |
| + const std::string& first_redirect, |
| + const RedirectDataMap& redirect_data_map, |
| + std::string* final_redirect) { |
| + DCHECK(final_redirect); |
| + |
| + RedirectDataMap::const_iterator it = redirect_data_map.find(first_redirect); |
| + if (it == redirect_data_map.end()) |
| + return false; |
| + |
| + const RedirectData& redirect_data = it->second; |
| + int best_redirect_i = -1; |
| + float best_confidence = 0.0; |
| + for (int i = 0; i < redirect_data.redirect_endpoints_size(); ++i) { |
| + const RedirectStat& redirect = redirect_data.redirect_endpoints(i); |
| + float confidence = |
| + (redirect.number_of_hits() + 0.0) / |
| + (redirect.number_of_hits() + redirect.number_of_misses()); |
| + if (confidence > best_confidence) { |
| + best_redirect_i = i; |
| + best_confidence = confidence; |
| + } |
| + } |
| + |
| + if (best_redirect_i == -1) |
| + return false; |
| + |
| + const RedirectStat& best_redirect = |
| + redirect_data.redirect_endpoints(best_redirect_i); |
| + const float kMinRedirectConfidenceToTriggerPrefetch = 0.7f; |
| + const int kMinRedirectHitsToTriggerPrefetch = 2; |
| + |
| + if (best_confidence < kMinRedirectConfidenceToTriggerPrefetch || |
| + best_redirect.number_of_hits() < kMinRedirectHitsToTriggerPrefetch) |
| + return false; |
| + |
| + *final_redirect = best_redirect.url(); |
| + return true; |
| +} |
| + |
| //////////////////////////////////////////////////////////////////////////////// |
| // ResourcePrefetchPredictor nested types. |
| @@ -419,9 +460,6 @@ void ResourcePrefetchPredictor::OnMainFrameRedirect( |
| const URLRequestSummary& response) { |
| DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| - // Stop any inflight prefetching. Remove the older navigation. |
| - StopPrefetching(response.navigation_id.main_frame_url); |
| - |
| std::unique_ptr<PageRequestSummary> summary; |
| NavigationMap::iterator nav_it = |
| inflight_navigations_.find(response.navigation_id); |
| @@ -492,47 +530,65 @@ void ResourcePrefetchPredictor::OnNavigationComplete( |
| bool ResourcePrefetchPredictor::GetPrefetchData(const GURL& main_frame_url, |
| std::vector<GURL>* urls) { |
| DCHECK(urls); |
| - |
| + DCHECK(urls->empty()); |
| bool use_url_data = config_.IsPrefetchingEnabled(profile_) ? |
| config_.IsURLPrefetchingEnabled(profile_) : |
| config_.IsURLLearningEnabled(); |
| - if (use_url_data) { |
| - PrefetchDataMap::const_iterator iterator = |
| - url_table_cache_->find(main_frame_url.spec()); |
| - if (iterator != url_table_cache_->end()) |
| - PopulatePrefetcherRequest(iterator->second, urls); |
| - } |
| - if (!urls->empty()) |
| - return true; |
| - |
| bool use_host_data = config_.IsPrefetchingEnabled(profile_) ? |
| config_.IsHostPrefetchingEnabled(profile_) : |
| config_.IsHostLearningEnabled(); |
| - if (use_host_data) { |
| - PrefetchDataMap::const_iterator iterator = |
| - host_table_cache_->find(main_frame_url.host()); |
| - if (iterator != host_table_cache_->end()) |
| - PopulatePrefetcherRequest(iterator->second, urls); |
| + |
| + if (use_url_data && |
|
pasko
2016/10/07 15:42:44
add a comment above:
// Attempt to fetch URLs base
alexilin
2016/10/07 16:33:44
Done.
|
| + PopulatePrefetcherRequest(main_frame_url.spec(), *url_table_cache_, urls)) |
| + return true; |
| + |
| + if (use_host_data && PopulatePrefetcherRequest(main_frame_url.host(), |
| + *host_table_cache_, urls)) |
| + return true; |
| + |
| + std::string redirect_endpoint; |
|
pasko
2016/10/07 15:42:44
Add a comment 1 line above saying:
// Fallback to
alexilin
2016/10/07 16:33:44
Done.
|
| + if (GetRedirectEndpoint(main_frame_url.spec(), *url_redirect_table_cache_, |
| + &redirect_endpoint)) { |
| + GURL redirect_endpoint_url(redirect_endpoint); |
| + |
| + if (use_url_data && PopulatePrefetcherRequest(redirect_endpoint_url.spec(), |
| + *url_table_cache_, urls)) |
|
pasko
2016/10/07 15:42:44
if the condition takes multiple lines, it requires
alexilin
2016/10/07 16:33:44
Oh, I've thought that this rule is applied only fo
pasko
2016/10/07 17:18:54
I was curious ... because I actually prefer always
alexilin
2016/10/11 13:14:41
Benoit has already forced me to remove brackets in
pasko
2016/10/11 14:16:19
Sorry for the fires :)
I am with Benoit on one-li
alexilin
2016/10/11 15:24:29
Yeah, I see that this is another case.
To summariz
pasko
2016/10/11 15:57:55
not only that, there are more stupid rules regardi
|
| + return true; |
| + |
| + if (use_host_data && PopulatePrefetcherRequest(redirect_endpoint_url.host(), |
|
pasko
2016/10/07 15:42:45
Using URL-based redirects for host-based prefetch
alexilin
2016/10/07 16:33:44
It wasn't discussed yet so this is only my opinion
pasko
2016/10/07 17:18:54
Im, interesting and reasonable.
Theoretically ..
alexilin
2016/10/11 13:14:41
Done.
|
| + *host_table_cache_, urls)) |
| + return true; |
| } |
| - return !urls->empty(); |
| + return use_host_data && |
| + GetRedirectEndpoint(main_frame_url.host(), *host_redirect_table_cache_, |
| + &redirect_endpoint) && |
| + PopulatePrefetcherRequest(redirect_endpoint, *host_table_cache_, urls); |
| } |
| -void ResourcePrefetchPredictor::PopulatePrefetcherRequest( |
| - const PrefetchData& data, |
| +bool ResourcePrefetchPredictor::PopulatePrefetcherRequest( |
| + const std::string& main_frame_key, |
| + const PrefetchDataMap& data_map, |
| std::vector<GURL>* urls) { |
| - for (const ResourceData& resource : data.resources()) { |
| + DCHECK(urls); |
| + PrefetchDataMap::const_iterator it = data_map.find(main_frame_key); |
| + if (it == data_map.end()) |
| + return false; |
| + |
| + size_t initial_size = urls->size(); |
| + for (const ResourceData& resource : it->second.resources()) { |
| float confidence = |
| static_cast<float>(resource.number_of_hits()) / |
| (resource.number_of_hits() + resource.number_of_misses()); |
| if (confidence < config_.min_resource_confidence_to_trigger_prefetch || |
| resource.number_of_hits() < |
| - config_.min_resource_hits_to_trigger_prefetch) { |
| + config_.min_resource_hits_to_trigger_prefetch) |
| continue; |
| - } |
| urls->push_back(GURL(resource.resource_url())); |
| } |
| + |
| + return urls->size() > initial_size; |
| } |
| void ResourcePrefetchPredictor::StartPrefetching(const GURL& url) { |
| @@ -1000,10 +1056,9 @@ void ResourcePrefetchPredictor::LearnRedirect(const std::string& key, |
| } |
| RedirectData& data = cache_entry->second; |
| - // Trim and sort the redirects after the update. |
| + // Trim the redirects after the update. |
| ResourcePrefetchPredictorTables::TrimRedirects( |
| &data, config_.max_consecutive_misses); |
| - ResourcePrefetchPredictorTables::SortRedirects(&data); |
| if (data.redirect_endpoints_size() == 0) { |
| redirect_map->erase(cache_entry); |