Skip to content

perf: stacks and queues #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 32 additions & 27 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type {
ReactDevToolsGlobalHook,
ReactRenderer,
} from './types.js';
import { type StackNode, createStackNode } from './utilities.js';

// https://github.com/facebook/react/blob/main/packages/react-reconciler/src/ReactWorkTags.js
export const FunctionComponentTag = 0;
Expand Down Expand Up @@ -306,18 +307,22 @@ export const didFiberCommit = (fiber: Fiber): boolean => {
*/
export const getMutatedHostFibers = (fiber: Fiber): Fiber[] => {
const mutations: Fiber[] = [];
const stack: Fiber[] = [fiber];
let head: StackNode<Fiber> | undefined = createStackNode(fiber);

while (stack.length) {
const node = stack.pop();
if (!node) continue;
while (head) {
const node: Fiber = head.value;
head = head.next;

if (isHostFiber(node) && didFiberCommit(node) && didFiberRender(node)) {
mutations.push(node);
}

if (node.child) stack.push(node.child);
if (node.sibling) stack.push(node.sibling);
if (node.child) {
head = createStackNode(node.child, head);
}
if (node.sibling) {
head = createStackNode(node.sibling, head);
}
}

return mutations;
Expand All @@ -338,11 +343,7 @@ export const getFiberStack = (fiber: Fiber): Fiber[] => {
stack.push(currentFiber);
currentFiber = currentFiber.return;
}
const newStack = new Array(stack.length);
for (let i = 0; i < stack.length; i++) {
newStack[i] = stack[stack.length - i - 1];
}
return newStack;
return stack;
};

/**
Expand Down Expand Up @@ -411,25 +412,26 @@ export const getNearestHostFiber = (
*/
export const getNearestHostFibers = (fiber: Fiber): Fiber[] => {
const hostFibers: Fiber[] = [];
const stack: Fiber[] = [];

let head: StackNode<Fiber> | undefined;

if (isHostFiber(fiber)) {
hostFibers.push(fiber);
} else if (fiber.child) {
stack.push(fiber.child);
head = createStackNode(fiber.child);
}

while (stack.length) {
const currentNode = stack.pop();
if (!currentNode) break;
if (isHostFiber(currentNode)) {
hostFibers.push(currentNode);
} else if (currentNode.child) {
stack.push(currentNode.child);
while (head) {
const current = head.value;
head = head.next;
if (isHostFiber(current)) {
hostFibers.push(current);
} else if (current.child) {
head = createStackNode(current.child, head);
}

if (currentNode.sibling) {
stack.push(currentNode.sibling);
if (current.sibling) {
head = createStackNode(current.sibling, head);
}
}

Expand Down Expand Up @@ -499,11 +501,14 @@ type FiberType =
* Returns the type (e.g. component definition) of the {@link Fiber}
*/
export const getType = (type: unknown): React.ComponentType<unknown> | null => {
if (!type) {
return null;
}
const currentType = type as FiberType;
if (typeof currentType === 'function') {
return currentType;
}
if (typeof currentType === 'object' && currentType) {
if (typeof currentType === 'object') {
// memo / forwardRef case
return getType(
(currentType as React.MemoExoticComponent<React.ComponentType<unknown>>)
Expand All @@ -518,11 +523,11 @@ export const getType = (type: unknown): React.ComponentType<unknown> | null => {
* Returns the display name of the {@link Fiber}.
*/
export const getDisplayName = (type: unknown): string | null => {
if (!type) {
return null;
}
const currentType = type as FiberType;
if (
typeof currentType !== 'function' &&
!(typeof currentType === 'object' && currentType)
) {
if (!(typeof currentType === 'function' || typeof currentType === 'object')) {
return null;
}
const name = currentType.displayName || currentType.name || null;
Expand Down
2 changes: 1 addition & 1 deletion src/index-dom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,8 @@ describe("getFiberStack", () => {
onCommitFiberRoot: (_rendererID, fiberRoot) => {
manualFiberStack = [];
maybeFiber = fiberRoot.current.child.child;
manualFiberStack.push(fiberRoot.current.child);
manualFiberStack.push(fiberRoot.current.child.child);
manualFiberStack.push(fiberRoot.current.child);
},
});
render(
Expand Down
10 changes: 5 additions & 5 deletions src/inspect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,11 @@ const FiberGraph = React.memo(
const nodes: Node[] = [];
const links: Link[] = [];
const nodeMap = new Map<string, Node>();
const stack: Array<{
fiber: Fiber;
depth: number;
parentId: string | null;
}> = [{ fiber, depth: 0, parentId: null }];
// const stack: Array<{
// fiber: Fiber;
// depth: number;
// parentId: string | null;
// }> = [{ fiber, depth: 0, parentId: null }];
let nodeCounter = 0;

const baseId = getFiberId(fiber).toString();
Expand Down
90 changes: 45 additions & 45 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,70 @@
import type {
HostConfig,
Thenable,
RootTag,
WorkTag,
BundleType,
ComponentSelector,
DevToolsConfig,
FiberRoot,
Flags,
HasPseudoClassSelector,
HookType,
Source,
HostConfig,
LanePriority,
Lanes,
Flags,
TypeOfMode,
ReactProvider,
ReactProviderType,
MutableSource,
OpaqueHandle,
OpaqueRoot,
React$AbstractComponent,
ReactConsumer,
ReactContext,
Fiber as ReactFiber,
ReactPortal,
ReactProvider,
ReactProviderType,
RefObject,
Fiber as ReactFiber,
FiberRoot,
MutableSource,
OpaqueHandle,
OpaqueRoot,
BundleType,
DevToolsConfig,
SuspenseHydrationCallbacks,
TransitionTracingCallbacks,
ComponentSelector,
HasPseudoClassSelector,
RoleSelector,
TextSelector,
TestNameSelector,
RootTag,
Selector,
React$AbstractComponent,
Source,
SuspenseHydrationCallbacks,
TestNameSelector,
TextSelector,
Thenable,
TransitionTracingCallbacks,
TypeOfMode,
WorkTag,
} from 'react-reconciler';

export type {
HostConfig,
Thenable,
RootTag,
WorkTag,
BundleType,
ComponentSelector,
DevToolsConfig,
FiberRoot,
Flags,
HasPseudoClassSelector,
HookType,
Source,
HostConfig,
LanePriority,
Lanes,
Flags,
TypeOfMode,
ReactProvider,
ReactProviderType,
MutableSource,
OpaqueHandle,
OpaqueRoot,
React$AbstractComponent,
ReactConsumer,
ReactContext,
ReactPortal,
ReactProvider,
ReactProviderType,
RefObject,
FiberRoot,
MutableSource,
OpaqueHandle,
OpaqueRoot,
BundleType,
DevToolsConfig,
SuspenseHydrationCallbacks,
TransitionTracingCallbacks,
ComponentSelector,
HasPseudoClassSelector,
RoleSelector,
TextSelector,
TestNameSelector,
RootTag,
Selector,
React$AbstractComponent,
Source,
SuspenseHydrationCallbacks,
TestNameSelector,
TextSelector,
Thenable,
TransitionTracingCallbacks,
TypeOfMode,
WorkTag,
};

export interface ReactDevToolsGlobalHook {
Expand Down
14 changes: 14 additions & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface StackNode<T> {
next: StackNode<T> | undefined;
value: T;
}

export function createStackNode<T>(
value: T,
next?: StackNode<T>,
): StackNode<T> {
return {
next,
value,
};
}