Skip to content

Commit

Permalink
Use single flag field for matched and insert (+28 B)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewiggins committed Oct 30, 2023
1 parent bd51a73 commit fd710b8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
3 changes: 1 addition & 2 deletions jsx-runtime/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ function createVNode(type, props, key, isStaticChildren, __source, __self) {
constructor: undefined,
_original: --vnodeId,
_index: -1,
_insert: false,
_matched: false,
_flags: 0,
__source,
__self
};
Expand Down
8 changes: 8 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
/** Indicates that this node needs to be inserted while patching children */
export const INSERT_VNODE = 1 << 16;
/** Indicates a VNode has been matched with another VNode in the diff */
export const MATCHED = 1 << 17;

/** Reset all mode flags */
export const RESET_MODE = ~(INSERT_VNODE | MATCHED);

export const EMPTY_OBJ = /** @type {any} */ ({});
export const EMPTY_ARR = [];
export const IS_NON_DIMENSIONAL =
Expand Down
3 changes: 1 addition & 2 deletions src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ export function createVNode(type, props, key, ref, original) {
constructor: undefined,
_original: original == null ? ++vnodeId : original,
_index: -1,
_insert: false,
_matched: false
_flags: 0
};

// Only invoke the vnode hook if this was *not* a direct copy:
Expand Down
30 changes: 20 additions & 10 deletions src/diff/children.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { diff, unmount, applyRef } from './index';
import { createVNode, Fragment } from '../create-element';
import { EMPTY_OBJ, EMPTY_ARR } from '../constants';
import {
EMPTY_OBJ,
EMPTY_ARR,
INSERT_VNODE,
MATCHED,
RESET_MODE
} from '../constants';
import { isArray } from '../util';
import { getDomSibling } from '../component';

Expand Down Expand Up @@ -77,7 +83,7 @@ export function diffChildren(
// unmounted.
for (i = 0; i < oldChildrenLength; i++) {
oldVNode = oldChildren[i];
if (oldVNode != null && !oldVNode._matched) {
if (oldVNode != null && (oldVNode._flags & MATCHED) === 0) {
if (oldDom == oldVNode._dom) {
oldDom = getDomSibling(oldVNode);

Expand Down Expand Up @@ -147,7 +153,10 @@ export function diffChildren(
}

if (typeof childVNode.type == 'function') {
if (childVNode._insert || oldVNode._children === childVNode._children) {
if (
childVNode._flags & INSERT_VNODE ||
oldVNode._children === childVNode._children
) {
oldDom = reorderChildren(childVNode, oldDom, parentDom);
} else if (childVNode._nextDom !== undefined) {
// Only Fragments or components that return Fragment like VNodes will
Expand All @@ -164,7 +173,7 @@ export function diffChildren(
// can clean up the property
childVNode._nextDom = undefined;
} else if (newDom) {
if (childVNode._insert) {
if (childVNode._flags & INSERT_VNODE) {
oldDom = placeChild(parentDom, newDom, oldDom);
} else {
oldDom = newDom.nextSibling;
Expand All @@ -185,7 +194,7 @@ export function diffChildren(
}

// Unset diffing flags
childVNode._insert = false;
childVNode._flags &= RESET_MODE;
}

newParentVNode._dom = firstChildDom;
Expand Down Expand Up @@ -322,7 +331,7 @@ function constructNewChildrenArray(
// property? We need it now so we can pull off the matchingIndex in
// diffChildren and when unmounting we can get the next DOM element by
// calling getDomSibling, which needs a complete oldTree
oldChildren[matchingIndex]._matched = true;
oldChildren[matchingIndex]._flags |= MATCHED;
}
}

Expand Down Expand Up @@ -365,7 +374,7 @@ function constructNewChildrenArray(
matchingIndex !== i + skew ||
(typeof childVNode.type != 'function' && isMounting)
) {
childVNode._insert = true;
childVNode._flags |= INSERT_VNODE;
}
}

Expand Down Expand Up @@ -464,7 +473,8 @@ function findMatchingIndex(
// if the oldVNode was null or matched, then there could needs to be at least
// 1 (aka `remainingOldChildren > 0`) children to find and compare against.
let shouldSearch =
remainingOldChildren > (oldVNode != null && !oldVNode._matched ? 1 : 0);
remainingOldChildren >
(oldVNode != null && (oldVNode._flags & MATCHED) === 0 ? 1 : 0);

if (
oldVNode === null ||
Expand All @@ -477,7 +487,7 @@ function findMatchingIndex(
oldVNode = oldChildren[x];
if (
oldVNode &&
!oldVNode._matched &&
(oldVNode._flags & MATCHED) === 0 &&
key == oldVNode.key &&
type === oldVNode.type
) {
Expand All @@ -490,7 +500,7 @@ function findMatchingIndex(
oldVNode = oldChildren[y];
if (
oldVNode &&
!oldVNode._matched &&
(oldVNode._flags & MATCHED) === 0 &&
key == oldVNode.key &&
type === oldVNode.type
) {
Expand Down
3 changes: 1 addition & 2 deletions src/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ declare global {
constructor: undefined;
_original: number;
_index: number;
_insert: boolean;
_matched: boolean;
_flags: number;
}

export interface Component<P = {}, S = {}> extends preact.Component<P, S> {
Expand Down

0 comments on commit fd710b8

Please sign in to comment.