Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(4509)

Unified Diff: chrome/browser/predictors/resource_prefetch_predictor_tables.cc

Issue 10416002: Seculative resource prefetching for URLs CL. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/predictors/resource_prefetch_predictor_tables.cc
diff --git a/chrome/browser/predictors/resource_prefetch_predictor_tables.cc b/chrome/browser/predictors/resource_prefetch_predictor_tables.cc
new file mode 100644
index 0000000000000000000000000000000000000000..a8ed76431d24a83dd80aa4d5de4db3f5c3a06cad
--- /dev/null
+++ b/chrome/browser/predictors/resource_prefetch_predictor_tables.cc
@@ -0,0 +1,218 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/predictors/resource_prefetch_predictor_tables.h"
+
+#include "base/logging.h"
+#include "base/metrics/histogram.h"
+#include "base/stringprintf.h"
+#include "content/public/browser/browser_thread.h"
+#include "sql/statement.h"
+
+using content::BrowserThread;
+
+namespace {
+
+const char kResourcePredictorUrlTableName[] = "resource_prefetch_predictor_url";
+
+// The maximum length allowed for strings in the database.
+const size_t kMaxURLLength = 2048;
+
+void BindUrlTableRowToStatement(
+ const predictors::ResourcePrefetchPredictorTables::UrlTableRow& row,
+ sql::Statement* statement) {
+ statement->BindString(0, row.main_frame_url_.spec().substr(0, kMaxURLLength));
+ statement->BindString(1, row.resource_url_.spec().substr(0, kMaxURLLength));
+ statement->BindInt(2, static_cast<int>(row.resource_type_));
+ statement->BindInt(3, row.number_of_hits_);
+ statement->BindInt(4, row.number_of_misses_);
+ statement->BindInt(5, row.consecutive_misses_);
+ statement->BindDouble(6, row.average_position_);
+}
+
+bool StepAndInitializeUrlTableRow(
+ sql::Statement* statement,
+ predictors::ResourcePrefetchPredictorTables::UrlTableRow* row) {
+ if (!statement->Step())
+ return false;
+
+ row->main_frame_url_ = GURL(statement->ColumnString(0));
+ row->resource_url_ = GURL(statement->ColumnString(1));
+ row->resource_type_ = ResourceType::FromInt(statement->ColumnInt(2));
+ row->number_of_hits_ = statement->ColumnInt(3);
+ row->number_of_misses_ = statement->ColumnInt(4);
+ row->consecutive_misses_ = statement->ColumnInt(5);
+ row->average_position_ = statement->ColumnDouble(6);
+ return true;
+}
+
+} // namespace
+
+namespace predictors {
+
+ResourcePrefetchPredictorTables::UrlTableRow::UrlTableRow()
+ : resource_type_(ResourceType::LAST_TYPE),
+ number_of_hits_(0),
+ number_of_misses_(0),
+ consecutive_misses_(0),
+ average_position_(0),
dominich 2012/05/21 16:16:53 warning: initializing a double with an int.
Shishir 2012/05/23 01:46:46 Done.
+ score_(0.0) {
+}
+
+ResourcePrefetchPredictorTables::UrlTableRow::UrlTableRow(
+ const UrlTableRow& other)
+ : main_frame_url_(other.main_frame_url_),
+ resource_url_(other.resource_url_),
+ resource_type_(other.resource_type_),
+ number_of_hits_(other.number_of_hits_),
+ number_of_misses_(other.number_of_misses_),
+ consecutive_misses_(other.consecutive_misses_),
+ average_position_(other.average_position_),
+ score_(other.score_) {
+}
+
+void ResourcePrefetchPredictorTables::UrlTableRow::UpdateScore() {
dominich 2012/05/21 16:16:53 Comment explaining the heuristic here. Also, might
Shishir 2012/05/23 01:46:46 Added comment. We might have additional field tria
+ static const int kMaxResourcesPerType = 100;
+ switch (resource_type_) {
+ case ResourceType::STYLESHEET:
+ case ResourceType::SCRIPT:
+ score_ = (2 * kMaxResourcesPerType) - average_position_;
+ break;
+
+ case ResourceType::IMAGE:
+ score_ = kMaxResourcesPerType - average_position_;
+ break;
+
+ default:
dominich 2012/05/21 16:16:53 What other resource types should be handled? Maybe
Shishir 2012/05/23 01:46:46 Sometimes we do not know the type of resource, eg
+ score_ = kMaxResourcesPerType - average_position_;
+ break;
+ }
+}
+
+bool ResourcePrefetchPredictorTables::UrlTableRowSorter::operator()(
+ const UrlTableRow& x,
+ const UrlTableRow& y) const {
+ return x.score_ > y.score_;
+}
+
+ResourcePrefetchPredictorTables::ResourcePrefetchPredictorTables()
+ : PredictorTableBase() {
+}
+
+ResourcePrefetchPredictorTables::~ResourcePrefetchPredictorTables() {
+}
+
+void ResourcePrefetchPredictorTables::CreateTableIfNonExistent() {
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
+ if (CantAccessDatabase())
+ return;
+
+ std::string url_table_creation_statement = base::StringPrintf(
+ "CREATE TABLE %s ( "
+ "main_page_url TEXT, "
+ "resource_url TEXT, "
+ "resource_type INTEGER, "
+ "number_of_hits INTEGER, "
+ "number_of_misses INTEGER, "
+ "consecutive_misses INTEGER, "
+ "average_position DOUBLE, "
+ "PRIMARY KEY(main_page_url, resource_url))",
+ kResourcePredictorUrlTableName);
+
+ if (!DB()->DoesTableExist(kResourcePredictorUrlTableName))
dominich 2012/05/21 16:16:53 add braces as the inner if has them.
Shishir 2012/05/23 01:46:46 Done.
+ if (!DB()->Execute(url_table_creation_statement.c_str())) {
+ ResetDB();
+ return;
+ }
+}
+
+void ResourcePrefetchPredictorTables::LogDatabaseStats() {
dominich 2012/05/21 16:16:53 Consider tracking the raw file size.
Shishir 2012/05/23 01:46:46 That happens in the PredictorDatabaseInternal.
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
+ if (CantAccessDatabase())
+ return;
+
+ sql::Statement url_statement(DB()->GetUniqueStatement(
+ base::StringPrintf("SELECT count(*) FROM %s",
+ kResourcePredictorUrlTableName).c_str()));
+ if (url_statement.Step())
+ UMA_HISTOGRAM_COUNTS("ResourcePrefetchPredictor.UrlTableRowCount",
+ url_statement.ColumnInt(0));
+}
+
+void ResourcePrefetchPredictorTables::GetAllRows(
+ std::vector<UrlTableRow>* url_row_buffer) {
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
+ if (CantAccessDatabase())
+ return;
+
+ CHECK(url_row_buffer);
+ url_row_buffer->clear();
+ sql::Statement url_statement(DB()->GetCachedStatement(SQL_FROM_HERE,
+ base::StringPrintf("SELECT * FROM %s",
+ kResourcePredictorUrlTableName).c_str()));
+
+ UrlTableRow url_row;
+ while (StepAndInitializeUrlTableRow(&url_statement, &url_row)) {
+ url_row.UpdateScore();
dominich 2012/05/21 16:16:53 why don't you store the score?
Shishir 2012/05/23 01:46:46 Not storing it right now because we want to run ex
+ url_row_buffer->push_back(url_row);
+ }
+}
+
+void ResourcePrefetchPredictorTables::UpdateRowsForUrl(
+ const GURL& main_page_url,
+ const std::vector<UrlTableRow>& row_buffer) {
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
+ if (CantAccessDatabase())
+ return;
+
+ DB()->BeginTransaction();
+
+ sql::Statement delete_statement(DB()->GetUniqueStatement(
dominich 2012/05/21 16:16:53 would it be more optimal to SELECT and then UPDATE
Shishir 2012/05/23 01:46:46 May not be, since we will end up updating all the
+ base::StringPrintf("DELETE FROM %s WHERE main_page_url='%s'",
+ kResourcePredictorUrlTableName,
+ main_page_url.spec().c_str()).c_str()));
+ if (!delete_statement.Run()) {
+ DB()->RollbackTransaction();
+ return;
+ }
+
+ for (int i = 0; i < static_cast<int>(row_buffer.size()); ++i) {
+ const UrlTableRow& row = row_buffer.at(i);
+
+ sql::Statement add_statement(
+ DB()->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf(
+ "INSERT INTO %s "
+ "(main_page_url, resource_url, resource_type, number_of_hits, "
+ "number_of_misses, consecutive_misses, average_position) "
+ "VALUES (?,?,?,?,?,?,?)",
+ kResourcePredictorUrlTableName).c_str()));
+ BindUrlTableRowToStatement(row, &add_statement);
+ if (!add_statement.Run()) {
+ DB()->RollbackTransaction();
+ return;
+ }
+ }
+
+ DB()->CommitTransaction();
+}
+
+void ResourcePrefetchPredictorTables::DeleteUrlRows(
+ const std::vector<GURL>& urls) {
+ CHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
+ if (CantAccessDatabase())
+ return;
+
+ // These do not need to be a transaction.
+ for (int i = 0; i < static_cast<int>(urls.size()); ++i) {
+ sql::Statement delete_statement(DB()->GetCachedStatement(
+ SQL_FROM_HERE,
+ base::StringPrintf("DELETE FROM %s WHERE main_page_url='%s'",
+ kResourcePredictorUrlTableName,
+ urls[i].spec().c_str()).c_str()));
+ }
+}
+
+} // namespace predictors

Powered by Google App Engine
This is Rietveld 408576698