Skip to content

Handling the case where the argument is backed by a SharedArrayBuffer #8

@bakkot

Description

@bakkot

It would be nice if we could avoid copying the argument when it's a same-type TypedArray, but if so we have to think about how to handle the case where the argument is backed by a SharedArrayBuffer: what happens if the elements change while in the middle of the search? For example:

// main thread
let buf = new SharedArrayBuffer(2);
let needle = new Uint8Array(buf);
needle[0] = 2;
needle[1] = 3;
worker.postMessage(buf);

// later...
console.log(Uint8Array.of(2, 2, 3, 3, 2).search(needle));
// worker.js
self.onmessage = (e) => {
 let needle = new Uint8Array(e.data);
 needle[0] = 3;
 needle[1] = 2;
}

Can this ever return -1? If the argument is copied before doing the search, then it cannot: while any of the four of [2, 2], [2, 3], [3, 2], [3, 3] are possible values at the time the search begins, those all occur at least somewhere in the haystack. But if it is not copied, it could change from [2, 3] to [2, 2] immediately after checking the first index but before checking the second and not change again until after the search completes, and therefore fail to find it at all.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions