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

experimental ApolloClient.getMemoryInternals helper #11409

Merged
merged 71 commits into from Dec 15, 2023

Conversation

phryneas
Copy link
Member

@phryneas phryneas commented Dec 4, 2023

Opening this one for feedback, and to see how feasible it is to have a helper like this.

Is this possible without adding a lot of bulk to the prod bundle size?

Checklist:

  • If this PR contains changes to the library itself (not necessary for e.g. docs updates), please include a changeset (see CONTRIBUTING.md)
  • If this PR is a new feature, please reference an issue where a consensus about the design was reached (not necessary for small changes)
  • Make sure all of the significant new logic is covered by tests

@phryneas phryneas added this to the MemoryAnalysis milestone Dec 4, 2023
Copy link

changeset-bot bot commented Dec 4, 2023

🦋 Changeset detected

Latest commit: c3ad505

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@apollo/client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Dec 4, 2023

size-limit report 📦

Path Size
dist/apollo-client.min.cjs 37.9 KB (+0.11% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" 45.52 KB (+2.28% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/main.cjs" (production) 43.06 KB (+0.19% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" 33.92 KB (+2.53% 🔺)
import { ApolloClient, InMemoryCache, HttpLink } from "dist/index.js" (production) 31.85 KB (+0.18% 🔺)
import { ApolloProvider } from "dist/react/index.js" 1.24 KB (0%)
import { ApolloProvider } from "dist/react/index.js" (production) 1.22 KB (0%)
import { useQuery } from "dist/react/index.js" 5.2 KB (+16.74% 🔺)
import { useQuery } from "dist/react/index.js" (production) 4.27 KB (0%)
import { useLazyQuery } from "dist/react/index.js" 5.51 KB (+15.62% 🔺)
import { useLazyQuery } from "dist/react/index.js" (production) 4.58 KB (+0.03% 🔺)
import { useMutation } from "dist/react/index.js" 3.51 KB (+27.75% 🔺)
import { useMutation } from "dist/react/index.js" (production) 2.73 KB (0%)
import { useSubscription } from "dist/react/index.js" 3.19 KB (+31.4% 🔺)
import { useSubscription } from "dist/react/index.js" (production) 2.39 KB (-0.05% 🔽)
import { useSuspenseQuery } from "dist/react/index.js" 5.18 KB (+16.46% 🔺)
import { useSuspenseQuery } from "dist/react/index.js" (production) 3.85 KB (0%)
import { useBackgroundQuery } from "dist/react/index.js" 4.77 KB (+18.36% 🔺)
import { useBackgroundQuery } from "dist/react/index.js" (production) 3.43 KB (0%)
import { useLoadableQuery } from "dist/react/index.js" 5 KB (+17.25% 🔺)
import { useLoadableQuery } from "dist/react/index.js" (production) 3.67 KB (0%)
import { useReadQuery } from "dist/react/index.js" 3.01 KB (0%)
import { useReadQuery } from "dist/react/index.js" (production) 2.95 KB (0%)
import { useFragment } from "dist/react/index.js" 2.11 KB (0%)
import { useFragment } from "dist/react/index.js" (production) 2.06 KB (0%)

*/
export const getApolloClientMemoryInternals =
__DEV__ ?
(_getApolloClientMemoryInternals as RemoveThis<
Copy link
Member

Choose a reason for hiding this comment

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

For future me, a comment about RemoveThis would be helpful here. Something like:

Since InMemoryCache inherits from ApolloCache, TS complains about the incompatibility of this, and so while it's useful to define the type of this as either InMemoryCache or ApolloCache for the purpose of the actual implementations in _getApolloCacheMemoryInternals and _getInMemoryCacheMemoryInternals, we remove this before exporting to avoid the TS conflict on assignment to their respective prototypes.

].filter((x) => x != null);
}

// removeTypenameFromVariables getVariableDefinitions
Copy link
Member

Choose a reason for hiding this comment

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

Can remove :D

Copy link
Member

@alessbell alessbell left a comment

Choose a reason for hiding this comment

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

Excited to integrate this into devtools 🔥

@github-actions github-actions bot added the auto-cleanup 🤖 label Dec 13, 2023
Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

Most of my comments were pretty minor and nitpicky. Super stoked about this though! This will be nice to display in the dev tools.

persistedQueryHashes: 1,
},
{
getVariableDefinitions: 0,
Copy link
Member

Choose a reason for hiding this comment

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

I'd be curious if you'd be interested in a sort of namespacing or identification for these links. Since these memory profiles will be useful for dev tools, its going to be really difficult to correlate these numbers with the links they belong to. Perhaps something like this?

Suggested change
getVariableDefinitions: 0,
'removeTypenameFromVariables.getVariableDefinitions': 0,

or

Suggested change
getVariableDefinitions: 0,
removeTypenameFromVariables: {
getVariableDefinitions: 0,
}

or maybe it makes sense to add a name property 🤔

Suggested change
getVariableDefinitions: 0,
name: 'removeTypenameFromVariables',
getVariableDefinitions: 0,

Thoughts? Or does this clutter this up too much?

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's wait for the pattern we come up over in the cache PR then we'll adjust here :)

