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 static constexpr int kMaxResourcesPerType = 100; |
pasko
2016/10/03 16:48:20
is static constexpr better in some way than just c
Benoit L
2016/10/04 09:49:54
Done.
| |
482 // scripts and fonts appear first, sorted by position(ascending) and then the | 484 int priority_multiplier, type_multiplier; |
483 // rest of the resources sorted by position (ascending). | 485 |
484 static const int kMaxResourcesPerType = 100; | 486 // The ranking is done by considering, in this order: |
487 // 1. Resource Priority | |
488 // 2. Request resource type | |
489 // 3. Finally, the average position, giving a higher priotity to earlier | |
490 // resources. | |
491 switch (data.priority()) { | |
492 case ResourceData::REQUEST_PRIORITY_HIGHEST: | |
493 priority_multiplier = 3; | |
494 break; | |
495 case ResourceData::REQUEST_PRIORITY_MEDIUM: | |
496 priority_multiplier = 2; | |
497 break; | |
498 case ResourceData::REQUEST_PRIORITY_LOW: | |
499 case ResourceData::REQUEST_PRIORITY_LOWEST: | |
500 case ResourceData::REQUEST_PRIORITY_IDLE: | |
501 default: | |
502 priority_multiplier = 1; | |
503 break; | |
504 } | |
505 | |
485 switch (data.resource_type()) { | 506 switch (data.resource_type()) { |
pasko
2016/10/03 16:48:20
nit: Seems better to move declaration of kMaxResou
Benoit L
2016/10/04 09:49:54
Done.
| |
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 return kMaxResourcesPerType * | |
520 (priority_multiplier * 100 + type_multiplier * 10) - | |
521 data.average_position(); | |
496 } | 522 } |
497 | 523 |
498 // static | 524 // static |
499 float ResourcePrefetchPredictorTables::ComputeRedirectScore( | 525 float ResourcePrefetchPredictorTables::ComputeRedirectScore( |
500 const RedirectStat& data) { | 526 const RedirectStat& data) { |
501 // TODO(alexilin): Invent some scoring. | 527 // TODO(alexilin): Invent some scoring. |
502 return 0.0; | 528 return 0.0; |
503 } | 529 } |
504 | 530 |
505 // static | 531 // static |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
678 return is_host ? kHostRedirectTableName : kUrlRedirectTableName; | 704 return is_host ? kHostRedirectTableName : kUrlRedirectTableName; |
679 case PrefetchDataType::METADATA: | 705 case PrefetchDataType::METADATA: |
680 return is_host ? kHostMetadataTableName : kUrlMetadataTableName; | 706 return is_host ? kHostMetadataTableName : kUrlMetadataTableName; |
681 } | 707 } |
682 | 708 |
683 NOTREACHED(); | 709 NOTREACHED(); |
684 return nullptr; | 710 return nullptr; |
685 } | 711 } |
686 | 712 |
687 } // namespace predictors | 713 } // namespace predictors |
OLD | NEW |