From 7f3273dfe707b6bd837f2c44b7839533f4aec40c Mon Sep 17 00:00:00 2001 From: wingman-jr-addon Date: Thu, 23 Dec 2021 09:03:44 -0600 Subject: [PATCH 1/2] Trying out a limiter on GIF frame parsing to improve responsiveness on pages with lots of GIFs --- background_gif.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/background_gif.js b/background_gif.js index 5ddadd4..161ef9a 100644 --- a/background_gif.js +++ b/background_gif.js @@ -125,12 +125,13 @@ function gifTryGetExtLength(parsedGif, si) { return (i + subBlockSize) - si; } -function parseGifFrames(nextBuffer, parsedGif) { +function parseGifFrames(nextBuffer, parsedGif, maxFramesToParse) { if(!parsedGif) { parsedGif = { header: null, //actually header plus global color table screenWidth: -1, screenHeight: -1, + lidCount: 0, unscannedFrames: [], newParsedData: new Uint8Array(), parsedIndex: 0, //index of last fully parsed image @@ -184,8 +185,9 @@ function parseGifFrames(nextBuffer, parsedGif) { let wasParseFailure = false; let isComplete = false; + let framesParsedThisTime = 0; - while(!wasParseFailure && !isComplete && i < b.byteLength) { + while(!wasParseFailure && !isComplete && i < b.byteLength && (maxFramesToParse == -1 || framesParsedThisTime < maxFramesToParse)) { let peekByte = b[i]; i+=1; switch(peekByte) { // Local Image Descriptor + Data Blocks @@ -208,6 +210,8 @@ function parseGifFrames(nextBuffer, parsedGif) { i += lidLength; parsedGif.parsedIndex = i; WJR_DEBUG && console.debug(`DEFG: Parsed LID - results now ${parsedGif.unscannedFrames.length} and parsed index ${parsedGif.parsedIndex}`); + framesParsedThisTime++; + parsedGif.lidCount++; } else { WJR_DEBUG && console.debug(`DEFG: Could not fully parse LID - results now ${parsedGif.unscannedFrames.length}`); wasParseFailure = true; @@ -227,6 +231,7 @@ function parseGifFrames(nextBuffer, parsedGif) { break; case PEEK_TRAILER: WJR_DEBUG && console.debug('DEFG: Found Trailer!'); + console.warn(`DEFG: GIF had a total of ${parsedGif.lidCount} frames`); parsedGif.parsedIndex = i; isComplete = true; break; @@ -238,6 +243,10 @@ function parseGifFrames(nextBuffer, parsedGif) { } } + if(framesParsedThisTime >= maxFramesToParse && maxFramesToParse != -1) { + console.warn(`DEFG: GIF parsing capped at ${framesParsedThisTime}`); + } + parsedGif.newParsedData = parsedGif.data.slice(parsedGif.lastParsedIndex, parsedGif.parsedIndex); } catch(e) { console.error('DEFG: Error parsing gif '+e); @@ -326,7 +335,7 @@ async function gifListener(details) { { //Ensure this top section remains synchronous WJR_DEBUG && console.debug(`DEFG: Data for ${details.requestId} of size ${newData.byteLength}`); - parsedGif = parseGifFrames(newData, parsedGif); + parsedGif = parseGifFrames(newData, parsedGif, isComplete ? -1 : 5); let capturedParsedIndex = parsedGif.parsedIndex; let capturedLastParsedIndex = parsedGif.lastParsedIndex; let unscannedFrames = parsedGif.unscannedFrames; From 71a97e9bc5335ce7035f927a95973f1b8d06187a Mon Sep 17 00:00:00 2001 From: wingman-jr-addon Date: Sat, 25 Dec 2021 20:46:05 -0600 Subject: [PATCH 2/2] Tweaking logging a bit --- background_gif.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/background_gif.js b/background_gif.js index 161ef9a..4d2b036 100644 --- a/background_gif.js +++ b/background_gif.js @@ -230,8 +230,7 @@ function parseGifFrames(nextBuffer, parsedGif, maxFramesToParse) { } break; case PEEK_TRAILER: - WJR_DEBUG && console.debug('DEFG: Found Trailer!'); - console.warn(`DEFG: GIF had a total of ${parsedGif.lidCount} frames`); + WJR_DEBUG && console.info(`DEFG: Found Trailer - GIF had a total of ${parsedGif.lidCount} frames`); parsedGif.parsedIndex = i; isComplete = true; break; @@ -244,7 +243,7 @@ function parseGifFrames(nextBuffer, parsedGif, maxFramesToParse) { } if(framesParsedThisTime >= maxFramesToParse && maxFramesToParse != -1) { - console.warn(`DEFG: GIF parsing capped at ${framesParsedThisTime}`); + WJR_DEBUG && console.warn(`DEFG: GIF parsing capped at ${framesParsedThisTime}`); } parsedGif.newParsedData = parsedGif.data.slice(parsedGif.lastParsedIndex, parsedGif.parsedIndex);