Context
I found this while profiling Memory Core's mark_read drain today (neo-agent-brain#464). 6.6 GB of that drain's allocation sat in the generic config getter's legacy array copy.
The engine's own collections read the same getter. collection.Base declares items_ as {clone: 'shallow', value: null} without a cloneOnGet, so every collection.items read returns a fresh copy. Measured: c.items !== c.items. One read of a 100k collection costs about 0.067 ms, against about 0.00013 ms for getCount().
The Problem
Two core loops read that getter once per iteration, so their cost grows quadratically:
Neo.collection.Base#findBy reads me.items[i] inside its loop, twice on a match.
Neo.data.Store#forEach passes me.items as the callback's fourth argument on every iteration.
Measured on dev@202699d10c with a unit probe whose callback does nothing:
| n |
Collection#findBy |
Store#forEach |
plain getAt loop |
| 1,000 |
1 ms |
1 ms |
0 ms |
| 10,000 |
13 ms |
13 ms |
2 ms |
| 50,000 |
1,802 ms |
1,876 ms |
10 ms |
The engine's callers are list.Base (while rendering), tree.List (three sites), tree.Accordion (two), and the list and tree drag zones. It is invisible at today's typical sizes, but at 50k records a single call blocks the App worker for about 1.9 s.
The Architectural Reality
- The generated config getter in
src/Neo.mjs has no cloneOnGet for items, so it takes the legacy branch: Array.isArray(value) leads to value.slice().
collection.Base already reads its backing array as me._items wherever it must not copy, 22 times in all.
data.Store#findBy delegates to super.findBy and maps the results to Records.
The Fix
Collection#findBy: iterate the backing _items array, not the getter.
Store#forEach: take one copy of items before the loop and pass it on every call. The fourth argument stays a defensive copy, but there is now one copy per call instead of one per record.
- Red-first unit cases count
items getter reads per call, so the test does not rely on timings.
Decision Record impact: none.
Acceptance Criteria
Out of Scope
- The getter's copy itself. Giving
items a non-copying cloneOnGet changes a public contract, since callers may rely on a copy they can mutate.
- Single reads such as
me.items.length, which cost O(n) per call, not per iteration.
Related
neo-agent-brain#464 and neo-agent-brain#467 cover the same copy class in Memory Core's graph store.
Sweeps (at filing):
- Live latest-open: latest 20 open issues, none equivalent. A keyword search over open issues found nothing.
- A2A: no claim on this scope.
- MC: the February 2026 sessions switched
items_ to a shallow clone on set but left these loops as they were.
- Own assignments: none on this surface.
Origin Session ID: 101d2ce9-9f43-4f5a-9ae2-3c75bf8f6fcf
Retrieval Hint: "collection items getter copies per read quadratic findBy Store forEach cloneOnGet"
Contract Ledger (added for PR #19193's review)
| Target surface |
Source of authority |
Behavior after the fix |
Fallback |
Docs |
Evidence |
Neo.collection.Base#findBy(fn, scope, start, end) |
the method |
Iterates the backing _items, re-read on every iteration, so the collection's current state stays visible to the callback. It returns the matching items as before. |
— |
JSDoc unchanged |
MassiveData.spec.mjs: 0 getter reads per call (110 on dev) |
Neo.data.Store#forEach(fn, scope), called back as fn(record, index, items) |
the method |
items is one defensive copy, shared by all the callbacks of a call: (1) every entry the loop has reached is its Record (items[index] === record, lazy hydration included); (2) a callback that changes the store's count gets a fresh copy from the next call on; (3) mutating items never reaches the store. |
— |
JSDoc on forEach ("treat it as read-only") |
Store.spec.mjs "forEach callback items": 3 cases, green on dev, red on a plain one-copy version. MassiveData.spec.mjs: 1 getter read per call when the count is unchanged. |
The one intentional change from dev: dev gave every call its own fresh copy, so a callback mutating items only affected its own copy. Now the copy is shared across calls, so treat it as read-only.
Context
I found this while profiling Memory Core's
mark_readdrain today (neo-agent-brain#464). 6.6 GB of that drain's allocation sat in the generic config getter's legacy array copy.The engine's own collections read the same getter.
collection.Basedeclaresitems_as{clone: 'shallow', value: null}without acloneOnGet, so everycollection.itemsread returns a fresh copy. Measured:c.items !== c.items. One read of a 100k collection costs about 0.067 ms, against about 0.00013 ms forgetCount().The Problem
Two core loops read that getter once per iteration, so their cost grows quadratically:
Neo.collection.Base#findByreadsme.items[i]inside its loop, twice on a match.Neo.data.Store#forEachpassesme.itemsas the callback's fourth argument on every iteration.Measured on
dev@202699d10cwith a unit probe whose callback does nothing:Collection#findByStore#forEachgetAtloopThe engine's callers are
list.Base(while rendering),tree.List(three sites),tree.Accordion(two), and the list and tree drag zones. It is invisible at today's typical sizes, but at 50k records a single call blocks the App worker for about 1.9 s.The Architectural Reality
src/Neo.mjshas nocloneOnGetforitems, so it takes the legacy branch:Array.isArray(value)leads tovalue.slice().collection.Basealready reads its backing array asme._itemswherever it must not copy, 22 times in all.data.Store#findBydelegates tosuper.findByand maps the results to Records.The Fix
Collection#findBy: iterate the backing_itemsarray, not the getter.Store#forEach: take one copy ofitemsbefore the loop and pass it on every call. The fourth argument stays a defensive copy, but there is now one copy per call instead of one per record.itemsgetter reads per call, so the test does not rely on timings.Decision Record impact: none.
Acceptance Criteria
Collection#findByover n items reads theitemsgetter zero times, and thatStore#forEachreads it once per call. Both cases are red ondev(n reads).findByandforEacheach finish within 5× of the plaingetAtloop. The receipt goes in the PR.Out of Scope
itemsa non-copyingcloneOnGetchanges a public contract, since callers may rely on a copy they can mutate.me.items.length, which cost O(n) per call, not per iteration.Related
neo-agent-brain#464 and neo-agent-brain#467 cover the same copy class in Memory Core's graph store.
Sweeps (at filing):
items_to a shallow clone on set but left these loops as they were.Origin Session ID: 101d2ce9-9f43-4f5a-9ae2-3c75bf8f6fcf Retrieval Hint: "collection items getter copies per read quadratic findBy Store forEach cloneOnGet"
Contract Ledger (added for PR #19193's review)
Neo.collection.Base#findBy(fn, scope, start, end)_items, re-read on every iteration, so the collection's current state stays visible to the callback. It returns the matching items as before.MassiveData.spec.mjs: 0 getter reads per call (110 ondev)Neo.data.Store#forEach(fn, scope), called back asfn(record, index, items)itemsis one defensive copy, shared by all the callbacks of a call: (1) every entry the loop has reached is its Record (items[index] === record, lazy hydration included); (2) a callback that changes the store's count gets a fresh copy from the next call on; (3) mutatingitemsnever reaches the store.forEach("treat it as read-only")Store.spec.mjs"forEach callback items": 3 cases, green ondev, red on a plain one-copy version.MassiveData.spec.mjs: 1 getter read per call when the count is unchanged.The one intentional change from
dev:devgave every call its own fresh copy, so a callback mutatingitemsonly affected its own copy. Now the copy is shared across calls, so treat it as read-only.