From cbc85407df37c256cbbf3d03bc0f715749c3b6a6 Mon Sep 17 00:00:00 2001 From: Mayhem Date: Thu, 13 Feb 2014 14:41:24 +0100 Subject: [PATCH] Optimize setting data operations. Setting local data on Chrome is not throttled, only debounce sync. Don't make DBs save data on every page load, don't save cleaning operations if nothing's changed. Debounce Unread.saveLastReadPost() on Chrome to every 5 seconds. --- css/style.css | 1 + lib/$.coffee | 6 +++--- src/General/DataBoard.coffee | 27 ++++++++++++++------------- src/Monitoring/Unread.coffee | 2 +- 4 files changed, 19 insertions(+), 17 deletions(-) diff --git a/css/style.css b/css/style.css index 2de6a7af2..11916e177 100644 --- a/css/style.css +++ b/css/style.css @@ -787,6 +787,7 @@ a.hide-announcement { min-width: 100%; overflow: hidden; white-space: nowrap; + position: relative; -webkit-user-select: none; -moz-user-select: none; user-select: none; diff --git a/lib/$.coffee b/lib/$.coffee index 801899dc6..939c1577e 100644 --- a/lib/$.coffee +++ b/lib/$.coffee @@ -288,8 +288,7 @@ do -> delete timeout[area] items[area] = {} - setAll = $.debounce $.SECOND, -> - setArea 'local' + setSync = $.debounce $.SECOND, -> setArea 'sync' $.set = (key, val) -> @@ -300,7 +299,8 @@ do -> for key in $.localKeys when key of items.sync items.local[key] = items.sync[key] delete items.sync[key] - setAll() + setArea 'local' + setSync() $.clear = (cb) -> items.local = {} diff --git a/src/General/DataBoard.coffee b/src/General/DataBoard.coffee index 2aab44b17..d82ff1b65 100644 --- a/src/General/DataBoard.coffee +++ b/src/General/DataBoard.coffee @@ -58,15 +58,14 @@ class DataBoard val or defaultValue clean: -> - for boardID, val of @data.boards - @deleteIfEmpty {boardID} - now = Date.now() - if (@data.lastChecked or 0) < now - 2 * $.HOUR - @data.lastChecked = now - for boardID of @data.boards - @ajaxClean boardID + return if (@data.lastChecked or 0) > now - 2 * $.HOUR + for boardID of @data.boards + @deleteIfEmpty {boardID} + @ajaxClean boardID if boardID of @data.boards + + @data.lastChecked = now @save() ajaxClean: (boardID) -> $.cache "//a.4cdn.org/#{boardID}/threads.json", (e) => @@ -76,12 +75,14 @@ class DataBoard board = @data.boards[boardID] threads = {} for page in e.target.response - for thread in page.threads - if thread.no of board - threads[thread.no] = board[thread.no] - @data.boards[boardID] = threads - @deleteIfEmpty {boardID} - @save() + for thread in page.threads when thread.no of board + threads[thread.no] = board[thread.no] + count = Object.keys(threads).length + return if count is Object.keys(board).length # Nothing changed. + if count + @set {boardID, val: threads} + else + @delete {boardID} onSync: (data) => @data = data or boards: {} diff --git a/src/Monitoring/Unread.coffee b/src/Monitoring/Unread.coffee index 8e13c463e..39fc9fce9 100644 --- a/src/Monitoring/Unread.coffee +++ b/src/Monitoring/Unread.coffee @@ -139,7 +139,7 @@ Unread = Unread.readArray Unread.postsQuotingYou Unread.update() if e - saveLastReadPost: -> + saveLastReadPost: <% if (type === 'crx') { %>$.debounce 5 * $.SECOND,<% } %> -> return if Unread.thread.isDead Unread.db.set boardID: Unread.thread.board.ID