Bug 717575 - Places should fallback to default cache size, excluding autocomplete.
authorMarco Bonardo <mbonardo@mozilla.com>
Mon, 13 Feb 2012 16:47:21 +0100
changeset 86725 c63b7b310d5f43db40ba58a2d5326c13680969bd
parent 86724 e652994cdd9ee79e67c0e1207366175a244c9e99
child 86726 bd04ba7c9996460fd6c83f6afb2c5047612d0082
push idunknown
push userunknown
push dateunknown
bugs717575
milestone13.0a1
Bug 717575 - Places should fallback to default cache size, excluding autocomplete. r=dietrich
toolkit/components/places/Database.cpp
toolkit/components/places/nsPlacesAutoComplete.js
--- a/toolkit/components/places/Database.cpp
+++ b/toolkit/components/places/Database.cpp
@@ -77,26 +77,16 @@
 // Filename of the database.
 #define DATABASE_FILENAME NS_LITERAL_STRING("places.sqlite")
 // Filename used to backup corrupt databases.
 #define DATABASE_CORRUPT_FILENAME NS_LITERAL_STRING("places.sqlite.corrupt")
 
 // Set when the database file was found corrupt by a previous maintenance.
 #define PREF_FORCE_DATABASE_REPLACEMENT "places.database.replaceOnStartup"
 
-// The wanted size of the cache.  This is calculated based on current database
-// size and clamped to the limits specified below.
-#define DATABASE_CACHE_TO_DATABASE_PERC 10
-// The minimum size of the cache.  We should never work without a cache, since
-// that would badly hurt WAL journaling mode.
-#define DATABASE_CACHE_MIN_BYTES (PRUint64)4194304 // 4MiB
-// The maximum size of the cache.  This is the maximum memory that each
-// connection may use.
-#define DATABASE_CACHE_MAX_BYTES (PRUint64)8388608 // 8MiB
-
 // Maximum size for the WAL file.  It should be small enough since in case of
 // crashes we could lose all the transactions in the file.  But a too small
 // file could hurt performance.
 #define DATABASE_MAX_WAL_SIZE_IN_KIBIBYTES 512
 
 #define BYTES_PER_MEBIBYTE 1048576
 
 // Old Sync GUID annotation.
@@ -562,49 +552,16 @@ Database::InitSchema(bool* aDatabaseMigr
     NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && mDBPageSize > 0, NS_ERROR_UNEXPECTED);
   }
 
   // Ensure that temp tables are held in memory, not on disk.
   nsresult rv = mMainConn->ExecuteSimpleSQL(NS_LITERAL_CSTRING(
       MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA temp_store = MEMORY"));
   NS_ENSURE_SUCCESS(rv, rv);
 
-  // Get the current database size. Due to chunked growth we have to use
-  // page_count to evaluate it.
-  PRUint64 databaseSizeBytes = 0;
-  {
-    nsCOMPtr<mozIStorageStatement> statement;
-    nsresult rv = mMainConn->CreateStatement(NS_LITERAL_CSTRING(
-      "PRAGMA page_count"
-    ), getter_AddRefs(statement));
-    NS_ENSURE_SUCCESS(rv, rv);
-    bool hasResult = false;
-    rv = statement->ExecuteStep(&hasResult);
-    NS_ENSURE_TRUE(NS_SUCCEEDED(rv) && hasResult, NS_ERROR_FAILURE);
-    PRInt32 pageCount = 0;
-    rv = statement->GetInt32(0, &pageCount);
-    NS_ENSURE_SUCCESS(rv, rv);
-    databaseSizeBytes = pageCount * mDBPageSize;
-  }
-
-  // Clamp the cache size to a percentage of the database size, forcing
-  // meaningful limits.
-  PRInt64 cacheSize = clamped(databaseSizeBytes *  DATABASE_CACHE_TO_DATABASE_PERC / 100,
-                              DATABASE_CACHE_MIN_BYTES,
-                              DATABASE_CACHE_MAX_BYTES);
-
-  // Set the number of cached pages.
-  // We don't use PRAGMA default_cache_size, since the database could be moved
-  // among different devices and the value would adapt accordingly.
-  nsCAutoString cacheSizePragma(MOZ_STORAGE_UNIQUIFY_QUERY_STR
-				"PRAGMA cache_size = ");
-  cacheSizePragma.AppendInt(cacheSize / mDBPageSize);
-  rv = mMainConn->ExecuteSimpleSQL(cacheSizePragma);
-  NS_ENSURE_SUCCESS(rv, rv);
-
   // Be sure to set journal mode after page_size.  WAL would prevent the change
   // otherwise.
   if (NS_SUCCEEDED(SetJournalMode(mMainConn, JOURNAL_WAL))) {
     // Set the WAL journal size limit.  We want it to be small, since in
     // synchronous = NORMAL mode a crash could cause loss of all the
     // transactions in the journal.  For added safety we will also force
     // checkpointing at strategic moments.
     PRInt32 checkpointPages =
--- a/toolkit/components/places/nsPlacesAutoComplete.js
+++ b/toolkit/components/places/nsPlacesAutoComplete.js
@@ -314,16 +314,23 @@ function nsPlacesAutoComplete()
     // to our own in-memory temp table, and having a cloned copy means we do not
     // run the risk of our queries taking longer due to the main database
     // connection performing a long-running task.
     let db = Cc["@mozilla.org/browser/nav-history-service;1"].
              getService(Ci.nsPIPlacesDatabase).
              DBConnection.
              clone(true);
 
+    // Autocomplete often fallbacks to a table scan due to lack of text indices.
+    // In such cases a larger cache helps reducing IO.  The default Storage
+    // value is MAX_CACHE_SIZE_BYTES in storage/src/mozStorageConnection.cpp.
+    let stmt = db.createAsyncStatement("PRAGMA cache_size = -6144"); // 6MiB
+    stmt.executeAsync();
+    stmt.finalize();
+
     // Create our in-memory tables for tab tracking.
     initTempTable(db);
 
     // Populate the table with current open pages cache contents.
     if (this._openPagesCache.length > 0) {
       // Avoid getter re-entrance from the _registerOpenPageQuery lazy getter.
       let stmt = this._registerOpenPageQuery =
         db.createAsyncStatement(this._registerOpenPageQuerySQL);