Changeset 278786 in webkit
- Timestamp:
- Jun 11, 2021, 3:56:18 PM (5 years ago)
- Location:
- trunk/Source/WebKit
- Files:
-
- 6 edited
-
ChangeLog (modified) (1 diff)
-
NetworkProcess/WebStorage/LocalStorageDatabase.cpp (modified) (7 diffs)
-
NetworkProcess/WebStorage/LocalStorageDatabase.h (modified) (4 diffs)
-
NetworkProcess/WebStorage/StorageArea.cpp (modified) (2 diffs)
-
NetworkProcess/WebStorage/StorageArea.h (modified) (1 diff)
-
NetworkProcess/WebStorage/StorageManagerSet.cpp (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WebKit/ChangeLog
r278783 r278786 1 2021-06-11 Chris Dumez <cdumez@apple.com> 2 3 [WK2] Batch local storage database writes using transactions 4 https://bugs.webkit.org/show_bug.cgi?id=226938 5 6 Reviewed by Geoff Garen. 7 8 Batch local storage writes using transactions, to improve performance 9 and reduce disk writes. In this patch, we use a simple time-based 10 approach where we batch transactions happening in the same 500ms 11 period. 12 13 * NetworkProcess/WebStorage/LocalStorageDatabase.cpp: 14 (WebKit::LocalStorageDatabase::create): 15 (WebKit::LocalStorageDatabase::LocalStorageDatabase): 16 (WebKit::LocalStorageDatabase::startTransactionIfNecessary): 17 (WebKit::LocalStorageDatabase::removeItem): 18 (WebKit::LocalStorageDatabase::setItem): 19 (WebKit::LocalStorageDatabase::clear): 20 (WebKit::LocalStorageDatabase::close): 21 * NetworkProcess/WebStorage/LocalStorageDatabase.h: 22 * NetworkProcess/WebStorage/StorageArea.cpp: 23 (WebKit::StorageArea::ensureDatabase const): 24 1 25 2021-06-11 Alex Christensen <achristensen@webkit.org> 2 26 -
trunk/Source/WebKit/NetworkProcess/WebStorage/LocalStorageDatabase.cpp
r278778 r278786 41 41 constexpr auto getItemsQueryString { "SELECT key, value FROM ItemTable"_s }; 42 42 constexpr unsigned maximumSizeForValuesKeptInMemory { 1024 }; // 1 KB 43 44 Ref<LocalStorageDatabase> LocalStorageDatabase::create(String&& databasePath, unsigned quotaInBytes) 45 { 46 return adoptRef(*new LocalStorageDatabase(WTFMove(databasePath), quotaInBytes)); 47 } 48 49 LocalStorageDatabase::LocalStorageDatabase(String&& databasePath, unsigned quotaInBytes) 50 : m_databasePath(WTFMove(databasePath)) 43 constexpr Seconds transactionDuration { 500_ms }; 44 45 Ref<LocalStorageDatabase> LocalStorageDatabase::create(Ref<WorkQueue>&& workQueue, String&& databasePath, unsigned quotaInBytes) 46 { 47 return adoptRef(*new LocalStorageDatabase(WTFMove(workQueue), WTFMove(databasePath), quotaInBytes)); 48 } 49 50 LocalStorageDatabase::LocalStorageDatabase(Ref<WorkQueue>&& workQueue, String&& databasePath, unsigned quotaInBytes) 51 : m_workQueue(WTFMove(workQueue)) 52 , m_databasePath(WTFMove(databasePath)) 51 53 , m_quotaInBytes(quotaInBytes) 52 54 { … … 99 101 100 102 return true; 103 } 104 105 void LocalStorageDatabase::startTransactionIfNecessary() 106 { 107 if (!m_transaction) 108 m_transaction = makeUnique<SQLiteTransaction>(m_database); 109 110 if (m_transaction->inProgress()) 111 return; 112 113 m_transaction->begin(); 114 m_workQueue->dispatchAfter(transactionDuration, [weakThis = makeWeakPtr(*this)] { 115 if (weakThis) 116 weakThis->m_transaction->commit(); 117 }); 101 118 } 102 119 … … 185 202 return; 186 203 204 startTransactionIfNecessary(); 187 205 oldValue = item(key); 188 206 if (oldValue.isNull()) … … 249 267 return; 250 268 269 startTransactionIfNecessary(); 251 270 oldValue = item(key); 252 271 … … 281 300 return false; 282 301 302 startTransactionIfNecessary(); 283 303 auto clearStatement = scopedStatement(m_clearStatement, "DELETE FROM ItemTable"_s); 284 304 if (!clearStatement) { … … 299 319 300 320 return m_database.lastChanges() > 0; 321 } 322 323 void LocalStorageDatabase::flushToDisk() 324 { 325 if (m_transaction) 326 m_transaction->commit(); 301 327 } 302 328 … … 316 342 m_deleteItemStatement = nullptr; 317 343 m_items = std::nullopt; 344 if (auto transaction = std::exchange(m_transaction, nullptr)) 345 transaction->commit(); 318 346 319 347 if (m_database.isOpen()) -
trunk/Source/WebKit/NetworkProcess/WebStorage/LocalStorageDatabase.h
r278778 r278786 28 28 #include <WebCore/SQLiteDatabase.h> 29 29 #include <wtf/HashMap.h> 30 #include <wtf/WeakPtr.h> 31 #include <wtf/WorkQueue.h> 30 32 31 33 namespace WebCore { 32 34 class SQLiteStatementAutoResetScope; 35 class SQLiteTransaction; 33 36 34 37 struct SecurityOriginData; … … 37 40 namespace WebKit { 38 41 39 class LocalStorageDatabase : public RefCounted<LocalStorageDatabase> {42 class LocalStorageDatabase : public RefCounted<LocalStorageDatabase>, public CanMakeWeakPtr<LocalStorageDatabase> { 40 43 public: 41 static Ref<LocalStorageDatabase> create( String&& databasePath, unsigned quotaInBytes);44 static Ref<LocalStorageDatabase> create(Ref<WorkQueue>&&, String&& databasePath, unsigned quotaInBytes); 42 45 ~LocalStorageDatabase(); 43 46 … … 52 55 void close(); 53 56 57 void flushToDisk(); 54 58 void handleLowMemoryWarning(); 55 59 56 60 private: 57 LocalStorageDatabase( String&& databasePath, unsigned quotaInBytes);61 LocalStorageDatabase(Ref<WorkQueue>&&, String&& databasePath, unsigned quotaInBytes); 58 62 59 63 enum class ShouldCreateDatabase : bool { No, Yes }; 60 64 bool openDatabase(ShouldCreateDatabase); 61 65 66 void startTransactionIfNecessary(); 62 67 bool migrateItemTableIfNeeded(); 63 68 bool databaseIsEmpty() const; … … 67 72 WebCore::SQLiteStatementAutoResetScope scopedStatement(std::unique_ptr<WebCore::SQLiteStatement>&, ASCIILiteral query) const; 68 73 74 Ref<WorkQueue> m_workQueue; 69 75 String m_databasePath; 70 76 mutable WebCore::SQLiteDatabase m_database; 77 std::unique_ptr<WebCore::SQLiteTransaction> m_transaction; 71 78 const unsigned m_quotaInBytes { 0 }; 72 79 bool m_isClosed { false }; -
trunk/Source/WebKit/NetworkProcess/WebStorage/StorageArea.cpp
r278409 r278786 176 176 if (!m_localStorageDatabase) { 177 177 auto* localStorageDatabaseTracker = m_localStorageNamespace->storageManager()->localStorageDatabaseTracker(); 178 m_localStorageDatabase = LocalStorageDatabase::create( localStorageDatabaseTracker->databasePath(m_securityOrigin), m_quotaInBytes);178 m_localStorageDatabase = LocalStorageDatabase::create(m_queue.copyRef(), localStorageDatabaseTracker->databasePath(m_securityOrigin), m_quotaInBytes); 179 179 m_localStorageDatabase->openIfExisting(); 180 180 } … … 200 200 } 201 201 202 void StorageArea::syncToDatabase() 203 { 204 if (m_localStorageDatabase) 205 m_localStorageDatabase->flushToDisk(); 206 } 207 202 208 void StorageArea::handleLowMemoryWarning() 203 209 { -
trunk/Source/WebKit/NetworkProcess/WebStorage/StorageArea.h
r276659 r278786 72 72 void close(); 73 73 74 void syncToDatabase(); 74 75 void handleLowMemoryWarning(); 75 76 -
trunk/Source/WebKit/NetworkProcess/WebStorage/StorageManagerSet.cpp
r278185 r278786 156 156 ASSERT(RunLoop::isMain()); 157 157 158 m_queue->dispatchSync([] { }); 158 BinarySemaphore semaphore; 159 m_queue->dispatch([this, &semaphore] { 160 for (const auto& storageArea : m_storageAreas.values()) { 161 ASSERT(storageArea); 162 if (storageArea) 163 storageArea->syncToDatabase(); 164 } 165 semaphore.signal(); 166 }); 167 semaphore.wait(); 159 168 } 160 169
Note:
See TracChangeset
for help on using the changeset viewer.