Skip to content
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

[DevTools] Add support for useMemoCache #26696

Merged
merged 5 commits into from
Apr 25, 2023
Merged

[DevTools] Add support for useMemoCache #26696

merged 5 commits into from
Apr 25, 2023

Conversation

poteto
Copy link
Member

@poteto poteto commented Apr 21, 2023

useMemoCache wasn't previously supported in the DevTools, so any attempt to inspect a component using the hook would result in a dispatcher.useMemoCache is not a function (it is undefined) error.

@poteto poteto requested a review from acdlite April 21, 2023 19:50
@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Apr 21, 2023
@react-sizebot
Copy link

react-sizebot commented Apr 21, 2023

Comparing: bf449ee...de0445d

Critical size changes

Includes critical production bundles, as well as any change greater than 2%:

Name +/- Base Current +/- gzip Base gzip Current gzip
oss-stable/react-dom/cjs/react-dom.production.min.js = 163.94 kB 163.94 kB = 51.68 kB 51.68 kB
oss-experimental/react-dom/cjs/react-dom.production.min.js = 169.47 kB 169.47 kB = 53.33 kB 53.33 kB
facebook-www/ReactDOM-prod.classic.js = 572.79 kB 572.79 kB = 101.48 kB 101.48 kB
facebook-www/ReactDOM-prod.modern.js = 556.53 kB 556.53 kB = 98.67 kB 98.67 kB
oss-experimental/react-debug-tools/cjs/react-debug-tools.production.min.js +5.55% 6.88 kB 7.26 kB +5.33% 2.55 kB 2.69 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.production.min.js +5.55% 6.88 kB 7.26 kB +5.33% 2.55 kB 2.69 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.production.min.js +5.55% 6.88 kB 7.26 kB +5.33% 2.55 kB 2.69 kB
oss-experimental/react-debug-tools/cjs/react-debug-tools.development.js +3.64% 22.83 kB 23.66 kB +3.33% 6.12 kB 6.33 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.development.js +3.64% 22.83 kB 23.66 kB +3.33% 6.12 kB 6.33 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.development.js +3.64% 22.83 kB 23.66 kB +3.33% 6.12 kB 6.33 kB

Significant size changes

Includes any change greater than 0.2%:

Expand to show
Name +/- Base Current +/- gzip Base gzip Current gzip
oss-experimental/react-debug-tools/cjs/react-debug-tools.production.min.js +5.55% 6.88 kB 7.26 kB +5.33% 2.55 kB 2.69 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.production.min.js +5.55% 6.88 kB 7.26 kB +5.33% 2.55 kB 2.69 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.production.min.js +5.55% 6.88 kB 7.26 kB +5.33% 2.55 kB 2.69 kB
oss-experimental/react-debug-tools/cjs/react-debug-tools.development.js +3.64% 22.83 kB 23.66 kB +3.33% 6.12 kB 6.33 kB
oss-stable-semver/react-debug-tools/cjs/react-debug-tools.development.js +3.64% 22.83 kB 23.66 kB +3.33% 6.12 kB 6.33 kB
oss-stable/react-debug-tools/cjs/react-debug-tools.development.js +3.64% 22.83 kB 23.66 kB +3.33% 6.12 kB 6.33 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.production.min.js +0.53% 102.34 kB 102.89 kB +0.56% 31.39 kB 31.56 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.production.min.js +0.53% 102.39 kB 102.93 kB +0.56% 31.41 kB 31.58 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.production.min.js +0.53% 102.52 kB 103.06 kB +0.55% 31.45 kB 31.63 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.production.min.js +0.53% 102.74 kB 103.29 kB +0.36% 31.87 kB 31.99 kB
oss-stable/react-test-renderer/umd/react-test-renderer.production.min.js +0.53% 102.79 kB 103.33 kB +0.36% 31.90 kB 32.01 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.production.min.js +0.53% 102.92 kB 103.46 kB +0.37% 31.92 kB 32.04 kB
oss-stable-semver/react-test-renderer/umd/react-test-renderer.development.js +0.34% 812.47 kB 815.27 kB +0.31% 171.15 kB 171.68 kB
oss-stable/react-test-renderer/umd/react-test-renderer.development.js +0.34% 812.49 kB 815.29 kB +0.31% 171.18 kB 171.70 kB
oss-experimental/react-test-renderer/umd/react-test-renderer.development.js +0.34% 812.88 kB 815.68 kB +0.31% 171.28 kB 171.81 kB
oss-stable-semver/react-test-renderer/cjs/react-test-renderer.development.js +0.34% 775.62 kB 778.25 kB +0.33% 169.39 kB 169.95 kB
oss-stable/react-test-renderer/cjs/react-test-renderer.development.js +0.34% 775.64 kB 778.27 kB +0.33% 169.42 kB 169.97 kB
oss-experimental/react-test-renderer/cjs/react-test-renderer.development.js +0.34% 776.01 kB 778.64 kB +0.32% 169.50 kB 170.05 kB

