bun-shared-memory 0.1.2 โ†’ 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +87 -10
  2. package/package.json +9 -3
package/README.md CHANGED
@@ -1,4 +1,25 @@
1
- # bun-shared-memory
1
+ <div align="center">
2
+
3
+ # ๐Ÿง  bun-shared-memory
4
+
5
+ **Share data between Bun workers/threads using `SharedArrayBuffer`** โ€” zero-copy, 4-buffer architecture, Atomics-based synchronization.
6
+
7
+ [![npm version](https://img.shields.io/npm/v/bun-shared-memory?style=for-the-badge&logo=npm&color=cb3837)](https://www.npmjs.com/package/bun-shared-memory)
8
+ [![npm downloads](https://img.shields.io/npm/dm/bun-shared-memory?style=for-the-badge&logo=npm&color=cb3837)](https://www.npmjs.com/package/bun-shared-memory)
9
+ [![npm bundle size](https://img.shields.io/bundlephobia/minzip/bun-shared-memory?style=for-the-badge&color=blueviolet)](https://bundlephobia.com/package/bun-shared-memory)
10
+
11
+ [![GitHub stars](https://img.shields.io/github/stars/Kapsonfire/bun-shared-memory?style=for-the-badge&logo=github&color=181717)](https://github.com/Kapsonfire/bun-shared-memory)
12
+ [![GitHub forks](https://img.shields.io/github/forks/Kapsonfire/bun-shared-memory?style=for-the-badge&logo=github&color=181717)](https://github.com/Kapsonfire/bun-shared-memory)
13
+ [![GitHub issues](https://img.shields.io/github/issues/Kapsonfire/bun-shared-memory?style=for-the-badge&logo=github&color=181717)](https://github.com/Kapsonfire/bun-shared-memory/issues)
14
+ [![GitHub license](https://img.shields.io/github/license/Kapsonfire/bun-shared-memory?style=for-the-badge&color=green)](https://github.com/Kapsonfire/bun-shared-memory/blob/main/LICENSE)
15
+
16
+ [![Bun](https://img.shields.io/badge/Bun-%E2%89%A51.4.0-black?style=for-the-badge&logo=bun&color=14151a)](https://bun.sh)
17
+ [![TypeScript](https://img.shields.io/badge/TypeScript-7-blue?style=for-the-badge&logo=typescript&color=3178c6)](https://www.typescriptlang.org)
18
+ [![Tests](https://img.shields.io/badge/tests-passing-brightgreen?style=for-the-badge&logo=checkmarx)](https://github.com/Kapsonfire/bun-shared-memory/actions)
19
+
20
+ </div>
21
+
22
+ ---
2
23
 
3
24
  A TypeScript library for the **Bun** runtime that enables sharing data between workers/threads using `SharedArrayBuffer`.
4
25
 
@@ -7,7 +28,42 @@ A TypeScript library for the **Bun** runtime that enables sharing data between w
7
28
  > onward. Core shared-memory operations (including `SharedArrayBuffer.grow()`)
8
29
  > work on earlier versions, but the automatic worker buffer-sharing does not.
9
30
 
10
- ## Install
31
+ ## ๐Ÿ“ˆ Real-world benchmark
32
+
33
+ `bun run bench:real` compares four methodologies for serving a "get user"
34
+ request through an [Elysia](https://elysiajs.com) mini API. Requests are built
35
+ by hand and passed directly to `app.fetch()` (no HTTP server); each methodology
36
+ is benchmarked sequentially with 4 workers. See
37
+ [`bench/real-world/README.md`](bench/real-world/README.md) for setup.
38
+
39
+ Measured on Linux (Bun 1.4.4, 100,000 requests per methodology, sorted by
40
+ speed; MySQL/Redis benchmarked over both TCP and Unix socket):
41
+
42
+ | Methodology | Transport | Requests | Elapsed (ms) | req/s | vs. shared |
43
+ |-------------|-----------|----------|--------------|------------|------------|
44
+ | `shared` | โ€” | 100,000 | 52 | 1,925,042 | โ€” |
45
+ | `sqlite` | โ€” | 100,000 | 187 | 535,469 | 3.6ร— slower |
46
+ | `redis` | socket | 100,000 | 1,688 | 59,233 | 32.5ร— slower |
47
+ | `mysql` | socket | 100,000 | 1,823 | 54,861 | 35.1ร— slower |
48
+ | `redis` | tcp | 100,000 | 2,863 | 34,923 | 55.1ร— slower |
49
+ | `mysql` | tcp | 100,000 | 3,281 | 30,476 | 63.2ร— slower |
50
+
51
+ Shared memory is the fastest by a wide margin: ~3.6ร— faster than a live SQLite
52
+ read and ~33โ€“63ร— faster than live MySQL/Redis reads. Unix sockets are
53
+ significantly faster than TCP for MySQL/Redis (~1.7ร—), but both remain far
54
+ slower than shared memory.
55
+
56
+ ## โœจ Features
57
+
58
+ - ๐Ÿš€ **Zero-copy** shared memory between workers/threads via `SharedArrayBuffer`
59
+ - ๐Ÿงฉ **4-buffer architecture** โ€” Index Table, DataTable, append-only StringTable, ArrayTable
60
+ - ๐Ÿ”’ **Atomics-based synchronization** โ€” re-entrant `Mutex` + `Notifier`
61
+ - โšก **Read & offset caches** โ€” lock-free fast path, cross-worker invalidation via version cells
62
+ - ๐Ÿงน **Reference counting & defragmentation** โ€” automatic memory reclamation
63
+ - ๐Ÿช„ **Proxy-based API** โ€” `Shared()` with direct getter/setter access
64
+ - ๐Ÿงต **Automatic worker integration** โ€” no manual buffer serialization
65
+
66
+ ## ๐Ÿ“ฆ Install
11
67
 
12
68
  ```sh
13
69
  bun add bun-shared-memory
@@ -15,15 +71,18 @@ bun add bun-shared-memory
15
71
 
16
72
  See [SPEC.md](./SPEC.md) for the full specification.
17
73
 
18
- ## Status
19
-
20
- โœ… **M3 โ€” Synchronization**: `Atomics`-based locking (`Mutex`) and notifications (`Notifier`) between workers, integrated into all shared-memory operations.
74
+ ## ๐Ÿ“Š Stats
21
75
 
22
- โœ… **Read cache**: reads are cached per worker and invalidated cross-worker via a per-key version counter.
23
-
24
- โœ… **Reference counting & defragmentation**: workers acquire/release references to shared objects/arrays (released on GC), the main thread tracks which worker holds what, and a defragmentation pass reclaims dead space when free space is low.
25
-
26
- โœ… **M4 โ€” Worker integration**: a worker can simply call `Shared()` and automatically share the same buffers as the main thread. Both sides call `init()` โ€” the main thread answers buffer requests, each worker requests the shared buffers โ€” no manual buffer serialization needed.
76
+ | | |
77
+ |---|---|
78
+ | **Version** | [![npm version](https://img.shields.io/npm/v/bun-shared-memory?color=cb3837)](https://www.npmjs.com/package/bun-shared-memory) |
79
+ | **Downloads / month** | [![npm downloads](https://img.shields.io/npm/dm/bun-shared-memory?color=cb3837)](https://www.npmjs.com/package/bun-shared-memory) |
80
+ | **Stars** | [![GitHub stars](https://img.shields.io/github/stars/Kapsonfire/bun-shared-memory?color=181717)](https://github.com/Kapsonfire/bun-shared-memory) |
81
+ | **Forks** | [![GitHub forks](https://img.shields.io/github/forks/Kapsonfire/bun-shared-memory?color=181717)](https://github.com/Kapsonfire/bun-shared-memory) |
82
+ | **Open issues** | [![GitHub issues](https://img.shields.io/github/issues/Kapsonfire/bun-shared-memory?color=181717)](https://github.com/Kapsonfire/bun-shared-memory/issues) |
83
+ | **Open PRs** | [![GitHub pull requests](https://img.shields.io/github/issues-pr/Kapsonfire/bun-shared-memory?color=181717)](https://github.com/Kapsonfire/bun-shared-memory/pulls) |
84
+ | **License** | [![License](https://img.shields.io/github/license/Kapsonfire/bun-shared-memory?color=green)](https://github.com/Kapsonfire/bun-shared-memory/blob/main/LICENSE) |
85
+ | **Language** | [![TypeScript](https://img.shields.io/badge/TypeScript-7-blue?color=3178c6)](https://www.typescriptlang.org) |
27
86
 
28
87
  ## Data Types
29
88
 
@@ -335,3 +394,21 @@ per-worker LRU cache grows dynamically (configurable via `internCapacity` /
335
394
  `internMax` on `SharedMemory`) and evicts least-recently-used entries once it
336
395
  reaches capacity. Because the StringTable is append-only, cached references stay
337
396
  valid for the lifetime of the buffer.
397
+
398
+ ### Real-world benchmark
399
+
400
+ See [๐Ÿ“ˆ Real-world benchmark](#-real-world-benchmark) at the top of this
401
+ README for the measured results.
402
+
403
+ ## ๐Ÿค Contributing
404
+
405
+ Contributions are welcome! Please open an issue or pull request.
406
+
407
+ - ๐Ÿ› [Report a bug](https://github.com/Kapsonfire/bun-shared-memory/issues/new?labels=bug)
408
+ - ๐Ÿ’ก [Request a feature](https://github.com/Kapsonfire/bun-shared-memory/issues/new?labels=enhancement)
409
+ - ๐Ÿ“– [Browse open issues](https://github.com/Kapsonfire/bun-shared-memory/issues)
410
+ - ๐Ÿ”€ [Open a pull request](https://github.com/Kapsonfire/bun-shared-memory/pulls)
411
+
412
+ ## ๐Ÿ“„ License
413
+
414
+ [MIT](./LICENSE) ยฉ [Kapsonfire](https://github.com/Kapsonfire)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bun-shared-memory",
3
- "version": "0.1.2",
3
+ "version": "0.1.3",
4
4
  "description": "Share data between Bun workers/threads using SharedArrayBuffer (zero-copy, 4-buffer architecture, Atomics-based synchronization).",
5
5
  "type": "module",
6
6
  "module": "index.ts",
@@ -18,7 +18,10 @@
18
18
  "scripts": {
19
19
  "typecheck": "bun check",
20
20
  "test": "bun test",
21
- "bench": "bun bench/bench.ts"
21
+ "bench": "bun bench/bench.ts",
22
+ "bench:real": "bun bench/real-world/run.ts",
23
+ "bench:real:seed": "bun bench/real-world/seed.ts",
24
+ "bench:real:mysql": "docker compose -f bench/real-world/docker-compose.yml up -d"
22
25
  },
23
26
  "keywords": [
24
27
  "bun",
@@ -41,7 +44,10 @@
41
44
  "access": "public"
42
45
  },
43
46
  "devDependencies": {
44
- "@types/bun": "latest"
47
+ "@types/bun": "latest",
48
+ "elysia": "^1.4.30",
49
+ "ioredis": "^6.0.0",
50
+ "mysql2": "^3.24.6"
45
51
  },
46
52
  "peerDependencies": {
47
53
  "typescript": "^7"