link?.getMemoryInternals?.(),
...linkInfo(link?.left),
...linkInfo(link?.right),
].filter((x) => x != null);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
].filter((x) => x != null);
].filter(Boolean);

might also be a nice shortcut that saves a couple bytes 🙂.

Copy link
Member Author

@phryneas phryneas Dec 14, 2023

Choose a reason for hiding this comment

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

I had that, but it also removed 0 values in the DocumentTransform use case further down (which just returns a number, not an object).
So we definitely need this down there. I can see if extracting it out can save a few bytes so at least we don't repeat the function definition. (Although we are talking dev bundle size anyways ^^)

Copy link
Member Author

Choose a reason for hiding this comment

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

Results in the same size, but slightly nicer: e283de3

src/utilities/caching/getMemoryInternals.ts Outdated Show resolved Hide resolved
src/utilities/graphql/DocumentTransform.ts Show resolved Hide resolved
src/utilities/graphql/DocumentTransform.ts Show resolved Hide resolved
},
links: linkInfo(this.link),
queryManager: {
Transforms: this["queryManager"]["transformCache"].size,
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Transforms: this["queryManager"]["transformCache"].size,
transformCache: this["queryManager"]["transformCache"].size,

I'm curious on the decision to name this as Transforms while the rest of the entries are lowercase and typically named after the property they are pulling from. Would it make more sense to call this transformCache?

Copy link
Member Author

Choose a reason for hiding this comment

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

The related cache configuration option is currently called queryManagerTransforms, so it seemed to make sense to have this as queryManager.Transforms instead of a completely different name.

src/utilities/caching/__tests__/getMemoryInternals.ts Outdated Show resolved Hide resolved
src/link/core/ApolloLink.ts Show resolved Hide resolved
src/link/core/ApolloLink.ts Show resolved Hide resolved
Copy link
Member

@jerelmiller jerelmiller left a comment

Choose a reason for hiding this comment

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

💯

Base automatically changed from pr/cache-sizes to release-3.9 December 15, 2023 09:52
Copy link

netlify bot commented Dec 15, 2023

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit 94d1b85
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/657c68904e4e9d00084b493d
😎 Deploy Preview https://deploy-preview-11409--apollo-client-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link

netlify bot commented Dec 15, 2023

Deploy Preview for apollo-client-docs ready!

Name Link
🔨 Latest commit d84a05b
🔍 Latest deploy log https://app.netlify.com/sites/apollo-client-docs/deploys/657c691974f6d90008ce3008
😎 Deploy Preview https://deploy-preview-11409--apollo-client-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@phryneas phryneas merged commit 2e7203b into release-3.9 Dec 15, 2023
26 checks passed
@phryneas phryneas deleted the pr/getCacheStatus branch December 15, 2023 16:15
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants