-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Conversation
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? |
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. |
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: redwood/packages/cli-packages/storybook/src/lib/project.ts Lines 11 to 19 in 708902d
This was from the CLI here: redwood/packages/cli/src/lib/index.js Lines 197 to 209 in 708902d
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. |
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. |
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. |
We have two functions
getPaths
andgetConfigPath
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 apaths
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.