Skip to content

Commit

Permalink
micro optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
xaviergonz committed Mar 24, 2024
1 parent a486c9a commit d98c09d
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 73 deletions.
18 changes: 6 additions & 12 deletions packages/lib/src/parent/setParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,13 @@ import { ParentPath, fastGetParentPath, fastGetRoot } from "./path"
*/
export const setParent = action(
"setParent",
({
value,
parentPath,
indexChangeAllowed,
isDataObject,
cloneIfApplicable,
}: {
value: any
parentPath: ParentPath<any> | undefined
indexChangeAllowed: boolean
isDataObject: boolean
(
value: any,
parentPath: ParentPath<any> | undefined,
indexChangeAllowed: boolean,
isDataObject: boolean,
cloneIfApplicable: boolean
}): any => {
): any => {
if (isPrimitive(value)) {
return value
}
Expand Down
24 changes: 12 additions & 12 deletions packages/lib/src/tweaker/tweak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ function internalTweak<T>(value: T, parentPath: ParentPath<any> | undefined): T

// already tweaked
if (isTweakedObject(value, true)) {
value = setParent({
value = setParent(
value,
parentPath,
indexChangeAllowed: false,
isDataObject: false,
cloneIfApplicable: true,
})
false, // indexChangeAllowed
false, // isDataObject
true // cloneIfApplicable
)
return value
}

Expand Down Expand Up @@ -162,14 +162,14 @@ export function tryUntweak(value: any): (() => void) | undefined {
const children = Array.from(getObjectChildren(value)!.values())

for (let i = 0; i < children.length; i++) {
setParent({
value: children[i],
parentPath: undefined,
indexChangeAllowed: false,
isDataObject: false,
setParent(
children[i], // value
undefined, // parentPath
false, // indexChangeAllowed
false, // isDataObject
// no need to clone if unsetting the parent
cloneIfApplicable: false,
})
false // cloneIfApplicable
)
}

return () => {
Expand Down
42 changes: 21 additions & 21 deletions packages/lib/src/tweaker/tweakArray.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ export function tweakArray<T extends any[]>(
}

tweakedObjects.set(tweakedArr, untweak)
setParent({
value: tweakedArr,
setParent(
tweakedArr, // value
parentPath,
indexChangeAllowed: false,
isDataObject: false,
false, // indexChangeAllowed
false, // isDataObject
// arrays shouldn't be cloned anyway
cloneIfApplicable: false,
})
false // cloneIfApplicable
)

const untransformedSn: any[] = []
untransformedSn.length = arrLn
Expand All @@ -82,14 +82,14 @@ export function tweakArray<T extends any[]>(
let tweakedValue
if (doNotTweakChildren) {
tweakedValue = v
setParent({
value: tweakedValue,
parentPath: path,
indexChangeAllowed: false,
isDataObject: false,
setParent(
tweakedValue, // value
path, // parentPath
false, // indexChangeAllowed
false, // isDataObject
// the value is already a new value (the result of a fromSnapshot)
cloneIfApplicable: false,
})
false // cloneIfApplicable
)
} else {
tweakedValue = tweak(v, path)
setIfDifferent(tweakedArr, i, tweakedValue)
Expand Down Expand Up @@ -394,17 +394,17 @@ function interceptArrayMutationSplice(change: IArrayWillSplice) {

if (oldNextIndex !== newNextIndex) {
for (let i = oldNextIndex, j = newNextIndex; i < change.object.length; i++, j++) {
setParent({
value: change.object[i],
parentPath: {
setParent(
change.object[i], // value
{
parent: change.object,
path: j,
},
indexChangeAllowed: true,
isDataObject: false,
}, // parentPath
true, // indexChangeAllowed
false, // isDataObject
// just re-indexing
cloneIfApplicable: false,
})
false // cloneIfApplicable
)
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions packages/lib/src/tweaker/tweakFrozen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export function tweakFrozen<T extends Frozen<any>>(
parentPath: ParentPath<any> | undefined
): T {
tweakedObjects.set(frozenObj, undefined)
setParent({
value: frozenObj,
setParent(
frozenObj, // value
parentPath,
indexChangeAllowed: false,
isDataObject: false,
false, // indexChangeAllowed
false, // isDataObject
// a frozen is not a value-type
cloneIfApplicable: false,
})
false // cloneIfApplicable
)

// we DON'T want data proxified, but the snapshot is the data itself
setNewInternalSnapshot(frozenObj, { [frozenKey]: true, data: frozenObj.data }, undefined, true)
Expand Down
10 changes: 5 additions & 5 deletions packages/lib/src/tweaker/tweakModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import { TweakerPriority } from "./TweakerPriority"
*/
export function tweakModel<T extends object>(value: T, parentPath: ParentPath<any> | undefined): T {
tweakedObjects.set(value, undefined)
setParent({
setParent(
value,
parentPath,
indexChangeAllowed: false,
isDataObject: false,
cloneIfApplicable: true,
})
false, // indexChangeAllowed
false, // isDataObject
true // cloneIfApplicable
)

// nothing to do for models, data is already proxified and its parent is set
// for snapshots we will use its "$" object snapshot directly
Expand Down
29 changes: 14 additions & 15 deletions packages/lib/src/tweaker/tweakPlainObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@ export function tweakPlainObject<T extends Record<string, any>>(
}

tweakedObjects.set(tweakedObj, untweak)
setParent({
value: tweakedObj,
setParent(
tweakedObj, // value
parentPath,
indexChangeAllowed: false,
false, // indexChangeAllowed
isDataObject,
// an object shouldn't be cloned
cloneIfApplicable: false,
})
false // cloneIfApplicable
)

const untransformedSn: any = {}

Expand All @@ -84,14 +84,14 @@ export function tweakPlainObject<T extends Record<string, any>>(
let tweakedValue
if (doNotTweakChildren) {
tweakedValue = v
setParent({
value: tweakedValue,
parentPath: path,
indexChangeAllowed: false,
isDataObject: false,
setParent(
tweakedValue, // value
path, // parentPath
false, // indexChangeAllowed
false, // isDataObject
// the value is already a new value (the result of a fromSnapshot)
cloneIfApplicable: false,
})
false // cloneIfApplicable
)
} else {
tweakedValue = tweak(v, path)
setIfDifferent(tweakedObj, k, tweakedValue)
Expand Down Expand Up @@ -273,8 +273,7 @@ function interceptObjectMutation(change: IObjectWillChange) {
break

case "remove": {
const oldVal = change.object[change.name]
tweak(oldVal, undefined)
tweak(change.object[change.name], undefined)
break
}

Expand All @@ -284,7 +283,7 @@ function interceptObjectMutation(change: IObjectWillChange) {
if (newVal !== oldVal) {
tweak(oldVal, undefined)

change.newValue = tweak(change.newValue, {
change.newValue = tweak(newVal, {
parent: change.object,
path: "" + change.name,
})
Expand Down
6 changes: 4 additions & 2 deletions packages/lib/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ export function isPrimitive(value: unknown): value is PrimitiveValue {
case "undefined":
case "bigint":
return true
default:
return value === null
}
return value === null
}

/**
Expand All @@ -110,8 +111,9 @@ export function isJSONPrimitive(value: unknown): value is JSONPrimitiveValue {
case "string":
case "boolean":
return true
default:
return value === null
}
return value === null
}

/**
Expand Down

0 comments on commit d98c09d

Please sign in to comment.