diff options
author | shess <shess@chromium.org> | 2015-11-05 12:47:42 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-11-05 20:48:28 +0000 |
commit | 5dac334f21d99d9f97d9c7bbfbba40c8e872545b (patch) | |
tree | b1b16e039b04c67dfb3c2b393327e7f18910d7ec /sql | |
parent | e725951c877cc38a6fc998da276a44ece8014a60 (diff) | |
download | chromium_src-5dac334f21d99d9f97d9c7bbfbba40c8e872545b.zip chromium_src-5dac334f21d99d9f97d9c7bbfbba40c8e872545b.tar.gz chromium_src-5dac334f21d99d9f97d9c7bbfbba40c8e872545b.tar.bz2 |
[sql] Increase database chunk size for large databases.
SQLite implements a hint to request the databases be truncated or
expanded to multiples of a chunk size. This was originally implemented
to reduce filesystem fragmentation, and it also can improve performance
because file sizes don't vary as much.
For a memory-mapped database, it also reduces the number of calls to
adjust the memory map when the database size changes.
BUG=551108
Review URL: https://codereview.chromium.org/1402373007
Cr-Commit-Position: refs/heads/master@{#358121}
Diffstat (limited to 'sql')
-rw-r--r-- | sql/connection.cc | 33 |
1 files changed, 28 insertions, 5 deletions
diff --git a/sql/connection.cc b/sql/connection.cc index 79264916..3d584a6 100644 --- a/sql/connection.cc +++ b/sql/connection.cc @@ -190,6 +190,16 @@ int GetSqlite3File(sqlite3* db, sqlite3_file** file) { return rc; } +// Convenience to get the sqlite3_file* and the size for the "main" database. +int GetSqlite3FileAndSize(sqlite3* db, + sqlite3_file** file, sqlite3_int64* db_size) { + int rc = GetSqlite3File(db, file); + if (rc != SQLITE_OK) + return rc; + + return (*file)->pMethods->xFileSize(*file, db_size); +} + // This should match UMA_HISTOGRAM_MEDIUM_TIMES(). base::HistogramBase* GetMediumTimeHistogram(const std::string& name) { return base::Histogram::FactoryTimeGet( @@ -515,12 +525,8 @@ void Connection::Preload() { return; sqlite3_file* file = NULL; - int rc = GetSqlite3File(db_, &file); - if (rc != SQLITE_OK) - return; - sqlite3_int64 file_size = 0; - rc = file->pMethods->xFileSize(file, &file_size); + int rc = GetSqlite3FileAndSize(db_, &file, &file_size); if (rc != SQLITE_OK) return; @@ -1656,6 +1662,23 @@ bool Connection::OpenInternal(const std::string& file_name, return false; } + // Set a reasonable chunk size for larger files. This reduces churn from + // remapping memory on size changes. It also reduces filesystem + // fragmentation. + // TODO(shess): It may make sense to have this be hinted by the client. + // Database sizes seem to be bimodal, some clients have consistently small + // databases (<20k) while other clients have a broad distribution of sizes + // (hundreds of kilobytes to many megabytes). + sqlite3_file* file = NULL; + sqlite3_int64 db_size = 0; + int rc = GetSqlite3FileAndSize(db_, &file, &db_size); + if (rc == SQLITE_OK && db_size > 16 * 1024) { + int chunk_size = 4 * 1024; + if (db_size > 128 * 1024) + chunk_size = 32 * 1024; + sqlite3_file_control(db_, NULL, SQLITE_FCNTL_CHUNK_SIZE, &chunk_size); + } + // Enable memory-mapped access. The explicit-disable case is because SQLite // can be built to default-enable mmap. This value will be capped by // SQLITE_MAX_MMAP_SIZE, which could be different between 32-bit and 64-bit |