Skip to content

Commit 7efdeea

Browse files
committed
Clean up autotrackMemoize
1 parent ca25a77 commit 7efdeea

File tree

5 files changed

+3
-205
lines changed

5 files changed

+3
-205
lines changed

src/autotrackMemoize/autotrackMemoize.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
export function autotrackMemoize<F extends (...args: any[]) => any>(func: F) {
1111
// we reference arguments instead of spreading them for performance reasons
1212

13-
// console.log('Creating autotrack memoizer node')
1413
const node: Node<Record<string, unknown>> = createNode(
1514
[] as unknown as Record<string, unknown>
1615
)
@@ -19,34 +18,16 @@ export function autotrackMemoize<F extends (...args: any[]) => any>(func: F) {
1918

2019
const shallowEqual = createCacheKeyComparator(defaultEqualityCheck)
2120

22-
// console.log('Creating cache')
2321
const cache = createCache(() => {
24-
// console.log('Executing cache: ', node.value)
2522
const res = func.apply(null, node.proxy as unknown as any[])
26-
// console.log('Res: ', res)
2723
return res
2824
})
2925

30-
// console.log('Creating memoized function')
3126
function memoized() {
32-
// console.log('Memoized running')
3327
if (!shallowEqual(lastArgs, arguments)) {
34-
// console.log(
35-
// 'Args are different: lastArgs =',
36-
// lastArgs,
37-
// 'newArgs =',
38-
// arguments
39-
// )
4028
updateNode(node, arguments as unknown as Record<string, unknown>)
4129
lastArgs = arguments
42-
} else {
43-
// console.log('Same args: ', lastArgs, arguments)
4430
}
45-
// const start = performance.now()
46-
// console.log('Calling memoized: ', arguments)
47-
48-
// const end = performance.now()
49-
// console.log('Memoized execution time: ', end - start)
5031
return cache.value
5132
}
5233

src/autotrackMemoize/autotracking.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@ export class Cell<T> {
1919
_isEqual: EqualityFn = tripleEq
2020

2121
constructor(initialValue: T, isEqual: EqualityFn = tripleEq) {
22-
// console.log('Constructing cell: ', initialValue)
2322
this._value = this._lastValue = initialValue
2423
this._isEqual = isEqual
2524
}
2625

2726
// Whenever a storage value is read, it'll add itself to the current tracker if
2827
// one exists, entangling its state with that cache.
2928
get value() {
30-
// console.log('Getting cell value: ', this._value)
3129
CURRENT_TRACKER?.add(this)
3230

3331
return this._value
@@ -39,12 +37,10 @@ export class Cell<T> {
3937
// based. We don't actively tell the caches which depend on the storage that
4038
// anything has happened. Instead, we recompute the caches when needed.
4139
set value(newValue) {
42-
// console.log('Setting value: ', this.value, newValue)
43-
// if (this.value === newValue) return
40+
if (this.value === newValue) return
4441

4542
this._value = newValue
4643
this.revision = ++$REVISION
47-
// scheduleRerender()
4844
}
4945
}
5046

@@ -138,16 +134,7 @@ export function setValue<T extends Cell<unknown>>(
138134
'setValue must be passed a tracked store created with `createStorage`.'
139135
)
140136

141-
// console.log('setValue: ', storage, value)
142-
143-
// console.log('Setting value: ', storage.value, value)
144-
// storage.value = value
145-
146-
const { _isEqual: isEqual, _lastValue: lastValue } = storage
147-
148-
// if (!isEqual(value, lastValue)) {
149137
storage.value = storage._lastValue = value
150-
// }
151138
}
152139

153140
export function createCell<T = unknown>(

src/autotrackMemoize/proxy.ts

Lines changed: 2 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
// import { DEBUG } from '@glimmer/env'
2-
3-
// import { consumeTag, createTag, dirtyTag, Tag } from '@glimmer/validator'
4-
// import { consumeTag, createTag, dirtyTag, Tag } from '../tracked-storage'
5-
import { formatMs, logLater } from './utils'
61
import {
72
consumeCollection,
83
dirtyCollection,
@@ -28,33 +23,17 @@ class ObjectTreeNode<T extends Record<string, unknown>> implements Node<T> {
2823
id = nextId++
2924

3025
constructor(public value: T) {
31-
// console.log('Object node: ', this.value)
3226
this.value = value
3327
this.tag.value = value
3428
}
3529
}
3630

3731
const objectProxyHandler = {
3832
get(node: Node, key: string | symbol): unknown {
39-
// if (DEBUG && key === REDUX_PROXY_LABEL) {
40-
// // logLater('Bailing out of getter: ', key)
41-
// return true
42-
// }
43-
// let res : unknown;
44-
45-
const keyString = key.toString()
46-
// if (keyString === 'constructor') {
47-
// console.log('Constructor: ', node)
48-
// }
49-
const start = performance.now()
50-
5133
function calculateResult() {
52-
// try {
5334
const { value } = node
5435

55-
// console.time('Reflect.get: ' + keyString)
5636
const childValue = Reflect.get(value, key)
57-
// console.timeEnd('Reflect.get: ' + keyString)
5837

5938
if (typeof key === 'symbol') {
6039
return childValue
@@ -65,55 +44,31 @@ const objectProxyHandler = {
6544
}
6645

6746
if (typeof childValue === 'object' && childValue !== null) {
68-
// logLater('Getting child node: ', key, childValue)
6947
let childNode = node.children[key]
7048

7149
if (childNode === undefined) {
72-
// console.time('Creating child node')
73-
74-
// console.log('Creating node: ', key, childValue)
7550
childNode = node.children[key] = createNode(childValue)
76-
// console.timeEnd('Creating child node')
7751
}
7852

7953
if (childNode.tag) {
80-
// logLater('Consuming tag: ', childNode)
81-
// console.time('Consuming tag A: ' + keyString)
82-
// console.log('Consuming tag: ', keyString)
8354
consumeTag(childNode.tag)
84-
// console.timeEnd('Consuming tag A: ' + keyString)
8555
}
8656

8757
return childNode.proxy
8858
} else {
8959
let tag = node.tags[key]
90-
if (key === 'constructor') {
91-
// console.log('Constructor tag: ', tag)
92-
}
9360

9461
if (tag === undefined) {
95-
// console.time('Creating tag: ' + key)
96-
// console.log('Creating tag: ', key)
9762
tag = node.tags[key] = createTag()
98-
// console.timeEnd('Creating tag: ' + key)
99-
// console.time('Assigning tag value: ' + keyString)
10063
tag.value = childValue
101-
// console.timeEnd('Assigning tag value: ' + keyString)
10264
}
10365

104-
// console.time('Consuming tag B: ' + keyString)
105-
// console.log('Consuming tag: ', keyString, tag)
10666
consumeTag(tag)
10767

108-
// console.timeEnd('Consuming tag B: ' + keyString)
109-
11068
return childValue
11169
}
11270
}
11371
const res = calculateResult()
114-
115-
const end = performance.now()
116-
// logLater(`Proxy get trap: ${keyString}: ${formatMs(end - start)}`)
11772
return res
11873
},
11974

@@ -126,7 +81,6 @@ const objectProxyHandler = {
12681
node: Node,
12782
prop: string | symbol
12883
): PropertyDescriptor | undefined {
129-
console.log('getOwnPropertyDescriptor', prop)
13084
return Reflect.getOwnPropertyDescriptor(node.value, prop)
13185
},
13286

@@ -144,7 +98,6 @@ class ArrayTreeNode<T extends Array<unknown>> implements Node<T> {
14498
id = nextId++
14599

146100
constructor(public value: T) {
147-
// console.log('Array node: ', value)
148101
this.value = value
149102
this.tag.value = value
150103
}
@@ -194,14 +147,10 @@ export function updateNode<T extends Array<unknown> | Record<string, unknown>>(
194147
node: Node<T>,
195148
newValue: T
196149
): void {
197-
// console.log('UpdateNode: ', newValue)
198150
const { value, tags, children } = node
199151

200152
node.value = newValue
201153

202-
const start = performance.now()
203-
204-
// console.time('updateNode: array check: ' + node.id)
205154
if (
206155
Array.isArray(value) &&
207156
Array.isArray(newValue) &&
@@ -214,8 +163,6 @@ export function updateNode<T extends Array<unknown> | Record<string, unknown>>(
214163
let newKeysSize = 0
215164
let anyKeysAdded = false
216165

217-
// console.log('Key check: ', value, newValue)
218-
219166
for (const _key in value) {
220167
oldKeysSize++
221168
}
@@ -228,82 +175,19 @@ export function updateNode<T extends Array<unknown> | Record<string, unknown>>(
228175
}
229176
}
230177

231-
// let oldKeys = keysMap.get(value)
232-
// if (!oldKeys) {
233-
// oldKeys = new Set<string>()
234-
// for (let key in value) {
235-
// oldKeys.add(key)
236-
// }
237-
// keysMap.set(value, oldKeys)
238-
// }
239-
// oldKeyIteration = performance.now()
240-
// let newKeys = keysMap.get(newValue)
241-
// if (!newKeys) {
242-
// newKeys = new Set<string>()
243-
// for (let key in newValue) {
244-
// newKeys.add(key)
245-
// }
246-
// keysMap.set(newValue, newKeys)
247-
// }
248-
// newKeyIteration = performance.now()
249-
// // const oldKeys = Object.keys(value)
250-
// // const newKeys = Object.keys(newValue)
251-
// const isDifferent =
252-
// oldKeys.size !== newKeys.size || anyKeysDifferent(oldKeys, newKeys)
253-
254178
const isDifferent = anyKeysAdded || oldKeysSize !== newKeysSize
255179

256-
if (
257-
isDifferent
258-
// [...oldKeys].some((k) => !newKeys!.has(k))
259-
) {
260-
// console.log('Dirtying collection: ', node)
180+
if (isDifferent) {
261181
dirtyCollection(node)
262182
}
263183
}
264-
// console.time('Checking object keys')
265-
// let oldKeys = keysMap.get(value)
266-
// if (!oldKeys) {
267-
// oldKeys = new Set<string>()
268-
// for (const key in value) {
269-
// oldKeys.add(key)
270-
// }
271-
// keysMap.set(value, oldKeys)
272-
// }
273-
// let newKeys = keysMap.get(value)
274-
// if (!newKeys) {
275-
// newKeys = new Set<string>()
276-
// for (const key in newValue) {
277-
// newKeys.add(key)
278-
// }
279-
// keysMap.set(newValue, newKeys)
280-
// }
281-
// // const oldKeys = Object.keys(value)
282-
// // const newKeys = Object.keys(newValue)
283-
284-
// if (
285-
// oldKeys.size !== newKeys.size ||
286-
// [...oldKeys].some(k => !newKeys!.has(k))
287-
// ) {
288-
// dirtyCollection(node)
289-
// }
290-
// console.timeEnd('Checking object keys')
291184
}
292185

293-
const arrayDone = performance.now()
294-
295-
// console.timeEnd('updateNode: array check: ' + node.id)
296-
297-
// console.time('updateNode: tags check: ' + node.id)
298-
299-
// console.log('Tags: ', tags)
300186
for (const key in tags) {
301-
// logLater('Tag key: ', key)
302187
const childValue = (value as Record<string, unknown>)[key]
303188
const newChildValue = (newValue as Record<string, unknown>)[key]
304189

305190
if (childValue !== newChildValue) {
306-
// console.log('Dirtying tag: ', { key, childValue, newChildValue })
307191
dirtyCollection(node)
308192
dirtyTag(tags[key], newChildValue)
309193
}
@@ -313,51 +197,21 @@ export function updateNode<T extends Array<unknown> | Record<string, unknown>>(
313197
}
314198
}
315199

316-
const tagsDone = performance.now()
317-
318-
// console.timeEnd('updateNode: tags check: ' + node.id)
319-
320-
// console.time('updateNode: keys check: ' + node.id)
321-
322200
for (const key in children) {
323-
// logLater('Child key: ', key)
324201
const childNode = children[key]
325202
const newChildValue = (newValue as Record<string, unknown>)[key]
326203

327204
const childValue = childNode.value
328205

329206
if (childValue === newChildValue) {
330-
// logLater('Skipping child node: ', key, childValue, newChildValue)
331207
continue
332-
} else if (
333-
typeof newChildValue === 'object' &&
334-
newChildValue !== null // &&
335-
// Object.getPrototypeOf(newChildValue) === Object.getPrototypeOf(childValue)
336-
) {
337-
// logLater('Updating child node: ', key, childValue, newChildValue)
338-
// console.time('Nested updateNode: ' + key)
208+
} else if (typeof newChildValue === 'object' && newChildValue !== null) {
339209
updateNode(childNode, newChildValue as Record<string, unknown>)
340-
// console.timeEnd('Nested updateNode: ' + key)
341210
} else {
342211
deleteNode(childNode)
343212
delete children[key]
344213
}
345214
}
346-
347-
const keysDone = performance.now()
348-
349-
// logLater(
350-
// 'updateNode: ',
351-
// {
352-
// total: formatMs(keysDone - start),
353-
// array: formatMs(arrayDone - start),
354-
// tags: formatMs(tagsDone - arrayDone),
355-
// keys: formatMs(keysDone - tagsDone)
356-
// },
357-
// node.value
358-
// )
359-
360-
// console.timeEnd('updateNode: keys check: ' + node.id)
361215
}
362216

363217
function deleteNode(node: Node): void {
@@ -371,6 +225,4 @@ function deleteNode(node: Node): void {
371225
for (const key in node.children) {
372226
deleteNode(node.children[key])
373227
}
374-
// Object.values(node.tags).map(dirtyTag)
375-
// Object.values(node.children).map(deleteNode)
376228
}

0 commit comments

Comments
 (0)