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

[Proposal] - Thead Safe data structures #55568

Open
HiImGiovi opened this issue Oct 28, 2024 · 3 comments
Open

[Proposal] - Thead Safe data structures #55568

HiImGiovi opened this issue Oct 28, 2024 · 3 comments
Labels
feature request Issues that request new features to be added to Node.js. worker Issues and PRs related to Worker support.

Comments

@HiImGiovi
Copy link

What is the problem this feature will solve?

What I'd like to achieve is an implementation of shared (between worker threads) and thread safe data structures.
I want something easier to work with when doing multithread nodejs. Sharing data between threads using SharedArrayBuffer can be hard sometimes.

What is the feature you are proposing to solve the problem?

I'll show you an example of what the API usage could look like for a data structure like LRU Cache:

This is the code in the main thread

// main 
import {LRUCache} from "shared-ds"
import {Worker} from "worker_threads"

const cache = new LRUCache();

cache.set("test", { p1: "property1", p2: "property2" })

const worker = new Worker("./worker.js", {
    workerData: {
          lruCache: cache
    }
}

this is the worker code:

import { workerData } from "worker_threads";
import { LRUCache } from "shared-ds";


const cache = LruCache.fromWorkerData(workerData.lruCache);

const testValue = cache.get("test");
console.log(testValue); // this outputs { p1: "property1", p2: "property2" }

I started working into an implementation that uses extensively SharedArrayBuffer and Atomics to reach thread safety but there are lots of problems to take into consideration like:

  • Serialization of data into SharedArrayBuffer
  • atomics operations ( I still did not figure out how I can read multiple 'bytes atomically' avoiding multiple Atomic.load calls)

Do you guys think it's a useful feature and worth implementing or taking it into consideration?

What alternatives have you considered?

No response

@HiImGiovi HiImGiovi added the feature request Issues that request new features to be added to Node.js. label Oct 28, 2024
@github-project-automation github-project-automation bot moved this to Awaiting Triage in Node.js feature requests Oct 28, 2024
@avivkeller avivkeller added the worker Issues and PRs related to Worker support. label Oct 28, 2024
@Jamesernator
Copy link

There is a proposal for shared objects.

There's an older prototype of that proposal (without the struct syntax) behind the flag --harmony-struct, use new SharedStructType(["key1", "key2"]) to create constructors.

( I still did not figure out how I can read multiple 'bytes atomically' avoiding multiple Atomic.load calls)

This is impossible with atomics, they can only read a word (8 bytes or less) at a time. Just use a lock/mutex (the prototype has Atomics.Mutex).

@avivkeller
Copy link
Member

avivkeller commented Oct 30, 2024

Workers are, IIUC, a bit different from web workers. Workers are executed from a subprocess of node (a separate command), so transferring data isn't as straightforward as one might think.

Edit: My knowledge of workers isn't very accurate 😅

@joyeecheung
Copy link
Member

joyeecheung commented Oct 31, 2024

Workers are executed from a subprocess of node (a separate command)

No, they are executed on a different thread but it's still in the same process. In terms of data sharing, it's not too different from Web workers. When V8 ships shared structs, they will also be usable in Node.js workers (maybe requires some additional integration patches to work properly, though we do already have some experimental integration for shared structs behind flags #47706)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. worker Issues and PRs related to Worker support.
Projects
Status: Awaiting Triage
Development

No branches or pull requests

4 participants