Modified: trunk/Source/WebCore/ChangeLog (249582 => 249583)
--- trunk/Source/WebCore/ChangeLog 2019-09-06 18:10:49 UTC (rev 249582)
+++ trunk/Source/WebCore/ChangeLog 2019-09-06 18:28:31 UTC (rev 249583)
@@ -1,3 +1,23 @@
+2019-09-06 Sihui Liu <[email protected]>
+
+ IndexedDB: use SQL COUNT statement for count operation
+ https://bugs.webkit.org/show_bug.cgi?id=201465
+
+ Reviewed by Geoffrey Garen.
+
+ We did count operation by moving objectStore/index iterator from begin to end and counting the steps. We can
+ utilize the SQL COUNT statement, and improve the performance further by caching the statement.
+
+ Tested on release build minibrowser. Without the change, the mean time to complete one iteration in
+ PerformanceTests/IndexedDB/basic/index-count.html and PerformanceTests/IndexedDB/basic/objectStore-count.html is
+ about 50ms. With the change it is about 2ms.
+
+ * Modules/indexeddb/server/SQLiteIDBBackingStore.cpp:
+ (WebCore::IDBServer::SQLiteIDBBackingStore::getCount):
+ (WebCore::IDBServer::SQLiteIDBBackingStore::cachedStatement):
+ (WebCore::IDBServer::SQLiteIDBBackingStore::closeSQLiteDB):
+ * Modules/indexeddb/server/SQLiteIDBBackingStore.h:
+
2019-09-06 Alex Christensen <[email protected]>
When disabling legacy private browsing for testing, change the SessionID back to what it was, not the defaultSessionID
Modified: trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp (249582 => 249583)
--- trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp 2019-09-06 18:10:49 UTC (rev 249582)
+++ trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.cpp 2019-09-06 18:28:31 UTC (rev 249583)
@@ -2304,21 +2304,78 @@
ASSERT(m_sqliteDB);
ASSERT(m_sqliteDB->isOpen());
- outCount = 0;
-
auto* transaction = m_transactions.get(transactionIdentifier);
if (!transaction || !transaction->inProgress())
return IDBError { UnknownError, "Attempt to get count from database without an in-progress transaction"_s };
- auto cursor = transaction->maybeOpenBackingStoreCursor(objectStoreIdentifier, indexIdentifier, range);
- if (!cursor) {
- LOG_ERROR("Cannot open cursor to populate indexes in database");
- return IDBError { UnknownError, "Unable to populate indexes in database"_s };
+ outCount = 0;
+
+ auto lowerKey = range.lowerKey.isNull() ? IDBKeyData::minimum() : range.lowerKey;
+ RefPtr<SharedBuffer> lowerBuffer = serializeIDBKeyData(lowerKey);
+ if (!lowerBuffer) {
+ LOG_ERROR("Unable to serialize lower IDBKey in lookup range");
+ return IDBError { UnknownError, "Unable to serialize lower IDBKey in lookup range for count operation"_s };
}
- while (cursor->advance(1))
- ++outCount;
+ auto upperKey = range.upperKey.isNull() ? IDBKeyData::maximum() : range.upperKey;
+ RefPtr<SharedBuffer> upperBuffer = serializeIDBKeyData(upperKey);
+ if (!upperBuffer) {
+ LOG_ERROR("Unable to serialize upper IDBKey in lookup range");
+ return IDBError { UnknownError, "Unable to serialize upper IDBKey in lookup range for count operation"_s };
+ }
+ SQLiteStatement* statement = nullptr;
+
+ if (!indexIdentifier) {
+ static const char* const countLowerOpenUpperOpenRecords = "SELECT COUNT(*) FROM Records WHERE objectStoreID = ? AND key > CAST(? AS TEXT) AND key < CAST(? AS TEXT);";
+ static const char* const countLowerOpenUpperClosedRecords = "SELECT COUNT(*) FROM Records WHERE objectStoreID = ? AND key > CAST(? AS TEXT) AND key <= CAST(? AS TEXT);";
+ static const char* const countLowerClosedUpperOpenRecords = "SELECT COUNT(*) FROM Records WHERE objectStoreID = ? AND key >= CAST(? AS TEXT) AND key < CAST(? AS TEXT);";
+ static const char* const countLowerClosedUpperClosedRecords = "SELECT COUNT(*) FROM Records WHERE objectStoreID = ? AND key >= CAST(? AS TEXT) AND key <= CAST(? AS TEXT);";
+
+ if (range.lowerOpen && range.upperOpen)
+ statement = cachedStatement(SQL::CountRecordsLowerOpenUpperOpen, countLowerOpenUpperOpenRecords);
+ else if (range.lowerOpen && !range.upperOpen)
+ statement = cachedStatement(SQL::CountRecordsLowerOpenUpperClosed, countLowerOpenUpperClosedRecords);
+ else if (!range.lowerOpen && range.upperOpen)
+ statement = cachedStatement(SQL::CountRecordsLowerClosedUpperOpen, countLowerClosedUpperOpenRecords);
+ else
+ statement = cachedStatement(SQL::CountRecordsLowerClosedUpperClosed, countLowerClosedUpperClosedRecords);
+
+ if (!statement
+ || statement->bindInt64(1, objectStoreIdentifier) != SQLITE_OK
+ || statement->bindBlob(2, lowerBuffer->data(), lowerBuffer->size()) != SQLITE_OK
+ || statement->bindBlob(3, upperBuffer->data(), upperBuffer->size()) != SQLITE_OK) {
+ LOG_ERROR("Could not count records in object store %" PRIi64 " from Records table (%i) - %s", objectStoreIdentifier, m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
+ return IDBError { UnknownError, "Unable to count records in object store due to binding failure"_s };
+ }
+ } else {
+ static const char* const countLowerOpenUpperOpenIndexRecords = "SELECT COUNT(*) FROM IndexRecords WHERE indexID = ? AND key > CAST(? AS TEXT) AND key < CAST(? AS TEXT);";
+ static const char* const countLowerOpenUpperClosedIndexRecords = "SELECT COUNT(*) FROM IndexRecords WHERE indexID = ? AND key > CAST(? AS TEXT) AND key <= CAST(? AS TEXT);";
+ static const char* const countLowerClosedUpperOpenIndexRecords = "SELECT COUNT(*) FROM IndexRecords WHERE indexID = ? AND key >= CAST(? AS TEXT) AND key < CAST(? AS TEXT);";
+ static const char* const countLowerClosedUpperClosedIndexRecords = "SELECT COUNT(*) FROM IndexRecords WHERE indexID = ? AND key >= CAST(? AS TEXT) AND key <= CAST(? AS TEXT);";
+
+ if (range.lowerOpen && range.upperOpen)
+ statement = cachedStatement(SQL::CountIndexRecordsLowerOpenUpperOpen, countLowerOpenUpperOpenIndexRecords);
+ else if (range.lowerOpen && !range.upperOpen)
+ statement = cachedStatement(SQL::CountIndexRecordsLowerOpenUpperClosed, countLowerOpenUpperClosedIndexRecords);
+ else if (!range.lowerOpen && range.upperOpen)
+ statement = cachedStatement(SQL::CountIndexRecordsLowerClosedUpperOpen, countLowerClosedUpperOpenIndexRecords);
+ else
+ statement = cachedStatement(SQL::CountIndexRecordsLowerClosedUpperClosed, countLowerClosedUpperClosedIndexRecords);
+
+ if (!statement
+ || statement->bindInt64(1, indexIdentifier) != SQLITE_OK
+ || statement->bindBlob(2, lowerBuffer->data(), lowerBuffer->size()) != SQLITE_OK
+ || statement->bindBlob(3, upperBuffer->data(), upperBuffer->size()) != SQLITE_OK) {
+ LOG_ERROR("Could not count records with index %" PRIi64 " from IndexRecords table (%i) - %s", indexIdentifier, m_sqliteDB->lastError(), m_sqliteDB->lastErrorMsg());
+ return IDBError { UnknownError, "Unable to count records for index due to binding failure"_s };
+ }
+ }
+
+ if (statement->step() != SQLITE_ROW)
+ return IDBError { UnknownError, "Unable to count records"_s };
+
+ outCount = statement->getColumnInt(0);
return IDBError { };
}
@@ -2571,7 +2628,7 @@
SQLiteStatement* SQLiteIDBBackingStore::cachedStatement(SQLiteIDBBackingStore::SQL sql, const char* statement)
{
- if (sql >= SQL::Count) {
+ if (sql >= SQL::Invalid) {
LOG_ERROR("Invalid SQL statement ID passed to cachedStatement()");
return nullptr;
}
@@ -2598,7 +2655,7 @@
void SQLiteIDBBackingStore::closeSQLiteDB()
{
- for (size_t i = 0; i < static_cast<int>(SQL::Count); ++i)
+ for (size_t i = 0; i < static_cast<int>(SQL::Invalid); ++i)
m_cachedStatements[i] = nullptr;
if (m_sqliteDB)
Modified: trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h (249582 => 249583)
--- trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h 2019-09-06 18:10:49 UTC (rev 249582)
+++ trunk/Source/WebCore/Modules/indexeddb/server/SQLiteIDBBackingStore.h 2019-09-06 18:28:31 UTC (rev 249583)
@@ -182,13 +182,21 @@
GetKeyRecordsLowerOpenUpperClosed,
GetKeyRecordsLowerClosedUpperOpen,
GetKeyRecordsLowerClosedUpperClosed,
- Count
+ CountRecordsLowerOpenUpperOpen,
+ CountRecordsLowerOpenUpperClosed,
+ CountRecordsLowerClosedUpperOpen,
+ CountRecordsLowerClosedUpperClosed,
+ CountIndexRecordsLowerOpenUpperOpen,
+ CountIndexRecordsLowerOpenUpperClosed,
+ CountIndexRecordsLowerClosedUpperOpen,
+ CountIndexRecordsLowerClosedUpperClosed,
+ Invalid,
};
SQLiteStatement* cachedStatement(SQL, const char*);
SQLiteStatement* cachedStatementForGetAllObjectStoreRecords(const IDBGetAllRecordsData&);
- std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Count)];
+ std::unique_ptr<SQLiteStatement> m_cachedStatements[static_cast<int>(SQL::Invalid)];
PAL::SessionID m_sessionID;
IDBDatabaseIdentifier m_identifier;