-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Use a special data structure for transferring feature id maps to the main thread #7132
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
0ecb487
introduce a special structure for transfering feature id map
mourner b027de4
fix overflow in feature state offsets
mourner 31ae799
simplify feature position map
mourner 784293d
fix buffer offset for feature state, more resilient structure
mourner 19fc7b0
add a unit test for feature map
mourner 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// @flow | ||
|
||
import { register } from '../util/web_worker_transfer'; | ||
import assert from 'assert'; | ||
|
||
type SerializedFeaturePositionMap = { | ||
ids: Float64Array; | ||
positions: Uint32Array; | ||
}; | ||
|
||
type FeaturePosition = { | ||
index: number; | ||
start: number; | ||
end: number; | ||
}; | ||
|
||
// A transferable data structure that maps feature ids to their indices and buffer offsets | ||
export default class FeaturePositionMap { | ||
ids: Array<number>; | ||
positions: Array<number>; | ||
indexed: boolean; | ||
|
||
constructor() { | ||
this.ids = []; | ||
this.positions = []; | ||
this.indexed = false; | ||
} | ||
|
||
add(id: number, index: number, start: number, end: number) { | ||
this.ids.push(id); | ||
this.positions.push(index, start, end); | ||
} | ||
|
||
getPositions(id: number): Array<FeaturePosition> { | ||
assert(this.indexed); | ||
|
||
// binary search for the first occurrence of id in this.ids; | ||
// relies on ids/positions being sorted by id, which happens in serialization | ||
let i = 0; | ||
let j = this.ids.length - 1; | ||
while (i < j) { | ||
const m = (i + j) >> 1; | ||
if (this.ids[m] >= id) { | ||
j = m; | ||
} else { | ||
i = m + 1; | ||
} | ||
} | ||
const positions = []; | ||
while (this.ids[i] === id) { | ||
const index = this.positions[3 * i]; | ||
const start = this.positions[3 * i + 1]; | ||
const end = this.positions[3 * i + 2]; | ||
positions.push({index, start, end}); | ||
i++; | ||
} | ||
return positions; | ||
} | ||
|
||
static serialize(map: FeaturePositionMap, transferables: Array<ArrayBuffer>): SerializedFeaturePositionMap { | ||
const ids = new Float64Array(map.ids); | ||
const positions = new Uint32Array(map.positions); | ||
|
||
sort(ids, positions, 0, ids.length - 1); | ||
|
||
transferables.push(ids.buffer, positions.buffer); | ||
|
||
return {ids, positions}; | ||
} | ||
|
||
static deserialize(obj: SerializedFeaturePositionMap): FeaturePositionMap { | ||
const map = new FeaturePositionMap(); | ||
// after transferring, we only use these arrays statically (no pushes), | ||
// so TypedArray vs Array distinction that flow points out doesn't matter | ||
map.ids = (obj.ids: any); | ||
map.positions = (obj.positions: any); | ||
map.indexed = true; | ||
return map; | ||
} | ||
} | ||
|
||
// custom quicksort that sorts ids, indices and offsets together (by ids) | ||
function sort(ids, positions, left, right) { | ||
if (left >= right) return; | ||
|
||
const pivot = ids[(left + right) >> 1]; | ||
let i = left - 1; | ||
let j = right + 1; | ||
|
||
while (true) { | ||
do i++; while (ids[i] < pivot); | ||
do j--; while (ids[j] > pivot); | ||
if (i >= j) break; | ||
swap(ids, i, j); | ||
swap(positions, 3 * i, 3 * j); | ||
swap(positions, 3 * i + 1, 3 * j + 1); | ||
swap(positions, 3 * i + 2, 3 * j + 2); | ||
} | ||
|
||
sort(ids, positions, left, j); | ||
sort(ids, positions, j + 1, right); | ||
} | ||
|
||
function swap(arr, i, j) { | ||
const tmp = arr[i]; | ||
arr[i] = arr[j]; | ||
arr[j] = tmp; | ||
} | ||
|
||
register('FeaturePositionMap', FeaturePositionMap); |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { test } from 'mapbox-gl-js-test'; | ||
|
||
import FeatureMap from '../../../src/data/feature_position_map'; | ||
import { serialize, deserialize } from '../../../src/util/web_worker_transfer'; | ||
|
||
test('FeaturePositionMap', (t) => { | ||
|
||
test('Can be queried after serialization/deserialization', (t) => { | ||
const featureMap = new FeatureMap(); | ||
featureMap.add(7, 1, 0, 1); | ||
featureMap.add(3, 2, 1, 2); | ||
featureMap.add(7, 3, 2, 3); | ||
featureMap.add(4, 4, 3, 4); | ||
featureMap.add(2, 5, 4, 5); | ||
featureMap.add(7, 6, 5, 7); | ||
|
||
const featureMap2 = deserialize(serialize(featureMap, [])); | ||
|
||
const compareIndex = (a, b) => a.index - b.index; | ||
|
||
t.same(featureMap2.getPositions(7).sort(compareIndex), [ | ||
{index: 1, start: 0, end: 1}, | ||
{index: 3, start: 2, end: 3}, | ||
{index: 6, start: 5, end: 7} | ||
].sort(compareIndex)); | ||
|
||
t.end(); | ||
}); | ||
|
||
test('Can not be queried before serialization/deserialization', (t) => { | ||
const featureMap = new FeatureMap(); | ||
featureMap.add(0, 1, 2, 3); | ||
|
||
t.throws(() => { | ||
featureMap.getPositions(0); | ||
}); | ||
|
||
t.end(); | ||
}); | ||
|
||
t.end(); | ||
}); |
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.
Would be nice to add a note here that it relies on sorting the
ids
andpositions
arrays as part of serialization. Maybe include a test to ensure that serialize sorts these.