Chromium Code Reviews| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 292 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 PrefetchKeyType key_type, | 303 PrefetchKeyType key_type, |
| 304 PrefetchDataMap* data_map) { | 304 PrefetchDataMap* data_map) { |
| 305 // Read the resources table and organize it per primary key. | 305 // Read the resources table and organize it per primary key. |
| 306 const char* table_name = GetTableName(key_type, PrefetchDataType::RESOURCE); | 306 const char* table_name = GetTableName(key_type, PrefetchDataType::RESOURCE); |
| 307 sql::Statement resource_reader(DB()->GetUniqueStatement( | 307 sql::Statement resource_reader(DB()->GetUniqueStatement( |
| 308 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); | 308 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); |
| 309 | 309 |
| 310 PrefetchData data; | 310 PrefetchData data; |
| 311 std::string key; | 311 std::string key; |
| 312 while (StepAndInitializeProtoData(&resource_reader, &key, &data)) { | 312 while (StepAndInitializeProtoData(&resource_reader, &key, &data)) { |
| 313 data_map->insert(std::make_pair(key, data)); | 313 data_map->insert({key, data}); |
| 314 DCHECK_EQ(data.primary_key(), key); | 314 DCHECK_EQ(data.primary_key(), key); |
| 315 } | 315 } |
| 316 | |
| 317 // Sort each of the resource vectors by score. | |
| 318 for (auto& kv : *data_map) { | |
|
alexilin
2017/04/21 12:07:28
Resources are written to the database already sort
Benoit L
2017/04/25 08:59:21
Unless the sorting function changes, right?
alexilin
2017/04/25 13:04:25
We could return this statement back if sorting fun
Benoit L
2017/04/25 15:12:39
Acknowledged.
| |
| 319 SortResources(&(kv.second)); | |
| 320 } | |
| 321 } | 316 } |
| 322 | 317 |
| 323 void ResourcePrefetchPredictorTables::GetAllRedirectDataHelper( | 318 void ResourcePrefetchPredictorTables::GetAllRedirectDataHelper( |
| 324 PrefetchKeyType key_type, | 319 PrefetchKeyType key_type, |
| 325 RedirectDataMap* data_map) { | 320 RedirectDataMap* data_map) { |
| 326 // Read the redirects table and organize it per primary key. | 321 // Read the redirects table and organize it per primary key. |
| 327 const char* table_name = GetTableName(key_type, PrefetchDataType::REDIRECT); | 322 const char* table_name = GetTableName(key_type, PrefetchDataType::REDIRECT); |
| 328 sql::Statement redirect_reader(DB()->GetUniqueStatement( | 323 sql::Statement redirect_reader(DB()->GetUniqueStatement( |
| 329 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); | 324 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); |
| 330 | 325 |
| 331 RedirectData data; | 326 RedirectData data; |
| 332 std::string key; | 327 std::string key; |
| 333 while (StepAndInitializeProtoData(&redirect_reader, &key, &data)) { | 328 while (StepAndInitializeProtoData(&redirect_reader, &key, &data)) { |
| 334 data_map->insert(std::make_pair(key, data)); | 329 data_map->insert({key, data}); |
| 335 DCHECK_EQ(data.primary_key(), key); | 330 DCHECK_EQ(data.primary_key(), key); |
| 336 } | 331 } |
| 337 } | 332 } |
| 338 | 333 |
| 339 void ResourcePrefetchPredictorTables::GetAllManifestDataHelper( | 334 void ResourcePrefetchPredictorTables::GetAllManifestDataHelper( |
| 340 ManifestDataMap* manifest_map) { | 335 ManifestDataMap* manifest_map) { |
| 341 sql::Statement manifest_reader(DB()->GetUniqueStatement( | 336 sql::Statement manifest_reader(DB()->GetUniqueStatement( |
| 342 base::StringPrintf(kSelectAllStatementTemplate, kManifestTableName) | 337 base::StringPrintf(kSelectAllStatementTemplate, kManifestTableName) |
| 343 .c_str())); | 338 .c_str())); |
| 344 | 339 |
| 345 precache::PrecacheManifest data; | 340 precache::PrecacheManifest data; |
| 346 std::string key; | 341 std::string key; |
| 347 while (StepAndInitializeProtoData(&manifest_reader, &key, &data)) { | 342 while (StepAndInitializeProtoData(&manifest_reader, &key, &data)) { |
| 348 manifest_map->insert(std::make_pair(key, data)); | 343 manifest_map->insert({key, data}); |
| 349 } | 344 } |
| 350 } | 345 } |
| 351 | 346 |
| 352 void ResourcePrefetchPredictorTables::GetAllOriginDataHelper( | 347 void ResourcePrefetchPredictorTables::GetAllOriginDataHelper( |
| 353 OriginDataMap* origin_map) { | 348 OriginDataMap* origin_map) { |
| 354 sql::Statement reader(DB()->GetUniqueStatement( | 349 sql::Statement reader(DB()->GetUniqueStatement( |
| 355 base::StringPrintf(kSelectAllStatementTemplate, kOriginTableName) | 350 base::StringPrintf(kSelectAllStatementTemplate, kOriginTableName) |
| 356 .c_str())); | 351 .c_str())); |
| 357 | 352 |
| 358 OriginData data; | 353 OriginData data; |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 440 default: | 435 default: |
| 441 type_multiplier = 1; | 436 type_multiplier = 1; |
| 442 } | 437 } |
| 443 | 438 |
| 444 constexpr int kMaxResourcesPerType = 100; | 439 constexpr int kMaxResourcesPerType = 100; |
| 445 return kMaxResourcesPerType * | 440 return kMaxResourcesPerType * |
| 446 (priority_multiplier * 100 + type_multiplier * 10) - | 441 (priority_multiplier * 100 + type_multiplier * 10) - |
| 447 data.average_position(); | 442 data.average_position(); |
| 448 } | 443 } |
| 449 | 444 |
| 445 // static | |
| 446 float ResourcePrefetchPredictorTables::ComputePrecacheResourceScore( | |
| 447 const precache::PrecacheResource& resource) { | |
| 448 int type_multiplier; | |
| 449 switch (resource.type()) { | |
|
Benoit L
2017/04/25 08:59:21
Can we make that consistent with the other scoring
alexilin
2017/04/25 17:37:05
Done.
| |
| 450 case precache::PrecacheResource::RESOURCE_TYPE_FONT: | |
| 451 type_multiplier = 4; | |
| 452 break; | |
| 453 case precache::PrecacheResource::RESOURCE_TYPE_STYLESHEET: | |
| 454 type_multiplier = 3; | |
| 455 break; | |
| 456 case precache::PrecacheResource::RESOURCE_TYPE_SCRIPT: | |
| 457 type_multiplier = 2; | |
| 458 break; | |
| 459 case precache::PrecacheResource::RESOURCE_TYPE_IMAGE: | |
| 460 type_multiplier = 1; | |
| 461 break; | |
| 462 default: | |
| 463 type_multiplier = 0; | |
| 464 } | |
| 465 | |
| 466 return type_multiplier * 10 + resource.weight_ratio(); | |
| 467 } | |
| 468 | |
| 469 // static | |
| 450 float ResourcePrefetchPredictorTables::ComputeOriginScore( | 470 float ResourcePrefetchPredictorTables::ComputeOriginScore( |
| 451 const OriginStat& origin) { | 471 const OriginStat& origin) { |
| 452 // The ranking is done by considering, in this order: | 472 // The ranking is done by considering, in this order: |
| 453 // 1. High confidence resources (>75% and more than 10 hits) | 473 // 1. High confidence resources (>75% and more than 10 hits) |
| 454 // 2. Mandatory network access | 474 // 2. Mandatory network access |
| 455 // 3. Network accessed | 475 // 3. Network accessed |
| 456 // 4. Average position (decreasing) | 476 // 4. Average position (decreasing) |
| 457 float score = 0; | 477 float score = 0; |
| 458 float confidence = static_cast<float>(origin.number_of_hits()) / | 478 float confidence = static_cast<float>(origin.number_of_hits()) / |
| 459 (origin.number_of_hits() + origin.number_of_misses()); | 479 (origin.number_of_hits() + origin.number_of_misses()); |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 609 return kManifestTableName; | 629 return kManifestTableName; |
| 610 case PrefetchDataType::ORIGIN: | 630 case PrefetchDataType::ORIGIN: |
| 611 return kOriginTableName; | 631 return kOriginTableName; |
| 612 } | 632 } |
| 613 | 633 |
| 614 NOTREACHED(); | 634 NOTREACHED(); |
| 615 return nullptr; | 635 return nullptr; |
| 616 } | 636 } |
| 617 | 637 |
| 618 } // namespace predictors | 638 } // namespace predictors |
| OLD | NEW |