@daneren2005/shared-memory-objects 0.0.4 → 0.0.6
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.
- package/package.json +60 -60
- package/src/allocated-memory.ts +8 -3
- package/src/memory-heap.ts +14 -1
package/package.json
CHANGED
|
@@ -1,60 +1,60 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@daneren2005/shared-memory-objects",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"author": "daneren2005@gmail.com",
|
|
5
|
-
"description": "Creating objects with a SharedArrayBuffer",
|
|
6
|
-
"homepage": "https://github.com/daneren2005/shared-memory-objects#readme",
|
|
7
|
-
"keywords": [
|
|
8
|
-
"sharedarraybuffer"
|
|
9
|
-
],
|
|
10
|
-
"license": "MIT",
|
|
11
|
-
"repository": {
|
|
12
|
-
"type": "git",
|
|
13
|
-
"url": "git+ssh://git@github.com/daneren2005/shared-memory-objects.git"
|
|
14
|
-
},
|
|
15
|
-
"bugs": {
|
|
16
|
-
"url": "https://github.com/daneren2005/shared-memory-objects/issues"
|
|
17
|
-
},
|
|
18
|
-
"type": "module",
|
|
19
|
-
"files": [
|
|
20
|
-
"src/",
|
|
21
|
-
"!__tests__"
|
|
22
|
-
],
|
|
23
|
-
"main": "./src/main.ts",
|
|
24
|
-
"exports": {
|
|
25
|
-
".": "./src/main.ts"
|
|
26
|
-
},
|
|
27
|
-
"scripts": {
|
|
28
|
-
"dev": "vite",
|
|
29
|
-
"lint": "eslint src/ --ext .js,.cjs,.mjs,.ts,.cts,.mts --ignore-path .gitignore",
|
|
30
|
-
"preview": "vite preview",
|
|
31
|
-
"publish": "npm publish --access public",
|
|
32
|
-
"start": "npm run dev",
|
|
33
|
-
"test:bench": "vitest bench",
|
|
34
|
-
"test:ui": "vitest --ui --api 9527",
|
|
35
|
-
"test:unit": "vitest run",
|
|
36
|
-
"test:unit:watch": "vitest",
|
|
37
|
-
"type-check": "tsc --build --force",
|
|
38
|
-
"prepare": "husky"
|
|
39
|
-
},
|
|
40
|
-
"devDependencies": {
|
|
41
|
-
"@rushstack/eslint-patch": "^1.10.1",
|
|
42
|
-
"@tsconfig/node18": "^18.2.4",
|
|
43
|
-
"@types/jsdom": "^21.1.6",
|
|
44
|
-
"@types/node": "^20.12.2",
|
|
45
|
-
"@vitest/coverage-v8": "^1.4.0",
|
|
46
|
-
"@vitest/ui": "^1.4.0",
|
|
47
|
-
"eslint": "^8.57.0",
|
|
48
|
-
"eslint-plugin-vitest": "^0.4.1",
|
|
49
|
-
"husky": "^9.0.11",
|
|
50
|
-
"jsdom": "^24.0.0",
|
|
51
|
-
"lint-staged": "^15.2.2",
|
|
52
|
-
"typescript": "^5.4.3",
|
|
53
|
-
"typescript-eslint": "^7.5.0",
|
|
54
|
-
"vite": "^5.2.0",
|
|
55
|
-
"vitest": "^1.4.0"
|
|
56
|
-
},
|
|
57
|
-
"dependencies": {
|
|
58
|
-
"pretty-bytes": "^6.1.1"
|
|
59
|
-
}
|
|
60
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "@daneren2005/shared-memory-objects",
|
|
3
|
+
"version": "0.0.6",
|
|
4
|
+
"author": "daneren2005@gmail.com",
|
|
5
|
+
"description": "Creating objects with a SharedArrayBuffer",
|
|
6
|
+
"homepage": "https://github.com/daneren2005/shared-memory-objects#readme",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"sharedarraybuffer"
|
|
9
|
+
],
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+ssh://git@github.com/daneren2005/shared-memory-objects.git"
|
|
14
|
+
},
|
|
15
|
+
"bugs": {
|
|
16
|
+
"url": "https://github.com/daneren2005/shared-memory-objects/issues"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"files": [
|
|
20
|
+
"src/",
|
|
21
|
+
"!__tests__"
|
|
22
|
+
],
|
|
23
|
+
"main": "./src/main.ts",
|
|
24
|
+
"exports": {
|
|
25
|
+
".": "./src/main.ts"
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"dev": "vite",
|
|
29
|
+
"lint": "eslint src/ --ext .js,.cjs,.mjs,.ts,.cts,.mts --ignore-path .gitignore",
|
|
30
|
+
"preview": "vite preview",
|
|
31
|
+
"publish": "npm publish --access public",
|
|
32
|
+
"start": "npm run dev",
|
|
33
|
+
"test:bench": "vitest bench",
|
|
34
|
+
"test:ui": "vitest --ui --api 9527",
|
|
35
|
+
"test:unit": "vitest run",
|
|
36
|
+
"test:unit:watch": "vitest",
|
|
37
|
+
"type-check": "tsc --build --force",
|
|
38
|
+
"prepare": "husky"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@rushstack/eslint-patch": "^1.10.1",
|
|
42
|
+
"@tsconfig/node18": "^18.2.4",
|
|
43
|
+
"@types/jsdom": "^21.1.6",
|
|
44
|
+
"@types/node": "^20.12.2",
|
|
45
|
+
"@vitest/coverage-v8": "^1.4.0",
|
|
46
|
+
"@vitest/ui": "^1.4.0",
|
|
47
|
+
"eslint": "^8.57.0",
|
|
48
|
+
"eslint-plugin-vitest": "^0.4.1",
|
|
49
|
+
"husky": "^9.0.11",
|
|
50
|
+
"jsdom": "^24.0.0",
|
|
51
|
+
"lint-staged": "^15.2.2",
|
|
52
|
+
"typescript": "^5.4.3",
|
|
53
|
+
"typescript-eslint": "^7.5.0",
|
|
54
|
+
"vite": "^5.2.0",
|
|
55
|
+
"vitest": "^1.4.0"
|
|
56
|
+
},
|
|
57
|
+
"dependencies": {
|
|
58
|
+
"pretty-bytes": "^6.1.1"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/allocated-memory.ts
CHANGED
|
@@ -38,9 +38,14 @@ export default class AllocatedMemory {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
getArray<T extends TypedArray>(type: TypedArrayConstructor<T>, offset: number, length: number): T {
|
|
41
|
-
if(import.meta.env.MODE === 'development') {
|
|
42
|
-
if(offset + length > this.data.
|
|
43
|
-
|
|
41
|
+
if(import.meta.env.MODE === 'development' || import.meta.env.MODE === 'test') {
|
|
42
|
+
if((offset + length) * type.BYTES_PER_ELEMENT > this.data.byteLength) {
|
|
43
|
+
const message = `Trying to grab more memory from AllocatedMemory.getArray then we have: ${offset * type.BYTES_PER_ELEMENT} + ${length * type.BYTES_PER_ELEMENT} > ${this.data.byteLength}`;
|
|
44
|
+
if(import.meta.env.MODE === 'test') {
|
|
45
|
+
throw new Error(message);
|
|
46
|
+
} else {
|
|
47
|
+
console.warn(message);
|
|
48
|
+
}
|
|
44
49
|
}
|
|
45
50
|
}
|
|
46
51
|
|
package/src/memory-heap.ts
CHANGED
|
@@ -32,6 +32,10 @@ export default class MemoryHeap {
|
|
|
32
32
|
});
|
|
33
33
|
this.isClone = true;
|
|
34
34
|
} else {
|
|
35
|
+
if(!('SharedArrayBuffer' in self)) {
|
|
36
|
+
console.warn('SharedArrayBuffer is not working: falling back to ArrayBuffer');
|
|
37
|
+
}
|
|
38
|
+
|
|
35
39
|
const bufferSize = config?.bufferSize ?? DEFAULT_BUFFER_SIZE;
|
|
36
40
|
if(bufferSize > MAX_BYTE_OFFSET_LENGTH) {
|
|
37
41
|
throw new Error(`Buffer size ${bufferSize} is greater than max ${MAX_BYTE_OFFSET_LENGTH} that we can reference with pointers`);
|
|
@@ -68,8 +72,16 @@ export default class MemoryHeap {
|
|
|
68
72
|
}
|
|
69
73
|
|
|
70
74
|
private createBuffer(bufferSize?: number): MemoryBuffer {
|
|
75
|
+
const usedBufferSize = bufferSize ?? this.bufferSize;
|
|
76
|
+
let buf: ArrayBuffer | SharedArrayBuffer;
|
|
77
|
+
if('SharedArrayBuffer' in globalThis) {
|
|
78
|
+
buf = new SharedArrayBuffer(usedBufferSize);
|
|
79
|
+
} else {
|
|
80
|
+
buf = new ArrayBuffer(usedBufferSize);
|
|
81
|
+
}
|
|
82
|
+
|
|
71
83
|
return new MemoryBuffer({
|
|
72
|
-
buf
|
|
84
|
+
buf,
|
|
73
85
|
|
|
74
86
|
// We can't use this unless we can 100% guarantee that every thread will stop using memory the instant it is freed
|
|
75
87
|
// ex: Allocate 16 bytes. Thread A frees that allocation and then allocates 12 bytes and 4 bytes, but Thread B is mid-execution on the old allocation can changes the internal state of the 4-byte allocation breaking everything
|
|
@@ -84,6 +96,7 @@ export default class MemoryHeap {
|
|
|
84
96
|
}
|
|
85
97
|
|
|
86
98
|
allocUI32(count: number): AllocatedMemory {
|
|
99
|
+
count = Math.ceil(count);
|
|
87
100
|
for(let i = 0; i < this.buffers.length; i++) {
|
|
88
101
|
const buffer = this.buffers[i];
|
|
89
102
|
// Should just mean we haven't synced this buffer from another thread yet
|