Skip to content

Commit

Permalink
Reduce redux saveState overhead (#283)
Browse files Browse the repository at this point in the history
* Reduce redux saveState overhead

I noticed my CPU was quite high when downloading big streams and most of
the CPU was being spent doing browser.storage.local.set(...) for saving
the state.  This state only appears to be needed when the background
page is reloaded (which will never happen if the a content-script is
active.

Buffering the save by 200ms reduces JS overhead by about ~40% (not
including native methods overhead) and total CPU usage on my system
(circa 2022 AMD 16 core) is reduced by ~28%.

* change: use throttle

---------

Co-authored-by: Shy <[email protected]>
  • Loading branch information
mayfield and puemos authored Jul 22, 2023
1 parent 876d2e5 commit 68ce610
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/extension/extension-background/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/extension/extension-background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"typescript": "4.9.5"
},
"dependencies": {
"@github/mini-throttle": "^2.1.0",
"@hls-downloader/core": "file:../../core",
"@types/uuid": "^7.0.2",
"@videojs/vhs-utils": "^1.3.0",
Expand Down
3 changes: 2 additions & 1 deletion src/extension/extension-background/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { CryptoDecryptor } from "./services/crypto-decryptor";
import { FetchLoader } from "./services/fetch-loader";
import { IndexedDBFS } from "./services/indexedb-fs";
import { M3u8Parser } from "./services/m3u8-parser";
import { throttle } from "@github/mini-throttle";

(async () => {
const state = await getState();
Expand All @@ -22,7 +23,7 @@ import { M3u8Parser } from "./services/m3u8-parser";
wrapStore(store);

store.subscribe(() => {
saveState(store.getState());
throttle(saveState, 100);
});

subscribeListeners(store);
Expand Down
3 changes: 2 additions & 1 deletion src/extension/extension-background/src/persistState.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { browser } from "webextension-polyfill-ts";
import { RootState } from "@hls-downloader/core/lib/adapters/redux/root-reducer";

export async function saveState(state: RootState) {
export async function saveState() {
const state = getState();
if (!state) {
return;
}
Expand Down

0 comments on commit 68ce610

Please sign in to comment.