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

Perform texture computation on web worker #107

Merged
merged 1 commit into from
Apr 30, 2020
Merged

Perform texture computation on web worker #107

merged 1 commit into from
Apr 30, 2020

Conversation

axelboc
Copy link
Contributor

@axelboc axelboc commented Apr 30, 2020

I used comlink, which simplifies writing and communicating with web workers, along with react-use-comlink.

I had to replace useDataScale with a utility function so I could recreate the data scale on the web worker (functions can't be sent to web workers).

The result is that the UI remains fluid while the texture is being recomputed.

ezgif-2-ff38971ec5c4

Next, I'll investigate whether there's a better way to send the values to the web worker. At the moment, the JS array is copied. I hear it's better to transfer a Uint8Array by reference... I don't think I'll bother with moving finding the domain to a web worker, as it seems to be pretty fast at the moment, and it only occurs once when selecting a dataset (contrary to the texture, which is recomputed when the config changes).

Comment on lines +71 to +76
await proxy.computeTextureData(
values,
customDomain || dataDomain,
hasLogScale,
colorMap
)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks to comlink, it's just like calling an async method!

Copy link
Member

Choose a reason for hiding this comment

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

Sweet !

Copy link
Member

@loichuder loichuder left a comment

Choose a reason for hiding this comment

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

Nice ! It would be nice to have some visual indicator that the texture is being computed (some graying or a spinner). For another PR ?

const scale = (isLog ? scaleSymlog : scaleLinear)();
scale.domain(domain);
return scale;
}
Copy link
Member

Choose a reason for hiding this comment

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

If I understand well, you had to make useDataScale a regular function rather than a hook to be able to call it from the worker that is not a hook ?

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 would have liked to pass the scale returned by useDataScale to the web worker, but as I mentioned, you can't pass functions to a web worker. https://github.com/GoogleChromeLabs/comlink/blob/master/structured-clone-table.md

Comment on lines +22 to +24
// eslint-disable-next-line
// @ts-ignore
import Worker from 'worker-loader!./worker'; // eslint-disable-line
Copy link
Member

Choose a reason for hiding this comment

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

eslint problems ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🙄 and create-react-app, which doesn't include worker-loader...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

... ad Jest, which doesn't really like webpack's inline loader syntax...

@axelboc axelboc merged commit b5526f8 into master Apr 30, 2020
@axelboc axelboc deleted the web-worker branch April 30, 2020 10:21
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.

None yet

2 participants