Generated by 🚫 dangerJS against de0445d

Copy link
Contributor

@josephsavona josephsavona left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, i leave it to folks with more devtools experience to stamp

@poteto poteto force-pushed the lt/devtools-uMC branch 2 times, most recently from 0266d1f to ad0bd14 Compare April 21, 2023 20:40
@poteto poteto requested a review from hoxyq April 21, 2023 20:43
@poteto
Copy link
Member Author

poteto commented Apr 21, 2023

cc @mondaychen, for some reason I couldn't add you as a reviewer

@mondaychen
Copy link
Contributor

No sure why the test is failing on CI. It works OK on my local machine.

Copy link
Contributor

@mondaychen mondaychen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It may appear that react-debug-tools is a separate package from react-devtools-* packages, allowing you to safely use shared/* within it. However, the truth is that it has always been an integral part of the React DevTools backend, which is designed to be compatible with any React version after v15.

If I had been able to complete my DevTools backend re-architecture project, this issue would be less concerning since we will have lockstep between React and DevTools. 🥲

At the moment, I don't have an optimal solution. Perhaps it's acceptable assuming the implementation of useMemoCache remains unchanged in the near future.

@@ -15,6 +15,7 @@ import type {
ReactProviderType,
StartTransitionOptions,
} from 'shared/ReactTypes';
import {REACT_MEMO_CACHE_SENTINEL} from 'shared/ReactSymbols';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might cause compatibility issues in the future. See packages/react-devtools-shared/src/backend/ReactSymbols.js
Also, this is not a type, so should not be in the middle of other import type lines

@mondaychen
Copy link
Contributor

Also @poteto it looks like I'm not longer authorized to approve or request changes on PRs here 😢

Comment on lines 362 to 373
data = new Array(size);
for (let i = 0; i < size; i++) {
data[i] = REACT_MEMO_CACHE_SENTINEL;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can this be simplified with Array.fill?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! We ran some benchmarks on this before: #25465 (comment)

@poteto
Copy link
Member Author

poteto commented Apr 24, 2023

@mondaychen @hoxyq any idea why this test variant would have different results?

@mondaychen
Copy link
Contributor

@poteto any reason you need to use toMatchInlineSnapshot for this one? Looks like other tests in this file are using expect(tree).toEqual

@poteto
Copy link
Member Author

poteto commented Apr 24, 2023

@poteto any reason you need to use toMatchInlineSnapshot for this one? Looks like other tests in this file are using expect(tree).toEqual

No, I changed it already but the fact is still that this particular test variant has a different output than all the other variants, and I'm not sure why

@hoxyq
Copy link
Contributor

hoxyq commented Apr 25, 2023

@mondaychen @hoxyq any idea why this test variant would have different results?

I think there is a problem with this function:

function isReactWrapper(functionName: any, primitiveName: string) {
if (!functionName) {
return false;
}
const expectedPrimitiveName = 'use' + primitiveName;
if (functionName.length < expectedPrimitiveName.length) {
return false;
}
return (
functionName.lastIndexOf(expectedPrimitiveName) ===
functionName.length - expectedPrimitiveName.length
);
}

Here functionName is Object.useMemoCache [as unstable_useMemoCache] and expectedPrimitiveName is useMemoCache and this function returns false. I am not sure, but based on that this function returns true for each other hook used in these tests, I am expecting that it should return true for unstable_useMemoCache.

This is where the things are starting to break. When we build hooks tree, at some point we are parsing the stack and if its not empty, then we are parsing custom hook names:

const stack = parseTrimmedStack(rootStack, hook);
if (stack !== null) {
// Note: The indices 0 <= n < length-1 will contain the names.
// The indices 1 <= n < length will contain the source locations.
// That's why we get the name from n - 1 and don't check the source
// of index 0.
let commonSteps = 0;
if (prevStack !== null) {
// Compare the current level's stack to the new stack.
while (commonSteps < stack.length && commonSteps < prevStack.length) {
const stackSource = stack[stack.length - commonSteps - 1].source;
const prevSource =
prevStack[prevStack.length - commonSteps - 1].source;
if (stackSource !== prevSource) {
break;
}
commonSteps++;
}
// Pop back the stack as many steps as were not common.
for (let j = prevStack.length - 1; j > commonSteps; j--) {
levelChildren = stackOfChildren.pop();
}
}

That's why we receiving .useMemoCache [as unstable_useMemoCache] as name inside the failing test ¯\_(ツ)_/¯

useMemoCache wasn't previously supported in the DevTools, so any attempt
to inspect a component using the hook would result in a
`dispatcher.useMemoCache is not a function (it is undefined)` error.
@poteto
Copy link
Member Author

poteto commented Apr 25, 2023

@hoxyq awesome, thanks for the pointer! I fixed the test by aliasing the unstable_ prefixed name to one without, rather than change anything in that code you linked. This is more correct for user code (eg lint rules would probably be written for the unprefixed hook name) and we'll eventually drop the unstable prefix anyway so there's nothing "wrong" about the current implementation not supporting unstable imo.

We don't need this symbol allocated unless we're handling a
useMemoCache call
@poteto poteto merged commit 25b99ef into main Apr 25, 2023
@poteto poteto deleted the lt/devtools-uMC branch April 25, 2023 16:19
hoxyq added a commit that referenced this pull request Nov 14, 2023
…27659)

In #27472 I've removed broken
`useMemoCache` implementation and replaced it with a stub. It actually
produces errors when trying to inspect components, which are compiled
with Forget.

The main difference from the implementation in
#26696 is that we are using
corresponding `Fiber` here, which has patched `updateQueue` with
`memoCache`. Previously we would check it on a hook object, which
doesn't have `updateQueue`.

Tested on pages, which are using Forget and by inspecting elements,
which are transpiled with Forget.
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
useMemoCache wasn't previously supported in the DevTools, so any attempt
to inspect a component using the hook would result in a
`dispatcher.useMemoCache is not a function (it is undefined)` error.
EdisonVan pushed a commit to EdisonVan/react that referenced this pull request Apr 15, 2024
…acebook#27659)

In facebook#27472 I've removed broken
`useMemoCache` implementation and replaced it with a stub. It actually
produces errors when trying to inspect components, which are compiled
with Forget.

The main difference from the implementation in
facebook#26696 is that we are using
corresponding `Fiber` here, which has patched `updateQueue` with
`memoCache`. Previously we would check it on a hook object, which
doesn't have `updateQueue`.

Tested on pages, which are using Forget and by inspecting elements,
which are transpiled with Forget.
bigfootjon pushed a commit that referenced this pull request Apr 18, 2024
useMemoCache wasn't previously supported in the DevTools, so any attempt
to inspect a component using the hook would result in a
`dispatcher.useMemoCache is not a function (it is undefined)` error.

DiffTrain build for commit 25b99ef.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants