From f5b0b9ceea456b019ae7d55f15f3496c3e270f2b Mon Sep 17 00:00:00 2001 From: Ben Turner Date: Tue, 2 Apr 2013 15:09:14 -0700 Subject: [PATCH] Bug 857283 - 'Smarter caching of index update queries'. r=khuey. --HG-- extra : rebase_source : 489638cea833b32d05471188cf3a0b96947404cc --- dom/indexedDB/IDBObjectStore.cpp | 51 ++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 22 deletions(-) diff --git a/dom/indexedDB/IDBObjectStore.cpp b/dom/indexedDB/IDBObjectStore.cpp index 9aca9b8dc516..9d753db74cc6 100644 --- a/dom/indexedDB/IDBObjectStore.cpp +++ b/dom/indexedDB/IDBObjectStore.cpp @@ -979,7 +979,6 @@ IDBObjectStore::UpdateIndexes(IDBTransaction* aTransaction, int64_t aObjectDataId, const nsTArray& aUpdateInfoArray) { - nsCOMPtr stmt; nsresult rv; NS_ASSERTION(aObjectDataId != INT64_MIN, "Bad objectData id!"); @@ -987,41 +986,49 @@ IDBObjectStore::UpdateIndexes(IDBTransaction* aTransaction, NS_NAMED_LITERAL_CSTRING(objectDataId, "object_data_id"); if (aOverwrite) { - stmt = aTransaction->GetCachedStatement( - "DELETE FROM unique_index_data " - "WHERE object_data_id = :object_data_id; " - "DELETE FROM index_data " - "WHERE object_data_id = :object_data_id"); - NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE); + nsCOMPtr deleteStmt = + aTransaction->GetCachedStatement( + "DELETE FROM unique_index_data " + "WHERE object_data_id = :object_data_id; " + "DELETE FROM index_data " + "WHERE object_data_id = :object_data_id"); + NS_ENSURE_TRUE(deleteStmt, NS_ERROR_FAILURE); - mozStorageStatementScoper scoper(stmt); + mozStorageStatementScoper scoper(deleteStmt); - rv = stmt->BindInt64ByName(objectDataId, aObjectDataId); + rv = deleteStmt->BindInt64ByName(objectDataId, aObjectDataId); NS_ENSURE_SUCCESS(rv, rv); - rv = stmt->Execute(); + rv = deleteStmt->Execute(); NS_ENSURE_SUCCESS(rv, rv); } + // Avoid lots of hash lookups for objectStores with lots of indexes by lazily + // holding the necessary statements on the stack outside the loop. + nsCOMPtr insertUniqueStmt; + nsCOMPtr insertStmt; + uint32_t infoCount = aUpdateInfoArray.Length(); for (uint32_t i = 0; i < infoCount; i++) { const IndexUpdateInfo& updateInfo = aUpdateInfoArray[i]; - // Insert new values. - - stmt = updateInfo.indexUnique ? - aTransaction->GetCachedStatement( - "INSERT INTO unique_index_data " - "(index_id, object_data_id, object_data_key, value) " - "VALUES (:index_id, :object_data_id, :object_data_key, :value)") : - aTransaction->GetCachedStatement( - "INSERT OR IGNORE INTO index_data (" - "index_id, object_data_id, object_data_key, value) " - "VALUES (:index_id, :object_data_id, :object_data_key, :value)"); + nsCOMPtr& stmt = + updateInfo.indexUnique ? insertUniqueStmt : insertStmt; + if (!stmt) { + stmt = updateInfo.indexUnique ? + aTransaction->GetCachedStatement( + "INSERT INTO unique_index_data " + "(index_id, object_data_id, object_data_key, value) " + "VALUES (:index_id, :object_data_id, :object_data_key, :value)") : + aTransaction->GetCachedStatement( + "INSERT OR IGNORE INTO index_data (" + "index_id, object_data_id, object_data_key, value) " + "VALUES (:index_id, :object_data_id, :object_data_key, :value)"); + } NS_ENSURE_TRUE(stmt, NS_ERROR_FAILURE); - mozStorageStatementScoper scoper4(stmt); + mozStorageStatementScoper scoper(stmt); rv = stmt->BindInt64ByName(NS_LITERAL_CSTRING("index_id"), updateInfo.indexId);