|
1 | 1 | import { globalVariable } from "./find-global.js"; |
2 | 2 | import { ref } from "./types.js"; |
3 | 3 |
|
4 | | -type SwiftRuntimeHeapEntry = { |
5 | | - id: number; |
6 | | - rc: number; |
7 | | -}; |
| 4 | +const SLOT_BITS = 24; |
| 5 | +const SLOT_MASK = (1 << SLOT_BITS) - 1; |
| 6 | +const GEN_MASK = (1 << (32 - SLOT_BITS)) - 1; |
| 7 | + |
8 | 8 | export class JSObjectSpace { |
9 | | - private _heapValueById: Map<number, any>; |
10 | | - private _heapEntryByValue: Map<any, SwiftRuntimeHeapEntry>; |
11 | | - private _heapNextKey: number; |
| 9 | + private _slotByValue: Map<any, number>; |
| 10 | + private _values: (any | undefined)[]; |
| 11 | + private _stateBySlot: number[]; |
| 12 | + private _freeSlotStack: number[]; |
12 | 13 |
|
13 | 14 | constructor() { |
14 | | - this._heapValueById = new Map(); |
15 | | - this._heapValueById.set(1, globalVariable); |
16 | | - |
17 | | - this._heapEntryByValue = new Map(); |
18 | | - this._heapEntryByValue.set(globalVariable, { id: 1, rc: 1 }); |
| 15 | + this._slotByValue = new Map(); |
| 16 | + this._values = []; |
| 17 | + this._stateBySlot = []; |
| 18 | + this._freeSlotStack = []; |
19 | 19 |
|
20 | 20 | // Note: 0 is preserved for invalid references, 1 is preserved for globalThis |
21 | | - this._heapNextKey = 2; |
| 21 | + this._values[0] = undefined; |
| 22 | + this._values[1] = globalVariable; |
| 23 | + this._slotByValue.set(globalVariable, 1); |
| 24 | + this._stateBySlot[1] = 1; // gen=0, rc=1 |
22 | 25 | } |
23 | 26 |
|
24 | 27 | retain(value: any) { |
25 | | - const entry = this._heapEntryByValue.get(value); |
26 | | - if (entry) { |
27 | | - entry.rc++; |
28 | | - return entry.id; |
| 28 | + const slot = this._slotByValue.get(value); |
| 29 | + if (slot !== undefined) { |
| 30 | + const state = this._stateBySlot[slot]!; |
| 31 | + const nextState = (state + 1) >>> 0; |
| 32 | + if ((nextState & SLOT_MASK) === 0) { |
| 33 | + throw new RangeError( |
| 34 | + `Reference count overflow at slot ${slot}`, |
| 35 | + ); |
| 36 | + } |
| 37 | + this._stateBySlot[slot] = nextState; |
| 38 | + return ((nextState & ~SLOT_MASK) | slot) >>> 0; |
| 39 | + } |
| 40 | + |
| 41 | + let newSlot: number; |
| 42 | + let state: number; |
| 43 | + if (this._freeSlotStack.length > 0) { |
| 44 | + newSlot = this._freeSlotStack.pop()!; |
| 45 | + const gen = this._stateBySlot[newSlot]! >>> SLOT_BITS; |
| 46 | + state = ((gen << SLOT_BITS) | 1) >>> 0; |
| 47 | + } else { |
| 48 | + newSlot = this._values.length; |
| 49 | + if (newSlot > SLOT_MASK) { |
| 50 | + throw new RangeError( |
| 51 | + `Reference slot overflow: ${newSlot} exceeds ${SLOT_MASK}`, |
| 52 | + ); |
| 53 | + } |
| 54 | + state = 1; |
29 | 55 | } |
30 | | - const id = this._heapNextKey++; |
31 | | - this._heapValueById.set(id, value); |
32 | | - this._heapEntryByValue.set(value, { id: id, rc: 1 }); |
33 | | - return id; |
| 56 | + |
| 57 | + this._stateBySlot[newSlot] = state; |
| 58 | + this._values[newSlot] = value; |
| 59 | + this._slotByValue.set(value, newSlot); |
| 60 | + return ((state & ~SLOT_MASK) | newSlot) >>> 0; |
34 | 61 | } |
35 | 62 |
|
36 | | - retainByRef(ref: ref) { |
37 | | - return this.retain(this.getObject(ref)); |
| 63 | + retainByRef(reference: ref) { |
| 64 | + const state = this._getValidatedSlotState(reference); |
| 65 | + const slot = reference & SLOT_MASK; |
| 66 | + const nextState = (state + 1) >>> 0; |
| 67 | + if ((nextState & SLOT_MASK) === 0) { |
| 68 | + throw new RangeError(`Reference count overflow at slot ${slot}`); |
| 69 | + } |
| 70 | + this._stateBySlot[slot] = nextState; |
| 71 | + return reference; |
38 | 72 | } |
39 | 73 |
|
40 | | - release(ref: ref) { |
41 | | - const value = this._heapValueById.get(ref); |
42 | | - const entry = this._heapEntryByValue.get(value)!; |
43 | | - entry.rc--; |
44 | | - if (entry.rc != 0) return; |
| 74 | + release(reference: ref) { |
| 75 | + const state = this._getValidatedSlotState(reference); |
| 76 | + const slot = reference & SLOT_MASK; |
| 77 | + if ((state & SLOT_MASK) > 1) { |
| 78 | + this._stateBySlot[slot] = (state - 1) >>> 0; |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + this._slotByValue.delete(this._values[slot]); |
| 83 | + this._values[slot] = undefined; |
| 84 | + const nextGen = ((state >>> SLOT_BITS) + 1) & GEN_MASK; |
| 85 | + this._stateBySlot[slot] = (nextGen << SLOT_BITS) >>> 0; |
| 86 | + this._freeSlotStack.push(slot); |
| 87 | + } |
45 | 88 |
|
46 | | - this._heapEntryByValue.delete(value); |
47 | | - this._heapValueById.delete(ref); |
| 89 | + getObject(reference: ref) { |
| 90 | + this._getValidatedSlotState(reference); |
| 91 | + return this._values[reference & SLOT_MASK]; |
48 | 92 | } |
49 | 93 |
|
50 | | - getObject(ref: ref) { |
51 | | - const value = this._heapValueById.get(ref); |
52 | | - if (value === undefined) { |
| 94 | + // Returns the packed state for the slot, after validating the reference. |
| 95 | + private _getValidatedSlotState(reference: ref): number { |
| 96 | + const slot = reference & SLOT_MASK; |
| 97 | + if (slot === 0) |
| 98 | + throw new ReferenceError( |
| 99 | + `Attempted to use invalid reference ${reference}`, |
| 100 | + ); |
| 101 | + const state = this._stateBySlot[slot]; |
| 102 | + if (state === undefined || (state & SLOT_MASK) === 0) { |
| 103 | + throw new ReferenceError( |
| 104 | + `Attempted to use invalid reference ${reference}`, |
| 105 | + ); |
| 106 | + } |
| 107 | + if (state >>> SLOT_BITS !== reference >>> SLOT_BITS) { |
53 | 108 | throw new ReferenceError( |
54 | | - "Attempted to read invalid reference " + ref, |
| 109 | + `Attempted to use stale reference ${reference}`, |
55 | 110 | ); |
56 | 111 | } |
57 | | - return value; |
| 112 | + return state; |
58 | 113 | } |
59 | 114 | } |
0 commit comments