-
Notifications
You must be signed in to change notification settings - Fork 56
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
Add SharedArrayBuffer support #46
Conversation
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.
Overall this definitely makes sense, thanks for the PR! Left a few nits inline. Also, I'm wondering if this could be covered by some kind of tests, even if SharedArrayBuffer
is mocked?
index.js
Outdated
@@ -11,8 +11,8 @@ const VERSION = 3; // serialized format version | |||
export default class Flatbush { | |||
|
|||
static from(data) { | |||
if (!(data instanceof ArrayBuffer)) { | |||
throw new Error('Data must be an instance of ArrayBuffer.'); | |||
if (!(data instanceof ArrayBuffer) && SharedArrayBuffer && !(data instanceof SharedArrayBuffer)) { |
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.
Two issues here, if I'm following this right:
- In case
SharedArrayBuffer
isn't available, just referencing it will throw an error, so this won't work as a check. We need to usetypeof
. - If we fix the reference, the whole condition will still evaluate to
false
in case of a wrong data type, so the error won't be thrown.
So this would need to look something like this:
if (!(data instanceof ArrayBuffer) && SharedArrayBuffer && !(data instanceof SharedArrayBuffer)) { | |
if (!(data instanceof ArrayBuffer) && (typeof SharedArrayBuffer === 'undefined' || !(data instanceof SharedArrayBuffer))) { |
index.js
Outdated
if (numItems === undefined) throw new Error('Missing required argument: numItems.'); | ||
if (isNaN(numItems) || numItems <= 0) throw new Error(`Unpexpected numItems value: ${numItems}.`); | ||
if (isNaN(numItems) || numItems <= 0) throw new Error(`Unexpected numItems value: ${numItems}.`); | ||
if (useSharedArrayBuffer && !SharedArrayBuffer) throw new Error('SharedArrayBuffer not available in your runtime.'); |
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.
Same issue, typeof
I finally used |
@jdesboeufs |
I'm definitely a backend developer 🤣 I agree with you for Here is another implementation, using As Tell me what to you think! 😅 |
Co-authored-by: Volodymyr Agafonkin <[email protected]>
I agree with that! If it's OK feel free to squash. No need to bloat the commit history 🫣 |
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.
Nice! Thanks for the contribution.
Hi @mourner,
I hope you, your family and friends are still safe.
This PR adds support for SharedArrayBuffer which is a fully compatible alternative to
ArrayBuffer
.Firstly it allows a
SharedArrayBuffer
to be given toFlatbush.from()
. Up to now it throwsData must be an instance of ArrayBuffer
.Secondly it adds a new optional parameter to the constructor:
useSharedArrayWorker
.Before to go further, I need to know what do you think of this.
Why use a
SharedArrayBuffer
? You can share the data between multiple workers (Worker
,SharedWorker
,ServiceWorker
and lower memory footprint.In my use case, the R-tree is big! (~2.5GB).
Jerome