OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" | 5 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <memory> | 10 #include <memory> |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 | 127 |
128 } // namespace | 128 } // namespace |
129 | 129 |
130 namespace predictors { | 130 namespace predictors { |
131 | 131 |
132 // static | 132 // static |
133 void ResourcePrefetchPredictorTables::SortResources( | 133 void ResourcePrefetchPredictorTables::SortResources( |
134 std::vector<ResourceData>* resources) { | 134 std::vector<ResourceData>* resources) { |
135 std::sort(resources->begin(), resources->end(), | 135 std::sort(resources->begin(), resources->end(), |
136 [](const ResourceData& x, const ResourceData& y) { | 136 [](const ResourceData& x, const ResourceData& y) { |
| 137 // Decreasing score ordering. |
137 return ComputeResourceScore(x) > ComputeResourceScore(y); | 138 return ComputeResourceScore(x) > ComputeResourceScore(y); |
138 }); | 139 }); |
139 } | 140 } |
140 | 141 |
141 // static | 142 // static |
142 void ResourcePrefetchPredictorTables::SortRedirects( | 143 void ResourcePrefetchPredictorTables::SortRedirects( |
143 std::vector<RedirectStat>* redirects) { | 144 std::vector<RedirectStat>* redirects) { |
144 std::sort(redirects->begin(), redirects->end(), | 145 std::sort(redirects->begin(), redirects->end(), |
145 [](const RedirectStat& x, const RedirectStat& y) { | 146 [](const RedirectStat& x, const RedirectStat& y) { |
| 147 // Decreasing score ordering. |
146 return ComputeRedirectScore(x) > ComputeRedirectScore(y); | 148 return ComputeRedirectScore(x) > ComputeRedirectScore(y); |
147 }); | 149 }); |
148 } | 150 } |
149 | 151 |
150 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( | 152 ResourcePrefetchPredictorTables::PrefetchData::PrefetchData( |
151 PrefetchKeyType i_key_type, | 153 PrefetchKeyType i_key_type, |
152 const std::string& i_primary_key) | 154 const std::string& i_primary_key) |
153 : key_type(i_key_type), | 155 : key_type(i_key_type), |
154 primary_key(i_primary_key) { | 156 primary_key(i_primary_key) { |
155 } | 157 } |
(...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 for (const RedirectStat& redirect : data.redirect_endpoints()) { | 473 for (const RedirectStat& redirect : data.redirect_endpoints()) { |
472 if (redirect.url().length() > kMaxStringLength) | 474 if (redirect.url().length() > kMaxStringLength) |
473 return false; | 475 return false; |
474 } | 476 } |
475 return true; | 477 return true; |
476 } | 478 } |
477 | 479 |
478 // static | 480 // static |
479 float ResourcePrefetchPredictorTables::ComputeResourceScore( | 481 float ResourcePrefetchPredictorTables::ComputeResourceScore( |
480 const ResourceData& data) { | 482 const ResourceData& data) { |
481 // The score is calculated so that when the rows are sorted, stylesheets, | 483 // The ranking is done by considering, in this order: |
482 // scripts and fonts appear first, sorted by position(ascending) and then the | 484 // 1. Resource Priority |
483 // rest of the resources sorted by position (ascending). | 485 // 2. Request resource type |
484 static const int kMaxResourcesPerType = 100; | 486 // 3. Finally, the average position, giving a higher priotity to earlier |
| 487 // resources. |
| 488 |
| 489 int priority_multiplier; |
| 490 switch (data.priority()) { |
| 491 case ResourceData::REQUEST_PRIORITY_HIGHEST: |
| 492 priority_multiplier = 3; |
| 493 break; |
| 494 case ResourceData::REQUEST_PRIORITY_MEDIUM: |
| 495 priority_multiplier = 2; |
| 496 break; |
| 497 case ResourceData::REQUEST_PRIORITY_LOW: |
| 498 case ResourceData::REQUEST_PRIORITY_LOWEST: |
| 499 case ResourceData::REQUEST_PRIORITY_IDLE: |
| 500 default: |
| 501 priority_multiplier = 1; |
| 502 break; |
| 503 } |
| 504 |
| 505 int type_multiplier; |
485 switch (data.resource_type()) { | 506 switch (data.resource_type()) { |
486 case ResourceData::RESOURCE_TYPE_STYLESHEET: | 507 case ResourceData::RESOURCE_TYPE_STYLESHEET: |
487 case ResourceData::RESOURCE_TYPE_SCRIPT: | 508 case ResourceData::RESOURCE_TYPE_SCRIPT: |
| 509 type_multiplier = 3; |
| 510 break; |
488 case ResourceData::RESOURCE_TYPE_FONT_RESOURCE: | 511 case ResourceData::RESOURCE_TYPE_FONT_RESOURCE: |
489 return (2 * kMaxResourcesPerType) - data.average_position(); | 512 type_multiplier = 2; |
490 | 513 break; |
491 case ResourceData::RESOURCE_TYPE_IMAGE: | 514 case ResourceData::RESOURCE_TYPE_IMAGE: |
492 default: | 515 default: |
493 return kMaxResourcesPerType - data.average_position(); | 516 type_multiplier = 1; |
494 } | 517 } |
495 // TODO(lizeb): Take priority into account. | 518 |
| 519 constexpr int kMaxResourcesPerType = 100; |
| 520 return kMaxResourcesPerType * |
| 521 (priority_multiplier * 100 + type_multiplier * 10) - |
| 522 data.average_position(); |
496 } | 523 } |
497 | 524 |
498 // static | 525 // static |
499 float ResourcePrefetchPredictorTables::ComputeRedirectScore( | 526 float ResourcePrefetchPredictorTables::ComputeRedirectScore( |
500 const RedirectStat& data) { | 527 const RedirectStat& data) { |
501 // TODO(alexilin): Invent some scoring. | 528 // TODO(alexilin): Invent some scoring. |
502 return 0.0; | 529 return 0.0; |
503 } | 530 } |
504 | 531 |
505 // static | 532 // static |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 return is_host ? kHostRedirectTableName : kUrlRedirectTableName; | 705 return is_host ? kHostRedirectTableName : kUrlRedirectTableName; |
679 case PrefetchDataType::METADATA: | 706 case PrefetchDataType::METADATA: |
680 return is_host ? kHostMetadataTableName : kUrlMetadataTableName; | 707 return is_host ? kHostMetadataTableName : kUrlMetadataTableName; |
681 } | 708 } |
682 | 709 |
683 NOTREACHED(); | 710 NOTREACHED(); |
684 return nullptr; | 711 return nullptr; |
685 } | 712 } |
686 | 713 |
687 } // namespace predictors | 714 } // namespace predictors |
OLD | NEW |