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

chore(project-config): Cache results of common functions #8581

Merged
merged 5 commits into from
Jun 15, 2023

Conversation

Josh-Walker-GM
Copy link
Collaborator

We have two functions getPaths and getConfigPath which are good candidates for caching. Their output is deterministic from the input and they are called often.

The original comment for getPaths mentioned using a proxy. I did not understanding the motivation behind that as we're exporting a function which returns an object and not just a paths object itself. I might just not be seeing the obvious though.

I had thought to freeze the paths object to prevent unintended mutations but decided not to fix a problem that doesn't exist.

@Josh-Walker-GM Josh-Walker-GM added the release:chore This PR is a chore (means nothing for users) label Jun 10, 2023
@Tobbe
Copy link
Member

Tobbe commented Jun 10, 2023

Is computing the paths map actually slow? Like, are commands noticeably faster with the caching?

The comment about the Proxy I think is more about the second part of that same comment. The part about making it lazy. I.e. only compute the path for something when it's first accessed. But honestly I think that'd be slower overall than just doing what we have now. String concatenation can't really be that slow, can it?

@Josh-Walker-GM
Copy link
Collaborator Author

Ahh I see what is meant by the proxy now, thanks!

I didn't do any benchmarks on this. I would bet the benefit is quite small and not likely to be noticeable. I would still argue it does no harm to cache this given we know it's typically called more than once and it's essentially a constant.

@jtoar
Copy link
Contributor

jtoar commented Jun 10, 2023

I'll just add that this is mainly a refactor, since we already make this optimization. We're trying to deduplicate the code here as we make more CLI packages:

export const _getPaths = () => {
try {
return getRedwoodPaths()
} catch (e) {
console.error(c.error((e as Error).message))
process.exit(1)
}
}
export const getPaths = memoize(_getPaths)

This was from the CLI here:

/**
* This wraps the core version of getPaths into something that catches the exception
* and displays a helpful error message.
*/
export const _getPaths = () => {
try {
return getRedwoodPaths()
} catch (e) {
console.error(c.error(e.message))
process.exit(1)
}
}
export const getPaths = memoize(_getPaths)

I'm not sure if we ever did the benchmarks before making this optimization, they'd still be good to do, but this an optimization we already make.

@dac09
Copy link
Collaborator

dac09 commented Jun 13, 2023

Just a thought - very often caching and memoisation in the CLI would have very little effect IMO. This is because CLI executions tend to me short running processes (the exceptions being dev, serve).

Which means anything cached in memory gets discarded as soon as the process has finished execution! IMHO, if we really wanted to do, the safest and least complicated option would be to just use the memoize.

@Josh-Walker-GM
Copy link
Collaborator Author

Thanks Danny. As Dom highlighted this was really just to upstream what we already do in the cli lib. It can't really hurt to cache these results even if it is just for a short time for a cli command.

@jtoar jtoar enabled auto-merge (squash) June 15, 2023 18:33
@jtoar jtoar disabled auto-merge June 15, 2023 18:33
@jtoar jtoar merged commit 368ea41 into main Jun 15, 2023
10 of 11 checks passed
@jtoar jtoar deleted the jgmw-config/caching branch June 15, 2023 18:33
@redwoodjs-bot redwoodjs-bot bot added this to the next-release milestone Jun 15, 2023
@jtoar jtoar modified the milestones: next-release, v6.0.0 Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:chore This PR is a chore (means nothing for users)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants