Skip to content

Commit

Permalink
Compress session data #3388
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Oct 4, 2023
1 parent 0a85f93 commit 42c44ee
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 10 deletions.
12 changes: 8 additions & 4 deletions webextensions/background/background-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
dumpTab,
wait,
mapAndFilter,
compress,
decompress,
configs
} from '/common/common.js';
import * as ApiTabs from '/common/api-tabs.js';
Expand Down Expand Up @@ -297,10 +299,11 @@ async function updateWindowCache(owner, key, value) {
}
else {
try {
return browser.sessions.setWindowValue(owner.windowId, key, value).catch(ApiTabs.createErrorSuppressor());
const valueToSave = await compress(value);
return browser.sessions.setWindowValue(owner.windowId, key, valueToSave).catch(ApiTabs.createErrorSuppressor());
}
catch(e) {
console.log(new Error('fatal error: failed to update window cache'), e, owner, key, value);
catch(error) {
console.log(new Error('fatal error: failed to update window cache'), error, owner, key, value);
}
}
}
Expand All @@ -318,7 +321,8 @@ export function markWindowCacheDirtyFromTab(tab, akey) {
}

async function getWindowCache(owner, key) {
return browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
const value = await browser.sessions.getWindowValue(owner.windowId, key).catch(ApiTabs.createErrorHandler());
return decompress(value);
}

function getWindowCacheOwner(windowId) {
Expand Down
51 changes: 51 additions & 0 deletions webextensions/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,57 @@ export function getWindowParamsFromSource(sourceWindow, { left, top, width, heig
return params;
}

export async function compress(input) {
return input;
try {
const start = Date.now();
const streamToCompress = new Blob([JSON.stringify(input)]).stream();
const format = 'deflate-raw';
// eslint-disable-next-line no-undef
const compressedStream = streamToCompress.pipeThrough(new CompressionStream(format));
const response = new Response(compressedStream);
const compressedArrayBuffer = await response.arrayBuffer();
const compressedBytes = new Uint8Array(compressedArrayBuffer);
let compressedString = '';
for (let i = 0, maxi = compressedBytes.byteLength; i < maxi; i++) {
compressedString += String.fromCharCode(compressedBytes[i]);
}
console.log('compressed: ', Date.now() - start);
return `compressed(deflate-raw):${btoa(compressedString)}`;
}
catch(error) {
console.log('could not compress data: ', error);
}
return input;
}

export async function decompress(input) {
const matched = typeof input == 'string' && input.match(/^compressed\(([^\)]+)\):/);
if (!matched)
return input;

try {
const start = Date.now();
const format = matched[1];
const valueToDecompress = atob(input.replace(/^compressed\([^\)]+\):/, ''));
const compressedBytes = new Uint8Array(valueToDecompress.length);
for (let i = 0, maxi = valueToDecompress.length; i < maxi; i++) {
compressedBytes[i] = valueToDecompress.charCodeAt(i);
}
const compressedStream = new Blob([compressedBytes]).stream();
// eslint-disable-next-line no-undef
const decompressedStream = compressedStream.pipeThrough(new DecompressionStream(format));
const response = new Response(decompressedStream);
const decompressedValue = await response.text();
console.log('decompressed: ', Date.now() - start);
return JSON.parse(decompressedValue);
}
catch(error) {
console.log('could not decompress data: ', error);
}
return null;
}

export function isNewTabCommandURL(url) {
const newTabUrls = new Set(configs.guessNewOrphanTabAsOpenedByNewTabCommandUrl.split('|'));
return newTabUrls.has(url);
Expand Down
14 changes: 8 additions & 6 deletions webextensions/sidebar/sidebar-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import {
countMatched,
toLines,
configs,
shouldApplyAnimation
shouldApplyAnimation,
compress,
decompress,
} from '/common/common.js';
import * as ApiTabs from '/common/api-tabs.js';
import * as Constants from '/common/constants.js';
Expand Down Expand Up @@ -79,10 +81,10 @@ export async function tryPreload(tab = null) {

async function preload(tab) {
const cache = await Promise.all([
browser.sessions.getWindowValue(tab.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR),
browser.sessions.getWindowValue(tab.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR).then(decompress),
browser.sessions.getWindowValue(tab.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY),
browser.sessions.getWindowValue(tab.windowId, Constants.kWINDOW_STATE_CACHED_SIDEBAR_COLLAPSED_DIRTY),
browser.sessions.getTabValue(tab.id, Constants.kWINDOW_STATE_CACHED_SIDEBAR),
browser.sessions.getTabValue(tab.id, Constants.kWINDOW_STATE_CACHED_SIDEBAR).then(decompress),
browser.sessions.getTabValue(tab.id, Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY),
browser.sessions.getTabValue(tab.id, Constants.kWINDOW_STATE_CACHED_SIDEBAR_COLLAPSED_DIRTY)
]).catch(ApiTabs.createErrorSuppressor());
Expand Down Expand Up @@ -110,7 +112,7 @@ export async function getEffectiveWindowCache(options = {}) {
let tabsDirty, collapsedDirty;
const preloadedCache = mPreloadedCaches.get(`window${mLastWindowCacheOwner.windowId}`);
[cache, tabsDirty, collapsedDirty] = preloadedCache || await MetricsData.addAsync('getEffectiveWindowCache: reading window cache', Promise.all([
getWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR),
getWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR).then(decompress),
getWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR_TABS_DIRTY),
getWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR_COLLAPSED_DIRTY)
]));
Expand Down Expand Up @@ -511,7 +513,7 @@ async function updateCachedTabbar() {
const signature = getWindowSignature(Tab.getAllTabs(mTargetWindow));
log('updateCachedTabbar ', { stack: configs.debug && new Error().stack });
mLastWindowCacheOwner = getWindowCacheOwner(mTargetWindow);
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR, {
updateWindowCache(Constants.kWINDOW_STATE_CACHED_SIDEBAR, await compress({
version: kCONTENTS_VERSION,
tabbar: {
contents: SidebarTabs.wholeContainer.innerHTML,
Expand All @@ -520,7 +522,7 @@ async function updateCachedTabbar() {
},
indent: Indent.getCacheInfo(),
signature
});
}));
}


Expand Down

0 comments on commit 42c44ee

Please sign in to comment.