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 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 *key = statement->ColumnString(0); | 62 *key = statement->ColumnString(0); |
63 | 63 |
64 int size = statement->ColumnByteLength(1); | 64 int size = statement->ColumnByteLength(1); |
65 const void* blob = statement->ColumnBlob(1); | 65 const void* blob = statement->ColumnBlob(1); |
66 DCHECK(blob); | 66 DCHECK(blob); |
67 data->ParseFromArray(blob, size); | 67 data->ParseFromArray(blob, size); |
68 | 68 |
69 return true; | 69 return true; |
70 } | 70 } |
71 | 71 |
72 predictors::ResourceData::ResourceType PrecacheResourceTypeToResourceType( | |
73 precache::PrecacheResource::Type resource_type) { | |
74 using precache::PrecacheResource; | |
75 using predictors::ResourceData; | |
76 switch (resource_type) { | |
77 case PrecacheResource::RESOURCE_TYPE_IMAGE: | |
78 return ResourceData::RESOURCE_TYPE_IMAGE; | |
79 case PrecacheResource::RESOURCE_TYPE_FONT: | |
80 return ResourceData::RESOURCE_TYPE_FONT_RESOURCE; | |
81 case PrecacheResource::RESOURCE_TYPE_STYLESHEET: | |
82 return ResourceData::RESOURCE_TYPE_STYLESHEET; | |
83 case PrecacheResource::RESOURCE_TYPE_SCRIPT: | |
84 return ResourceData::RESOURCE_TYPE_SCRIPT; | |
85 case PrecacheResource::RESOURCE_TYPE_OTHER: | |
86 case PrecacheResource::RESOURCE_TYPE_UNKNOWN: | |
87 default: | |
88 return ResourceData::RESOURCE_TYPE_SUB_RESOURCE; | |
89 } | |
90 } | |
91 | |
92 int GetResourceTypeMultiplier( | |
93 predictors::ResourceData::ResourceType resource_type) { | |
94 switch (resource_type) { | |
95 case predictors::ResourceData::RESOURCE_TYPE_STYLESHEET: | |
96 return 4; | |
97 case predictors::ResourceData::RESOURCE_TYPE_SCRIPT: | |
98 return 3; | |
99 case predictors::ResourceData::RESOURCE_TYPE_FONT_RESOURCE: | |
alexilin
2017/04/25 17:37:05
Actually, I'm also thinking about prioritizing fon
Benoit L
2017/04/27 08:54:15
Well, this is because we start fetching font at th
alexilin
2017/04/27 15:32:23
Agree. Thanks for the explanation!
| |
100 return 2; | |
101 case predictors::ResourceData::RESOURCE_TYPE_IMAGE: | |
102 default: | |
103 return 1; | |
104 } | |
105 } | |
106 | |
72 } // namespace | 107 } // namespace |
73 | 108 |
74 namespace predictors { | 109 namespace predictors { |
75 | 110 |
76 using content::BrowserThread; | 111 using content::BrowserThread; |
77 | 112 |
78 // static | 113 // static |
79 void ResourcePrefetchPredictorTables::TrimResources( | 114 void ResourcePrefetchPredictorTables::TrimResources( |
80 PrefetchData* data, | 115 PrefetchData* data, |
81 size_t max_consecutive_misses) { | 116 size_t max_consecutive_misses) { |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 PrefetchKeyType key_type, | 338 PrefetchKeyType key_type, |
304 PrefetchDataMap* data_map) { | 339 PrefetchDataMap* data_map) { |
305 // Read the resources table and organize it per primary key. | 340 // Read the resources table and organize it per primary key. |
306 const char* table_name = GetTableName(key_type, PrefetchDataType::RESOURCE); | 341 const char* table_name = GetTableName(key_type, PrefetchDataType::RESOURCE); |
307 sql::Statement resource_reader(DB()->GetUniqueStatement( | 342 sql::Statement resource_reader(DB()->GetUniqueStatement( |
308 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); | 343 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); |
309 | 344 |
310 PrefetchData data; | 345 PrefetchData data; |
311 std::string key; | 346 std::string key; |
312 while (StepAndInitializeProtoData(&resource_reader, &key, &data)) { | 347 while (StepAndInitializeProtoData(&resource_reader, &key, &data)) { |
313 data_map->insert(std::make_pair(key, data)); | 348 data_map->insert({key, data}); |
314 DCHECK_EQ(data.primary_key(), key); | 349 DCHECK_EQ(data.primary_key(), key); |
315 } | 350 } |
316 | |
317 // Sort each of the resource vectors by score. | |
318 for (auto& kv : *data_map) { | |
319 SortResources(&(kv.second)); | |
320 } | |
321 } | 351 } |
322 | 352 |
323 void ResourcePrefetchPredictorTables::GetAllRedirectDataHelper( | 353 void ResourcePrefetchPredictorTables::GetAllRedirectDataHelper( |
324 PrefetchKeyType key_type, | 354 PrefetchKeyType key_type, |
325 RedirectDataMap* data_map) { | 355 RedirectDataMap* data_map) { |
326 // Read the redirects table and organize it per primary key. | 356 // Read the redirects table and organize it per primary key. |
327 const char* table_name = GetTableName(key_type, PrefetchDataType::REDIRECT); | 357 const char* table_name = GetTableName(key_type, PrefetchDataType::REDIRECT); |
328 sql::Statement redirect_reader(DB()->GetUniqueStatement( | 358 sql::Statement redirect_reader(DB()->GetUniqueStatement( |
329 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); | 359 base::StringPrintf(kSelectAllStatementTemplate, table_name).c_str())); |
330 | 360 |
331 RedirectData data; | 361 RedirectData data; |
332 std::string key; | 362 std::string key; |
333 while (StepAndInitializeProtoData(&redirect_reader, &key, &data)) { | 363 while (StepAndInitializeProtoData(&redirect_reader, &key, &data)) { |
334 data_map->insert(std::make_pair(key, data)); | 364 data_map->insert({key, data}); |
335 DCHECK_EQ(data.primary_key(), key); | 365 DCHECK_EQ(data.primary_key(), key); |
336 } | 366 } |
337 } | 367 } |
338 | 368 |
339 void ResourcePrefetchPredictorTables::GetAllManifestDataHelper( | 369 void ResourcePrefetchPredictorTables::GetAllManifestDataHelper( |
340 ManifestDataMap* manifest_map) { | 370 ManifestDataMap* manifest_map) { |
341 sql::Statement manifest_reader(DB()->GetUniqueStatement( | 371 sql::Statement manifest_reader(DB()->GetUniqueStatement( |
342 base::StringPrintf(kSelectAllStatementTemplate, kManifestTableName) | 372 base::StringPrintf(kSelectAllStatementTemplate, kManifestTableName) |
343 .c_str())); | 373 .c_str())); |
344 | 374 |
345 precache::PrecacheManifest data; | 375 precache::PrecacheManifest data; |
346 std::string key; | 376 std::string key; |
347 while (StepAndInitializeProtoData(&manifest_reader, &key, &data)) { | 377 while (StepAndInitializeProtoData(&manifest_reader, &key, &data)) { |
348 manifest_map->insert(std::make_pair(key, data)); | 378 manifest_map->insert({key, data}); |
349 } | 379 } |
350 } | 380 } |
351 | 381 |
352 void ResourcePrefetchPredictorTables::GetAllOriginDataHelper( | 382 void ResourcePrefetchPredictorTables::GetAllOriginDataHelper( |
353 OriginDataMap* origin_map) { | 383 OriginDataMap* origin_map) { |
354 sql::Statement reader(DB()->GetUniqueStatement( | 384 sql::Statement reader(DB()->GetUniqueStatement( |
355 base::StringPrintf(kSelectAllStatementTemplate, kOriginTableName) | 385 base::StringPrintf(kSelectAllStatementTemplate, kOriginTableName) |
356 .c_str())); | 386 .c_str())); |
357 | 387 |
358 OriginData data; | 388 OriginData data; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 priority_multiplier = 2; | 450 priority_multiplier = 2; |
421 break; | 451 break; |
422 case ResourceData::REQUEST_PRIORITY_LOW: | 452 case ResourceData::REQUEST_PRIORITY_LOW: |
423 case ResourceData::REQUEST_PRIORITY_LOWEST: | 453 case ResourceData::REQUEST_PRIORITY_LOWEST: |
424 case ResourceData::REQUEST_PRIORITY_IDLE: | 454 case ResourceData::REQUEST_PRIORITY_IDLE: |
425 default: | 455 default: |
426 priority_multiplier = 1; | 456 priority_multiplier = 1; |
427 break; | 457 break; |
428 } | 458 } |
429 | 459 |
430 int type_multiplier; | 460 int type_multiplier = GetResourceTypeMultiplier(data.resource_type()); |
431 switch (data.resource_type()) { | |
432 case ResourceData::RESOURCE_TYPE_STYLESHEET: | |
433 case ResourceData::RESOURCE_TYPE_SCRIPT: | |
434 type_multiplier = 3; | |
435 break; | |
436 case ResourceData::RESOURCE_TYPE_FONT_RESOURCE: | |
437 type_multiplier = 2; | |
438 break; | |
439 case ResourceData::RESOURCE_TYPE_IMAGE: | |
440 default: | |
441 type_multiplier = 1; | |
442 } | |
443 | 461 |
444 constexpr int kMaxResourcesPerType = 100; | 462 constexpr int kMaxResourcesPerType = 100; |
445 return kMaxResourcesPerType * | 463 return kMaxResourcesPerType * |
446 (priority_multiplier * 100 + type_multiplier * 10) - | 464 (priority_multiplier * 100 + type_multiplier * 10) - |
447 data.average_position(); | 465 data.average_position(); |
448 } | 466 } |
449 | 467 |
468 // static | |
469 float ResourcePrefetchPredictorTables::ComputePrecacheResourceScore( | |
470 const precache::PrecacheResource& resource) { | |
471 int type_multiplier = GetResourceTypeMultiplier( | |
472 PrecacheResourceTypeToResourceType(resource.type())); | |
473 // This means a strict ordering, since the weight_ratio is in [0,1). | |
474 return type_multiplier * 10 + resource.weight_ratio(); | |
475 } | |
476 | |
477 // static | |
450 float ResourcePrefetchPredictorTables::ComputeOriginScore( | 478 float ResourcePrefetchPredictorTables::ComputeOriginScore( |
451 const OriginStat& origin) { | 479 const OriginStat& origin) { |
452 // The ranking is done by considering, in this order: | 480 // The ranking is done by considering, in this order: |
453 // 1. High confidence resources (>75% and more than 10 hits) | 481 // 1. High confidence resources (>75% and more than 10 hits) |
454 // 2. Mandatory network access | 482 // 2. Mandatory network access |
455 // 3. Network accessed | 483 // 3. Network accessed |
456 // 4. Average position (decreasing) | 484 // 4. Average position (decreasing) |
457 float score = 0; | 485 float score = 0; |
458 float confidence = static_cast<float>(origin.number_of_hits()) / | 486 float confidence = static_cast<float>(origin.number_of_hits()) / |
459 (origin.number_of_hits() + origin.number_of_misses()); | 487 (origin.number_of_hits() + origin.number_of_misses()); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
609 return kManifestTableName; | 637 return kManifestTableName; |
610 case PrefetchDataType::ORIGIN: | 638 case PrefetchDataType::ORIGIN: |
611 return kOriginTableName; | 639 return kOriginTableName; |
612 } | 640 } |
613 | 641 |
614 NOTREACHED(); | 642 NOTREACHED(); |
615 return nullptr; | 643 return nullptr; |
616 } | 644 } |
617 | 645 |
618 } // namespace predictors | 646 } // namespace predictors |
OLD | NEW |