diff --git a/src/autotrackMemoize/autotrackMemoize.ts b/src/autotrackMemoize/autotrackMemoize.ts index c7cae55cf..e065b31e9 100644 --- a/src/autotrackMemoize/autotrackMemoize.ts +++ b/src/autotrackMemoize/autotrackMemoize.ts @@ -10,7 +10,6 @@ import { export function autotrackMemoize any>(func: F) { // we reference arguments instead of spreading them for performance reasons - // console.log('Creating autotrack memoizer node') const node: Node> = createNode( [] as unknown as Record ) @@ -19,34 +18,16 @@ export function autotrackMemoize any>(func: F) { const shallowEqual = createCacheKeyComparator(defaultEqualityCheck) - // console.log('Creating cache') const cache = createCache(() => { - // console.log('Executing cache: ', node.value) const res = func.apply(null, node.proxy as unknown as any[]) - // console.log('Res: ', res) return res }) - // console.log('Creating memoized function') function memoized() { - // console.log('Memoized running') if (!shallowEqual(lastArgs, arguments)) { - // console.log( - // 'Args are different: lastArgs =', - // lastArgs, - // 'newArgs =', - // arguments - // ) updateNode(node, arguments as unknown as Record) lastArgs = arguments - } else { - // console.log('Same args: ', lastArgs, arguments) } - // const start = performance.now() - // console.log('Calling memoized: ', arguments) - - // const end = performance.now() - // console.log('Memoized execution time: ', end - start) return cache.value } diff --git a/src/autotrackMemoize/autotracking.ts b/src/autotrackMemoize/autotracking.ts index 5d19b8185..aec0ca0bb 100644 --- a/src/autotrackMemoize/autotracking.ts +++ b/src/autotrackMemoize/autotracking.ts @@ -19,7 +19,6 @@ export class Cell { _isEqual: EqualityFn = tripleEq constructor(initialValue: T, isEqual: EqualityFn = tripleEq) { - // console.log('Constructing cell: ', initialValue) this._value = this._lastValue = initialValue this._isEqual = isEqual } @@ -27,7 +26,6 @@ export class Cell { // Whenever a storage value is read, it'll add itself to the current tracker if // one exists, entangling its state with that cache. get value() { - // console.log('Getting cell value: ', this._value) CURRENT_TRACKER?.add(this) return this._value @@ -39,12 +37,10 @@ export class Cell { // based. We don't actively tell the caches which depend on the storage that // anything has happened. Instead, we recompute the caches when needed. set value(newValue) { - // console.log('Setting value: ', this.value, newValue) - // if (this.value === newValue) return + if (this.value === newValue) return this._value = newValue this.revision = ++$REVISION - // scheduleRerender() } } @@ -138,16 +134,7 @@ export function setValue>( 'setValue must be passed a tracked store created with `createStorage`.' ) - // console.log('setValue: ', storage, value) - - // console.log('Setting value: ', storage.value, value) - // storage.value = value - - const { _isEqual: isEqual, _lastValue: lastValue } = storage - - // if (!isEqual(value, lastValue)) { storage.value = storage._lastValue = value - // } } export function createCell( diff --git a/src/autotrackMemoize/proxy.ts b/src/autotrackMemoize/proxy.ts index c4aed98f3..c18c7f6e1 100644 --- a/src/autotrackMemoize/proxy.ts +++ b/src/autotrackMemoize/proxy.ts @@ -1,8 +1,3 @@ -// import { DEBUG } from '@glimmer/env' - -// import { consumeTag, createTag, dirtyTag, Tag } from '@glimmer/validator' -// import { consumeTag, createTag, dirtyTag, Tag } from '../tracked-storage' -import { formatMs, logLater } from './utils' import { consumeCollection, dirtyCollection, @@ -28,7 +23,6 @@ class ObjectTreeNode> implements Node { id = nextId++ constructor(public value: T) { - // console.log('Object node: ', this.value) this.value = value this.tag.value = value } @@ -36,25 +30,10 @@ class ObjectTreeNode> implements Node { const objectProxyHandler = { get(node: Node, key: string | symbol): unknown { - // if (DEBUG && key === REDUX_PROXY_LABEL) { - // // logLater('Bailing out of getter: ', key) - // return true - // } - // let res : unknown; - - const keyString = key.toString() - // if (keyString === 'constructor') { - // console.log('Constructor: ', node) - // } - const start = performance.now() - function calculateResult() { - // try { const { value } = node - // console.time('Reflect.get: ' + keyString) const childValue = Reflect.get(value, key) - // console.timeEnd('Reflect.get: ' + keyString) if (typeof key === 'symbol') { return childValue @@ -65,55 +44,31 @@ const objectProxyHandler = { } if (typeof childValue === 'object' && childValue !== null) { - // logLater('Getting child node: ', key, childValue) let childNode = node.children[key] if (childNode === undefined) { - // console.time('Creating child node') - - // console.log('Creating node: ', key, childValue) childNode = node.children[key] = createNode(childValue) - // console.timeEnd('Creating child node') } if (childNode.tag) { - // logLater('Consuming tag: ', childNode) - // console.time('Consuming tag A: ' + keyString) - // console.log('Consuming tag: ', keyString) consumeTag(childNode.tag) - // console.timeEnd('Consuming tag A: ' + keyString) } return childNode.proxy } else { let tag = node.tags[key] - if (key === 'constructor') { - // console.log('Constructor tag: ', tag) - } if (tag === undefined) { - // console.time('Creating tag: ' + key) - // console.log('Creating tag: ', key) tag = node.tags[key] = createTag() - // console.timeEnd('Creating tag: ' + key) - // console.time('Assigning tag value: ' + keyString) tag.value = childValue - // console.timeEnd('Assigning tag value: ' + keyString) } - // console.time('Consuming tag B: ' + keyString) - // console.log('Consuming tag: ', keyString, tag) consumeTag(tag) - // console.timeEnd('Consuming tag B: ' + keyString) - return childValue } } const res = calculateResult() - - const end = performance.now() - // logLater(`Proxy get trap: ${keyString}: ${formatMs(end - start)}`) return res }, @@ -126,7 +81,6 @@ const objectProxyHandler = { node: Node, prop: string | symbol ): PropertyDescriptor | undefined { - console.log('getOwnPropertyDescriptor', prop) return Reflect.getOwnPropertyDescriptor(node.value, prop) }, @@ -144,7 +98,6 @@ class ArrayTreeNode> implements Node { id = nextId++ constructor(public value: T) { - // console.log('Array node: ', value) this.value = value this.tag.value = value } @@ -194,14 +147,10 @@ export function updateNode | Record>( node: Node, newValue: T ): void { - // console.log('UpdateNode: ', newValue) const { value, tags, children } = node node.value = newValue - const start = performance.now() - - // console.time('updateNode: array check: ' + node.id) if ( Array.isArray(value) && Array.isArray(newValue) && @@ -214,8 +163,6 @@ export function updateNode | Record>( let newKeysSize = 0 let anyKeysAdded = false - // console.log('Key check: ', value, newValue) - for (const _key in value) { oldKeysSize++ } @@ -228,82 +175,19 @@ export function updateNode | Record>( } } - // let oldKeys = keysMap.get(value) - // if (!oldKeys) { - // oldKeys = new Set() - // for (let key in value) { - // oldKeys.add(key) - // } - // keysMap.set(value, oldKeys) - // } - // oldKeyIteration = performance.now() - // let newKeys = keysMap.get(newValue) - // if (!newKeys) { - // newKeys = new Set() - // for (let key in newValue) { - // newKeys.add(key) - // } - // keysMap.set(newValue, newKeys) - // } - // newKeyIteration = performance.now() - // // const oldKeys = Object.keys(value) - // // const newKeys = Object.keys(newValue) - // const isDifferent = - // oldKeys.size !== newKeys.size || anyKeysDifferent(oldKeys, newKeys) - const isDifferent = anyKeysAdded || oldKeysSize !== newKeysSize - if ( - isDifferent - // [...oldKeys].some((k) => !newKeys!.has(k)) - ) { - // console.log('Dirtying collection: ', node) + if (isDifferent) { dirtyCollection(node) } } - // console.time('Checking object keys') - // let oldKeys = keysMap.get(value) - // if (!oldKeys) { - // oldKeys = new Set() - // for (const key in value) { - // oldKeys.add(key) - // } - // keysMap.set(value, oldKeys) - // } - // let newKeys = keysMap.get(value) - // if (!newKeys) { - // newKeys = new Set() - // for (const key in newValue) { - // newKeys.add(key) - // } - // keysMap.set(newValue, newKeys) - // } - // // const oldKeys = Object.keys(value) - // // const newKeys = Object.keys(newValue) - - // if ( - // oldKeys.size !== newKeys.size || - // [...oldKeys].some(k => !newKeys!.has(k)) - // ) { - // dirtyCollection(node) - // } - // console.timeEnd('Checking object keys') } - const arrayDone = performance.now() - - // console.timeEnd('updateNode: array check: ' + node.id) - - // console.time('updateNode: tags check: ' + node.id) - - // console.log('Tags: ', tags) for (const key in tags) { - // logLater('Tag key: ', key) const childValue = (value as Record)[key] const newChildValue = (newValue as Record)[key] if (childValue !== newChildValue) { - // console.log('Dirtying tag: ', { key, childValue, newChildValue }) dirtyCollection(node) dirtyTag(tags[key], newChildValue) } @@ -313,51 +197,21 @@ export function updateNode | Record>( } } - const tagsDone = performance.now() - - // console.timeEnd('updateNode: tags check: ' + node.id) - - // console.time('updateNode: keys check: ' + node.id) - for (const key in children) { - // logLater('Child key: ', key) const childNode = children[key] const newChildValue = (newValue as Record)[key] const childValue = childNode.value if (childValue === newChildValue) { - // logLater('Skipping child node: ', key, childValue, newChildValue) continue - } else if ( - typeof newChildValue === 'object' && - newChildValue !== null // && - // Object.getPrototypeOf(newChildValue) === Object.getPrototypeOf(childValue) - ) { - // logLater('Updating child node: ', key, childValue, newChildValue) - // console.time('Nested updateNode: ' + key) + } else if (typeof newChildValue === 'object' && newChildValue !== null) { updateNode(childNode, newChildValue as Record) - // console.timeEnd('Nested updateNode: ' + key) } else { deleteNode(childNode) delete children[key] } } - - const keysDone = performance.now() - - // logLater( - // 'updateNode: ', - // { - // total: formatMs(keysDone - start), - // array: formatMs(arrayDone - start), - // tags: formatMs(tagsDone - arrayDone), - // keys: formatMs(keysDone - tagsDone) - // }, - // node.value - // ) - - // console.timeEnd('updateNode: keys check: ' + node.id) } function deleteNode(node: Node): void { @@ -371,6 +225,4 @@ function deleteNode(node: Node): void { for (const key in node.children) { deleteNode(node.children[key]) } - // Object.values(node.tags).map(dirtyTag) - // Object.values(node.children).map(deleteNode) } diff --git a/src/autotrackMemoize/tracking.ts b/src/autotrackMemoize/tracking.ts index 1d12ab817..3d70303d0 100644 --- a/src/autotrackMemoize/tracking.ts +++ b/src/autotrackMemoize/tracking.ts @@ -1,9 +1,3 @@ -// import { -// createStorage, -// getValue as consumeTag, -// setValue, -// } from '../tracked-storage' - import { createCell as createStorage, getValue as consumeTag, @@ -11,10 +5,6 @@ import { Cell } from './autotracking' -// import { consumeTag, createTag, dirtyTag, Tag } from '@glimmer/validator' - -// export { consumeTag, createTag, dirtyTag, Tag } from '@glimmer/validator' - export type Tag = Cell const neverEq = (a: any, b: any): boolean => false @@ -27,8 +17,6 @@ export function dirtyTag(tag: Tag, value: any): void { setValue(tag, value) } -//////////// - export interface Node< T extends Array | Record = | Array diff --git a/src/autotrackMemoize/utils.ts b/src/autotrackMemoize/utils.ts index bdcd1c6db..cef655a08 100644 --- a/src/autotrackMemoize/utils.ts +++ b/src/autotrackMemoize/utils.ts @@ -7,13 +7,3 @@ export function assert( throw new Error(msg) } } - -export function formatMs(n: number) { - return n.toFixed(4) + 'ms' -} - -export const loggedValues: any[] = [] - -export const logLater: typeof console.log = (...args: any[]) => { - loggedValues.push([new Date(), ...args]) -}