-
Notifications
You must be signed in to change notification settings - Fork 18
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
Limit Workers to max. avail. CPUs #12
base: master
Are you sure you want to change the base?
Conversation
lib/ImageWorker.js
Outdated
this.image = null; | ||
|
||
if (!workerPoolCreated) { | ||
createWorkerPool(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One liner ifs can also be made an awesome one
!workerPoolCreated && createWorkerPool();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I forgot a line, that's why I couldn't do the shortcut :)
lib/ImageWorker.js
Outdated
} | ||
|
||
/** Marks worker `index` as available. */ | ||
function setFree(index: int) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change int => number
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, I'm new to flow!
|
||
state = { | ||
isLoading: true, | ||
imgSrc: '' | ||
} | ||
constructor(props: ImageWorkerProps) { | ||
super(props); | ||
this.worker.onmessage = (event: Object) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What was the purpose of removing the listener from the constructor and moving it to CDM?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved everything to the constructor back again. My original idea was that it should only load the image when truly needed, but the constructor is called before CDM.
Each IMageWorker component will now spawn 4 worker threads. As we've removed the |
can you run |
const blobURL = URL.createObjectURL( | ||
new Blob([webWorkerScript], { type: 'application/javascript' }) | ||
); | ||
for (let i = 0; i < window.navigator.hardwareConcurrency || 4; ++i) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will go into an infinite loop
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why so?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when you say
i < window.navigator.hardwareConcurrency || 4
The browser starts evaluating from the left and this will be treated as
(i < window.navigator.hardwareConcurrency) || (4)
even if the value of i
goes to say 200, the above will then become
200 < window.navigator.hardwareConcurrency || 4
Which will translate to false || 4
and will always be true
class ImageWorker extends Component<ImageWorkerProps, ImageWorkerState> { | ||
image: HTMLImageElement; | ||
worker = new Worker(URL.createObjectURL( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why remove this, where is the worker actually being spawned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved to line 42
lib/ImageWorker.js
Outdated
@@ -3,15 +3,10 @@ | |||
import React, { Component } from 'react'; | |||
|
|||
const webWorkerScript = ` | |||
const handleResponse = () => response.blob(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
response is not defined here. When this fn is executed it will break you can change it to
response => response.blob()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@nitish24p I fixed the mistakes, sorry for those, I was in a hurry. |
lib/ImageWorker.js
Outdated
@@ -34,33 +29,71 @@ const wrappedComponent = WrappedComponent => props => { | |||
return <WrappedComponent {...props} />; | |||
}; | |||
|
|||
/** Have we initiated the worker pool already? */ | |||
const workerPoolCreated = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you will have to change this to a let
and reassign it to a true, after the createWorkerPool
fn is invoked. Else everytime there will be 4 workers created as workerPoolCreated
will always be false
lib/ImageWorker.js
Outdated
this.loadImage(event.data); | ||
setFree(workerObj.index); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be workerObj.i
as per your code
There's a big flaw in my current implementation: Edit: I've overlooked, that Workers have the |
Fixes #10