-
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0f2d230
add SharedArrayBuffer support to Flatbush.from
jdesboeufs fbee264
add an option to opt-in SharedArrayBuffer creation
jdesboeufs b06fdb3
update README
jdesboeufs b2f58b4
use global.SharedArrayBuffer to avoid exception and allow mocking
jdesboeufs eae7302
ensure ArrayBufferType reflects data internal type
jdesboeufs c788087
add unit tests for SharedArrayBuffer support
jdesboeufs 6c2b887
improve SharedArrayBuffer implementation
jdesboeufs 8c415c4
update README
jdesboeufs 64dac98
remaining reference to useSharedArrayBuffer
jdesboeufs 2522556
use duck-typing to check data is an ArrayBuffer-like
jdesboeufs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
|
||
/* global SharedArrayBuffer */ | ||
import FlatQueue from 'flatqueue'; | ||
|
||
const ARRAY_TYPES = [ | ||
|
@@ -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)) { | ||
throw new Error('Data must be an instance of ArrayBuffer or SharedArrayBuffer.'); | ||
} | ||
const [magic, versionAndType] = new Uint8Array(data, 0, 2); | ||
if (magic !== 0xfb) { | ||
|
@@ -24,12 +24,13 @@ export default class Flatbush { | |
const [nodeSize] = new Uint16Array(data, 2, 1); | ||
const [numItems] = new Uint32Array(data, 4, 1); | ||
|
||
return new Flatbush(numItems, nodeSize, ARRAY_TYPES[versionAndType & 0x0f], data); | ||
return new Flatbush(numItems, nodeSize, ARRAY_TYPES[versionAndType & 0x0f], undefined, data); | ||
} | ||
|
||
constructor(numItems, nodeSize = 16, ArrayType = Float64Array, data) { | ||
constructor(numItems, nodeSize = 16, ArrayType = Float64Array, useSharedArrayBuffer = false, data) { | ||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Same issue, |
||
|
||
this.numItems = +numItems; | ||
this.nodeSize = Math.min(Math.max(+nodeSize, 2), 65535); | ||
|
@@ -46,6 +47,7 @@ export default class Flatbush { | |
} while (n !== 1); | ||
|
||
this.ArrayType = ArrayType || Float64Array; | ||
this.ArrayBufferType = useSharedArrayBuffer ? SharedArrayBuffer : ArrayBuffer; | ||
this.IndexArrayType = numNodes < 16384 ? Uint16Array : Uint32Array; | ||
|
||
const arrayTypeIndex = ARRAY_TYPES.indexOf(this.ArrayType); | ||
|
@@ -55,7 +57,7 @@ export default class Flatbush { | |
throw new Error(`Unexpected typed array class: ${ArrayType}.`); | ||
} | ||
|
||
if (data && (data instanceof ArrayBuffer)) { | ||
if (data && ((data instanceof ArrayBuffer) || (SharedArrayBuffer && data instanceof SharedArrayBuffer))) { | ||
this.data = data; | ||
this._boxes = new this.ArrayType(this.data, 8, numNodes * 4); | ||
this._indices = new this.IndexArrayType(this.data, 8 + nodesByteSize, numNodes); | ||
|
@@ -67,7 +69,7 @@ export default class Flatbush { | |
this.maxY = this._boxes[this._pos - 1]; | ||
|
||
} else { | ||
this.data = new ArrayBuffer(8 + nodesByteSize + numNodes * this.IndexArrayType.BYTES_PER_ELEMENT); | ||
this.data = new this.ArrayBufferType(8 + nodesByteSize + numNodes * this.IndexArrayType.BYTES_PER_ELEMENT); | ||
this._boxes = new this.ArrayType(this.data, 8, numNodes * 4); | ||
this._indices = new this.IndexArrayType(this.data, 8 + nodesByteSize, numNodes); | ||
this._pos = 0; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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:
SharedArrayBuffer
isn't available, just referencing it will throw an error, so this won't work as a check. We need to usetypeof
.false
in case of a wrong data type, so the error won't be thrown.So this would need to look something like this: