Skip to content

Commit

Permalink
Bug 836039 - Reduce cache size per SQLite connection to 2MB
Browse files Browse the repository at this point in the history
r=asuth
  • Loading branch information
mak77 committed Feb 21, 2013
1 parent 47a89b1 commit 7e4bc03
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 32 deletions.
25 changes: 4 additions & 21 deletions storage/src/mozStorageConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@

#define MIN_AVAILABLE_BYTES_PER_CHUNKED_GROWTH 524288000 // 500 MiB

// Maximum size of the pages cache per connection. If the default cache_size
// value evaluates to a larger size, it will be reduced to save memory.
#define MAX_CACHE_SIZE_BYTES 4194304 // 4 MiB

// Default maximum number of pages to allow in the connection pages cache.
#define DEFAULT_CACHE_SIZE_PAGES 2000
// Maximum size of the pages cache per connection.
#define MAX_CACHE_SIZE_KIBIBYTES 2048 // 2 MiB

#ifdef PR_LOGGING
PRLogModuleInfo* gStorageLog = nullptr;
Expand Down Expand Up @@ -576,26 +572,13 @@ Connection::initializeInternal(nsIFile* aDatabaseFile)
nsresult rv = ExecuteSimpleSQL(pageSizeQuery);
NS_ENSURE_SUCCESS(rv, rv);

// Get the current page_size, since it may differ from the specified value.
sqlite3_stmt *stmt;
NS_NAMED_LITERAL_CSTRING(pragma_page_size,
MOZ_STORAGE_UNIQUIFY_QUERY_STR "PRAGMA page_size");
int srv = prepareStatement(pragma_page_size, &stmt);
if (srv == SQLITE_OK) {
if (SQLITE_ROW == stepStatement(stmt)) {
pageSize = ::sqlite3_column_int64(stmt, 0);
}
(void)::sqlite3_finalize(stmt);
}

// Setting the cache_size forces the database open, verifying if it is valid
// or corrupt. So this is executed regardless it being actually needed.
// The cache_size is calculated from the actual page_size, to save memory.
nsAutoCString cacheSizeQuery(MOZ_STORAGE_UNIQUIFY_QUERY_STR
"PRAGMA cache_size = ");
cacheSizeQuery.AppendInt(std::min(DEFAULT_CACHE_SIZE_PAGES,
int32_t(MAX_CACHE_SIZE_BYTES / pageSize)));
srv = executeSql(cacheSizeQuery.get());
cacheSizeQuery.AppendInt(-MAX_CACHE_SIZE_KIBIBYTES);
int srv = executeSql(cacheSizeQuery.get());
if (srv != SQLITE_OK) {
::sqlite3_close(mDBConn);
mDBConn = nullptr;
Expand Down
13 changes: 3 additions & 10 deletions storage/test/unit/test_cache_size.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,7 @@ function new_file(name)

function run_test()
{
// This is copied from the logic in Connection::initialize().
function cacheSize(pageSize) {
const DEFAULT_CACHE_SIZE_PAGES = 2000;
const MAX_CACHE_SIZE_BYTES = 4 * 1024 * 1024;
return Math.min(DEFAULT_CACHE_SIZE_PAGES, MAX_CACHE_SIZE_BYTES / pageSize);
}
const kExpectedCacheSize = -2048; // 2MiB

let pageSizes = [
1024,
Expand All @@ -69,11 +64,9 @@ function run_test()

for (let i = 0; i < pageSizes.length; i++) {
let pageSize = pageSizes[i];
let expectedCacheSize = cacheSize(pageSize);
check_size(getDatabase,
new_file("shared" + pageSize), pageSize, expectedCacheSize);
new_file("shared" + pageSize), pageSize, kExpectedCacheSize);
check_size(getService().openUnsharedDatabase,
new_file("unshared" + pageSize), pageSize, expectedCacheSize);
new_file("unshared" + pageSize), pageSize, kExpectedCacheSize);
}
}

2 changes: 1 addition & 1 deletion storage/test/unit/test_page_size_is_32k.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// This file tests that dbs are using 32k pagesize

const kExpectedPageSize = 32768; // 32K
const kExpectedCacheSize = 128; // (4MiB / 32KiB)
const kExpectedCacheSize = -2048; // 2MiB

function check_size(db)
{
Expand Down

0 comments on commit 7e4bc03

Please sign in to comment.