From 3faa8410c31d8f48b31245545a11a7daafa02959 Mon Sep 17 00:00:00 2001 From: daishi Date: Wed, 16 Oct 2024 07:22:07 +0900 Subject: [PATCH 1/2] avoid maybeProxify --- src/vanilla/utils/proxyMap.ts | 36 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/vanilla/utils/proxyMap.ts b/src/vanilla/utils/proxyMap.ts index ac4c58c6..48cb3d98 100644 --- a/src/vanilla/utils/proxyMap.ts +++ b/src/vanilla/utils/proxyMap.ts @@ -1,7 +1,6 @@ import { proxy, unstable_getInternalStates } from '../../vanilla.ts' const { proxyStateMap, snapCache } = unstable_getInternalStates() -const maybeProxify = (x: any) => (typeof x === 'object' ? proxy({ x }).x : x) const isProxy = (x: any) => proxyStateMap.has(x) type InternalProxyObject = Map & { @@ -32,9 +31,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { 'proxyMap:\n\tinitial state must be iterable\n\t\ttip: structure should be [[key, value]]', ) } - for (const [k, v] of entries) { - const key = maybeProxify(k) - const value = maybeProxify(v) + for (const [key, value] of entries) { indexMap.set(key, initialIndex) initialData[initialIndex++] = key initialData[initialIndex++] = value @@ -53,8 +50,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { }, get(key: K) { const map = getMapForThis(this) - const k = maybeProxify(key) - const index = map.get(k) + const index = map.get(key) if (index === undefined) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions this.index @@ -64,8 +60,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { }, has(key: K) { const map = getMapForThis(this) - const k = maybeProxify(key) - const exists = map.has(k) + const exists = map.has(key) if (!exists && !isProxy(this)) { // eslint-disable-next-line @typescript-eslint/no-unused-expressions this.index @@ -76,16 +71,14 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { if (!isProxy(this)) { throw new Error('Cannot perform mutations on a snapshot') } - const k = maybeProxify(key) - const v = maybeProxify(value) - const index = indexMap.get(k) + const index = indexMap.get(key) if (index !== undefined) { - this.data[index + 1] = v + this.data[index + 1] = value } else { let nextIndex = this.index - indexMap.set(k, nextIndex) - this.data[nextIndex++] = k - this.data[nextIndex++] = v + indexMap.set(key, nextIndex) + this.data[nextIndex++] = key + this.data[nextIndex++] = value this.index = nextIndex } return this @@ -94,14 +87,13 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { if (!isProxy(this)) { throw new Error('Cannot perform mutations on a snapshot') } - const k = maybeProxify(key) - const index = indexMap.get(k) + const index = indexMap.get(key) if (index === undefined) { return false } delete this.data[index] delete this.data[index + 1] - indexMap.delete(k) + indexMap.delete(key) return true }, clear() { @@ -114,14 +106,14 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { }, forEach(cb: (value: V, key: K, map: Map) => void) { const map = getMapForThis(this) - map.forEach((index) => { - cb(this.data[index + 1] as V, this.data[index] as K, this) + map.forEach((index, key) => { + cb(this.data[index + 1] as V, key, this) }) }, *entries(): MapIterator<[K, V]> { const map = getMapForThis(this) - for (const index of map.values()) { - yield [this.data[index], this.data[index + 1]] as [K, V] + for (const [key, index] of map) { + yield [key, this.data[index + 1]] as [K, V] } }, *keys(): IterableIterator { From 016f5903ed2116e5e334ec9ffacfbb3a9e8a7ea4 Mon Sep 17 00:00:00 2001 From: daishi Date: Wed, 16 Oct 2024 07:32:27 +0900 Subject: [PATCH 2/2] no need to add key in this data --- src/vanilla/utils/proxyMap.ts | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/vanilla/utils/proxyMap.ts b/src/vanilla/utils/proxyMap.ts index 48cb3d98..fb7eec2b 100644 --- a/src/vanilla/utils/proxyMap.ts +++ b/src/vanilla/utils/proxyMap.ts @@ -4,13 +4,13 @@ const { proxyStateMap, snapCache } = unstable_getInternalStates() const isProxy = (x: any) => proxyStateMap.has(x) type InternalProxyObject = Map & { - data: Array + data: Array index: number toJSON: () => Map } export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { - const initialData: Array = [] + const initialData: Array = [] let initialIndex = 0 const indexMap = new Map() @@ -33,7 +33,6 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { } for (const [key, value] of entries) { indexMap.set(key, initialIndex) - initialData[initialIndex++] = key initialData[initialIndex++] = value } } @@ -56,7 +55,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { this.index return undefined } - return this.data[index + 1] as V + return this.data[index] }, has(key: K) { const map = getMapForThis(this) @@ -73,13 +72,10 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { } const index = indexMap.get(key) if (index !== undefined) { - this.data[index + 1] = value + this.data[index] = value } else { - let nextIndex = this.index - indexMap.set(key, nextIndex) - this.data[nextIndex++] = key - this.data[nextIndex++] = value - this.index = nextIndex + indexMap.set(key, this.index) + this.data[this.index++] = value } return this }, @@ -92,7 +88,6 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { return false } delete this.data[index] - delete this.data[index + 1] indexMap.delete(key) return true }, @@ -107,13 +102,13 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { forEach(cb: (value: V, key: K, map: Map) => void) { const map = getMapForThis(this) map.forEach((index, key) => { - cb(this.data[index + 1] as V, key, this) + cb(this.data[index]!, key, this) }) }, *entries(): MapIterator<[K, V]> { const map = getMapForThis(this) for (const [key, index] of map) { - yield [key, this.data[index + 1]] as [K, V] + yield [key, this.data[index]!] } }, *keys(): IterableIterator { @@ -125,7 +120,7 @@ export function proxyMap(entries?: Iterable<[K, V]> | undefined | null) { *values(): IterableIterator { const map = getMapForThis(this) for (const index of map.values()) { - yield this.data[index + 1] as V + yield this.data[index]! } }, [Symbol.iterator]() {