GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for running inside a node 10.5 worker_thread by rpetrich · Pull Request #1007 · TryGhost/node-sqlite3

This repository was archived by the owner on Jul 1, 2026. It is now read-only.

Conversation

@rpetrich

@rpetrich rpetrich commented Jun 23, 2018

Copy link
Copy Markdown
Contributor

Properly tracks which event loop callbacks should be dispatched on so that node doesn't busy loop on the main thread if node-sqlite3 is called from a worker.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
rpetrich added 3 commits June 23, 2018 15:49
@rpetrich
Add support for node 10.5's --experimental-worker threading support
1b5aa3a
@rpetrich
Add test:worker script that runs the mocha tests inside a worker
c7bdf1f
@rpetrich
Use the default loop when running on node 9 or earlier
f91b4fe
@springmeyer

springmeyer commented Jun 23, 2018

Copy link
Copy Markdown
Contributor

Very cool, thanks for putting up this PR. So, this enables node-sqlite3 to be used from a native node.js thread started via the new worker_threads module in node >= v10?

I'm interested to hear more about what usecases you'll use this for. Also, as someone on the bleeding edge of worker_threads, curious if you'd be interested in working on comparing perf like proposed at mapbox/node-cpp-skel#143. That ticket proposing comparing perf between pure JS and C++, but your PR here gets me thinking that it would also be beneficial to have an easy to run benchmark that compares perf of using something like node-sqlite3 from the main loop vs from a worker. Interest?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rpetrich

rpetrich commented Jun 23, 2018

Copy link
Copy Markdown
Contributor Author

Yes. This PR makes it safe to use in a multithreaded context using node's new experimental worker threads feature. It does not introduce new APIs to enable sharing of connections in serialized mode between worker threads or to reduce overhead via sqlite's shared cache feature, but perhaps this could be explored in the future.

My basis for comparison is an existing worker implementation on top of child_process.fork with messages sent over their IPC channels. Behind the scenes node uses JSON.stringify/JSON.parse for this, so the bar for performance isn't exactly high 🙃. Within these workers lives userland code operating in a simulated browser environment with additional access to node APIs including node-sqlite3. Haven't assessed performance yet, but I expect worker threads to benchmark well for this use case (when I get there). My expectation is that node-sqlite3 performance will be the same regardless of whether used from a subprocess or a worker threads, but getting the data there will have less overhead when using worker threads.

If you are aware of benchmarks showing the overhead of JS worker threads vs C++ native modules with background threads, that would be very interesting though not directly relevant to the project that led to this PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@springmeyer

springmeyer commented Jun 24, 2018

Copy link
Copy Markdown
Contributor

My expectation is that node-sqlite3 performance will be the same regardless of whether used from a subprocess or a worker threads, but getting the data there will have less overhead when using worker threads.

I'll be interested to know if that expectation is met. One thing that could negatively impact the performance, when run inside a worker_thread, is if any of the mutex locks inside libsqlite3 interact with the locking that (I presume) is needed to keep the worker_thread implementation safe. Hopefully this will not happen, but I worry it could.

If you are aware of benchmarks showing the overhead of JS worker threads vs C++ native modules with background threads, that would be very interesting though not directly relevant to the project that led to this PR.

Got it, none that I'm aware of which is why I created mapbox/node-cpp-skel#143

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@springmeyer

springmeyer commented Jun 24, 2018

Copy link
Copy Markdown
Contributor

Noticing that the only failure for this PR is unrelated (coverage build failed). So, merging now. This will likely land in v4.0.2 (when I cut it, but I don't have an ETA on that).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@springmeyer
springmeyer merged commit 36ec8cf into TryGhost:master Jun 24, 2018
@springmeyer springmeyer added this to the 4.0.2 milestone Jun 24, 2018
@rpetrich

rpetrich commented Jun 25, 2018

Copy link
Copy Markdown
Contributor Author

Unfortunately, I didn't do my due diligence and actually test with multiple workers. When I did so, it uncovered some additional adjustments that needed to be made. It also uncovered a bug in the way node's native module support interacts with thread_workers (see: nodejs/node#21517). Once this bug get's resolved, I'll follow up with an additional PR.

In the meantime, the performance script is available here: https://gist.github.com/rpetrich/2e49540a26e1e6e0dc2c57252b8a562c. It will require rpetrich/node-sqlite3@020a0eb and rpetrich/node@ffdc0c0

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@springmeyer

springmeyer commented Jun 25, 2018

Copy link
Copy Markdown
Contributor

Thanks for the followup. I'll revert then for now until this has a chance to settle out.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@springmeyer springmeyer mentioned this pull request Jun 25, 2018
Revert "Add support for running inside a node 10.5 worker_thread" #1009
Merged
@springmeyer springmeyer removed this from the 4.0.2 milestone Jun 25, 2018
@springmeyer springmeyer mentioned this pull request Jul 5, 2018
[FUTURE] Write a comparison benchmark using worker_threads module mapbox/node-cpp-skel#143
Open
@kewde kewde mentioned this pull request Sep 30, 2019
Cannot require sqlite3 in both a main thread and a worker thread #1223
Closed
@kewde

kewde commented Sep 30, 2019

Copy link
Copy Markdown
Collaborator

@rpetrich

Could you inform me if the upstream bug has been resolved or not?

bradisbell, magnetnation, and n1ru4l reacted with thumbs up emoji
All reactions
  • 👍 3 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@kewde kewde mentioned this pull request Feb 8, 2020
Make sqlite3 context aware for Electron 7+ compatibility #1274
Closed
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@rpetrich @springmeyer @kewde
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.