masterrecord 1.32.3 → 1.32.4
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/CHANGES.md +16 -0
- package/context.js +33 -15
- package/package.json +1 -1
package/CHANGES.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# MasterRecord Changelog
|
|
2
2
|
|
|
3
|
+
## v1.32.4 — a context is freed as soon as nothing uses it
|
|
4
|
+
|
|
5
|
+
**Fix: every context stayed in memory until the current job ended.** Each context registered
|
|
6
|
+
`new WeakRef(this)` for the cross-context dirty check. V8 holds a WeakRef's target strongly until
|
|
7
|
+
control returns to the event loop, so a chain of awaits that never did (synchronous SQLite, a worker
|
|
8
|
+
run driven in microtasks, a test) kept every context it created, closed or not, with all its dbsets.
|
|
9
|
+
For CodeBook's 311-table `KanbanContext` that was about 3 MB a context: its `agent-output-limits`
|
|
10
|
+
test ended with 289 MB on the heap after a full collection, and 58 MB with this fix.
|
|
11
|
+
|
|
12
|
+
- The registry now holds a WeakRef to a small entry that shares the context's dirty index and never
|
|
13
|
+
points at the context. The cross-context warning reads that index, which holds every pending change.
|
|
14
|
+
- `attach()` now also takes the entity out of its old context's change set. Before, the old context's
|
|
15
|
+
`saveChanges()` still flushed an entity that had moved to another context.
|
|
16
|
+
- New `test/context-memory-freed.test.js` builds 40 contexts in one job, closed and dropped, read and
|
|
17
|
+
written, and checks each leaves under 100 KB behind (it was about 640 KB for its 120-table context).
|
|
18
|
+
|
|
3
19
|
## v1.32.3 — building a context is 7 times cheaper
|
|
4
20
|
|
|
5
21
|
**Fix: `dbset()` walked every registered entity each time it ran.** Registering an entity re-resolved
|
package/context.js
CHANGED
|
@@ -101,11 +101,22 @@ function _ensurePoolReaper() {
|
|
|
101
101
|
// entity loaded by context A, then call saveChanges() on context B (a different
|
|
102
102
|
// instance), and B silently writes nothing because A owns the change tracking.
|
|
103
103
|
//
|
|
104
|
-
// Leak-safe: the Set holds WeakRefs
|
|
105
|
-
//
|
|
106
|
-
//
|
|
107
|
-
|
|
108
|
-
|
|
104
|
+
// Leak-safe: the Set holds WeakRefs, and a FinalizationRegistry prunes a WeakRef
|
|
105
|
+
// once its target is GC'd. Live contexts are also removed explicitly on close().
|
|
106
|
+
//
|
|
107
|
+
// The WeakRefs point at a small entry ({ dirty: <the context's dirty index> }),
|
|
108
|
+
// never at the context itself. Creating a WeakRef (and every deref()) adds its
|
|
109
|
+
// target to V8's "kept during job" list, which holds it STRONGLY until the
|
|
110
|
+
// current job ends. A job ends only when control returns to the event loop, so
|
|
111
|
+
// in a long chain of awaits that never does (synchronous SQLite, a run driven
|
|
112
|
+
// in microtasks) every context created stayed alive with all its dbsets (about
|
|
113
|
+
// 3 MB for a 311-table context), closed or not. Pinning the entry instead pins
|
|
114
|
+
// only the dirty index, which is empty once the context saved or closed.
|
|
115
|
+
//
|
|
116
|
+
// The global key changed with this shape (v2), so a second copy of the package
|
|
117
|
+
// loaded in the same process never mixes the two kinds of entry.
|
|
118
|
+
const _liveContexts = global.__MR_LIVE_CONTEXTS_V2__ || (global.__MR_LIVE_CONTEXTS_V2__ = new Set());
|
|
119
|
+
const _liveContextFinalizer = global.__MR_LIVE_CTX_FIN_V2__ || (global.__MR_LIVE_CTX_FIN_V2__ =
|
|
109
120
|
new FinalizationRegistry((ref) => { _liveContexts.delete(ref); }));
|
|
110
121
|
|
|
111
122
|
function _poolKey(type, cfg) {
|
|
@@ -407,11 +418,13 @@ class context {
|
|
|
407
418
|
this.__trackedEntitiesMap = new Map(); // Initialize Map for O(1) lookups
|
|
408
419
|
|
|
409
420
|
// Register this instance for cross-context change-tracking detection
|
|
410
|
-
// (see _liveContexts).
|
|
411
|
-
//
|
|
412
|
-
|
|
421
|
+
// (see _liveContexts). The registry holds a WeakRef to an entry that
|
|
422
|
+
// shares this context's dirty index and must never reference the context
|
|
423
|
+
// itself (see _liveContexts for why); removed on close() and pruned on GC.
|
|
424
|
+
this.__liveEntry = { dirty: this.__dirtyEntities };
|
|
425
|
+
this.__liveRef = new WeakRef(this.__liveEntry);
|
|
413
426
|
_liveContexts.add(this.__liveRef);
|
|
414
|
-
_liveContextFinalizer.register(this, this.__liveRef, this.__liveRef);
|
|
427
|
+
_liveContextFinalizer.register(this.__liveEntry, this.__liveRef, this.__liveRef);
|
|
415
428
|
|
|
416
429
|
// Track if this is the first instance of this context class
|
|
417
430
|
// Used to determine if duplicate warnings should be shown
|
|
@@ -4015,6 +4028,7 @@ class context {
|
|
|
4015
4028
|
_liveContexts.delete(this.__liveRef);
|
|
4016
4029
|
_liveContextFinalizer.unregister(this.__liveRef);
|
|
4017
4030
|
this.__liveRef = null;
|
|
4031
|
+
this.__liveEntry = null;
|
|
4018
4032
|
}
|
|
4019
4033
|
|
|
4020
4034
|
// Find this instance's pool in the registry and decrement
|
|
@@ -4166,6 +4180,9 @@ class context {
|
|
|
4166
4180
|
if (previous && previous !== this && entity.__ID &&
|
|
4167
4181
|
previous.__trackedEntitiesMap && previous.__trackedEntitiesMap.has(entity.__ID)) {
|
|
4168
4182
|
previous.__trackedEntitiesMap.delete(entity.__ID); // list view follows the map
|
|
4183
|
+
// Leave the old context's dirty index too: it is what the old
|
|
4184
|
+
// context's saveChanges() flushes and what the cross-context check reads.
|
|
4185
|
+
if (previous.__dirtyEntities) previous.__dirtyEntities.delete(entity);
|
|
4169
4186
|
if (typeof previous.__unindexIdentity === 'function') previous.__unindexIdentity(entity);
|
|
4170
4187
|
}
|
|
4171
4188
|
|
|
@@ -4620,12 +4637,13 @@ class context {
|
|
|
4620
4637
|
_foreignDirtyEntities() {
|
|
4621
4638
|
const foreign = [];
|
|
4622
4639
|
for (const ref of _liveContexts) {
|
|
4623
|
-
const
|
|
4624
|
-
if (!
|
|
4625
|
-
if (
|
|
4626
|
-
|
|
4627
|
-
|
|
4628
|
-
|
|
4640
|
+
const entry = ref.deref();
|
|
4641
|
+
if (!entry) { _liveContexts.delete(ref); continue; } // prune dead ref
|
|
4642
|
+
if (entry === this.__liveEntry) { continue; }
|
|
4643
|
+
// The dirty index is the complete set of a context's pending changes
|
|
4644
|
+
// (every dirty transition goes through __markDirty / __track).
|
|
4645
|
+
for (const e of entry.dirty) {
|
|
4646
|
+
if (context._isEntityDirty(e) && !e.__noTracking) { foreign.push(e); }
|
|
4629
4647
|
}
|
|
4630
4648
|
}
|
|
4631
4649
|
return foreign;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "masterrecord",
|
|
3
|
-
"version": "1.32.
|
|
3
|
+
"version": "1.32.4",
|
|
4
4
|
"description": "An Object-relational mapping for the Master framework. Master Record connects classes to relational database tables to establish a database with almost zero-configuration ",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./MasterRecord.js",
|