Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/workers optimised #256

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

ivanovSPvirtru
Copy link
Contributor

No description provided.

Copy link

If these changes look good, signoff on them with:

git pull && git commit --amend --signoff && git push --force-with-lease origin

If they aren't any good, please remove them with:

git pull && git reset --hard HEAD~1 && git push --force-with-lease origin

@ivanovSPvirtru ivanovSPvirtru marked this pull request as ready for review November 20, 2023 15:52
@ivanovSPvirtru ivanovSPvirtru requested a review from a team as a code owner November 20, 2023 15:52
Signed-off-by: sivanov <[email protected]>
@ivanovSPvirtru ivanovSPvirtru force-pushed the feature/workers_unoptimised branch from f3c4784 to 223e9a0 Compare November 20, 2023 15:53
Copy link

sonarcloud bot commented Nov 21, 2023

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

67.0% 67.0% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@ivanovSPvirtru ivanovSPvirtru changed the title Feature/workers unoptimised Feature/workers optimised Nov 21, 2023
@@ -981,6 +982,10 @@ export async function sliceAndDecrypt({
cryptoService: CryptoService;
segmentIntegrityAlgorithm: IntegrityAlgorithm;
}) {
const limit = pLimit(navigator.hardwareConcurrency || 4); // save fallback number
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to make this customizable by the caller somehow? (See also: chunksInOneDownload)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think we do tbh. Ive benchmarked it tried multiple configurations. More plimit with hardwareConcurrency amount of workers, vice-verca, more both of them. What i got:

  1. number of Workers more than amount of cores really didnt bosted performance
  2. pLimit ether didnt had any effect ether were so big that gained small improvement but froze ui


const workerBlob = new Blob([workerScript], { type: 'application/javascript' });
const workerUrl = URL.createObjectURL(workerBlob);
const workersArray: Worker[] = Array.from({ length: maxWorkers }, () => new Worker(workerUrl));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we'd be doing this at build time, not runtime. Do we have any methods for doing this? I can't find a good way to do this without doing a two-pass build.

Copy link
Contributor Author

@ivanovSPvirtru ivanovSPvirtru Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code gets executate in the instance when we import entry point of sdk to any frontend app. Because this file eventually gets imported from entry point further down the nodes. And imports are synchronous. ANd code above too.

I dont think it could be initiated earlier that that in js world tbh

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the bundler will do all this work for you can you can treat this like a dynamic import almost. It looks like both vitejs and webpack 5 support this:

https://vitejs.dev/guide/features.html#web-workers

https://webpack.js.org/guides/web-workers/

I guess the question is are they smart enough to work through all the intermediate translations we do. Most notably, virtru-sdk uses webpack5 to bundle, and then secure-lib.js loads that (webpack 5 again) as does secure-share (webpack 5 as well), but our only test library here uses vite.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes i tried it and i had constant problems on a app side. Ive tuned it on Virtru-sdk side but after webpack on secure share it had constant errors. Spend a wile on that.

Thats why i decided to go with this aproach. Its makes sdk independent of bundlers issues and doesnt slow down performance, since its a singleton instance that initializes workers as soon as sdk imported it. So when youll start download stuff its already be there.

pop: () => Promise<Worker>;
}

const workersQueue: WorkersQueue = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some test coverage for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants