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

Use a single SharedArrayBuffer #154

Merged
merged 5 commits into from Jan 3, 2024
Merged

Conversation

onigoetz
Copy link
Contributor

@onigoetz onigoetz commented Jan 2, 2024

As a replacement to PR #150 this is a new PR that uses a single SharedArrayBuffer instead of creating a new one on each call.

More precisely this PR brings the following changes:

  1. Use a single SharedArrayBuffer that's initialized once and passed to the worker at initialization time
  2. Reduce the size of the SharedArrayBuffer to 4 bytes; since it is only used for notification purposes, this is the only byte that is needed in this SharedArrayBuffer
  3. Remove the bufferSize option and SYNCKIT_BUFFER_SIZE env variable as they are not needed anymore (they actually had no effect before either)
  4. Keep the method signature that contains bufferSize as removing it would effectively be a backwards compatibility break and I think it's better to avoid it.
  5. Add an Atomics.store at the beginning of the function calling the worker to reset the SharedArrayBuffer otherwise it would not wait for the result

Here is the benchmark accompanying this change:

Execution time
Memory usage after: native 141.443072MB (+82.00192MB)
Memory usage after: sync-threads (PR #3) 154.042368MB (+12.599296MB)
Memory usage after: synckit (0.8.8) 200.146944MB (+46.104576MB)
Memory usage after: synckit (PR #150) 237.666304MB (+37.51936MB)
Memory usage after: synckit (PR #154) 272.334848MB (+34.668544MB)
Memory usage after: sync-threads (1.0.1) 5780.946944MB (+5508.612096MB)

Fastest is native
┌─────────┬────────────────────────┬───────────┬────────────┬─────────┐
│ (index) │       Task Name        │  ops/sec  │   Margin   │ Samples │
├─────────┼────────────────────────┼───────────┼────────────┼─────────┤
│    0    │        'native'        │ '153,289' │ '± 0.39%'  │   95    │
│    1    │  'synckit (PR #154)'   │ '16,382'  │ '± 0.26%'  │   96    │
│    2    │   'synckit (0.8.8)'    │ '16,026'  │ '± 0.40%'  │   98    │
│    3    │ 'sync-threads (PR #3)' │ '15,985'  │ '± 0.26%'  │   95    │
│    4    │  'synckit (PR #150)'   │ '14,842'  │ '± 0.54%'  │   93    │
│    5    │ 'sync-threads (1.0.1)' │   '367'   │ '± 45.23%' │   10    │
└─────────┴────────────────────────┴───────────┴────────────┴─────────┘

Apart from the performance itself, this PR should also bring a much reduced pressure on the memory as it requires a single 4B SharedArrayBuffer per initialized function instead of a 1024B SharedArrayBuffer per call

Copy link

changeset-bot bot commented Jan 2, 2024

🦋 Changeset detected

Latest commit: 4a98a17

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
synckit Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

codesandbox-ci bot commented Jan 2, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link

codecov bot commented Jan 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (e21f030) 100.00% compared to head (4a98a17) 100.00%.

Additional details and impacted files
@@            Coverage Diff            @@
##              main      #154   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines          207       204    -3     
  Branches       103       100    -3     
=========================================
- Hits           207       204    -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

README.md Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/index.ts Outdated Show resolved Hide resolved
src/index.ts Show resolved Hide resolved
Copy link
Member

@JounQin JounQin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your awesome work! @onigoetz

@JounQin JounQin enabled auto-merge (squash) January 3, 2024 17:49
@JounQin JounQin merged commit 2541a1e into un-ts:main Jan 3, 2024
19 checks passed
@JounQin JounQin mentioned this pull request Jan 3, 2024
@JounQin JounQin added the perf label Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants