refactor(cache): simplify noCache condition#362
Merged
ezolenko merged 1 commit intoJun 24, 2022
Conversation
- if there is no cache, we don't need to do any operations on a cache at all, so we can just totally skip all the cache operations and return early
- this should be a perf improvement, at the very least on memory usage, as lots of stuff isn't created or processed now
- this may make `clean: true` a more optimal choice for smaller projects, as the FS usage when writing to cache may be slower than the small amount of compilation and type-checking required in small projects
- to start, from the constructor, the only necessary piece when `noCache` is the dependency tree
- this is needed for `walkTree` and `setDependency` to function, but otherwise is unused when `noCache`
- this _might_ be able to be further optimized to remove the graph entirely when `noCache`, but that is saved for potential future work and not part of this commit
- so, we can just move the tree creation further up in the constructor as its previous ordering within the constructor does not actually matter
- once this is done, we can just early return when `noCache` instead of doing all the cache-related actions, since they're not neeeded when there is no cache
- no need to set `cacheDir` or any hashes etc since they're not used
- note that `clean` only uses `cachePrefix` and `cacheRoot`, which are already set, and does not use `cacheDir`
- no need to `init` the cache as it's not used
- also slightly change the ordering to move `init` right after its prereqs are done, i.e. setting `cacheDir`, `hashOptions`, etc
- just keeps with the flow instead of calling it in the middle of the ambient type processing
- no need to check ambient types as that is only used for cache invalidation (marking things dirty), which is not used when there is no cache
- note that `isDirty` is literally never called when `noCache`
- from there, since we don't call `checkAmbientTypes` or `init` when `noCache` (the constructor is the only place they are called and they are both `private`), we can entirely remove their `noCache` branches
- fairly simple for `checkAmbientTypes`, we just remove the tiny if block that sets `ambientTypesDirty`, as, well, "dirty" isn't used when there is no cache
- for `init`, this means we can entirely remove the creation of `NoCache`, which isn't needed when there is no cache
- that means we can also remove the implementation and tests for `NoCache`
- and the reference to it in `CONTRIBUTING.md`
- in `done`, we can also simply skip rolling caches and early return when there is no cache
- the only other tiny change is the non-null assertions for `ambientTypes` and `cacheDir`
- this matches the existing, simplifying non-null assertions for all the caches, so I did not workaround that
- _could_ set a default of an empty array for `ambientTypes` etc to workaround this, but thought it better to match existing code style and not add new things
- this also matches the behavior, as while `ambientTypes` and `cacheDir` could be `null`, this is only if there is no cache, in which case, they are never used
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Greatly simplify the
noCachecondition intscache-- basically most of the code is not used and can be removed or can early return in the case that there is no cache.cleanmethod #358 's simplificationsDetails
if there is no cache, we don't need to do any operations on a cache at all, so we can just totally skip all the cache operations and return early
clean: truea more optimal choice for smaller projects, as the FS usage when writing to cache may be slower than the small amount of compilation and type-checking required in small projectsto start, from the
constructor, the only necessary piece whennoCacheis the dependency treewalkTreeandsetDependencyto function, but otherwise is unused whennoCachenoCache, but that is saved for potential future work and not part of this PRconstructoras its previous ordering within theconstructordoes not actually matternoCacheinstead of doing all the cache-related actions, since they're not needed when there is no cachecacheDiror any hashes etc since they're not usedcleanonly usescachePrefixandcacheRoot, which are already set, and does not usecacheDirinitthe cache as it's not usedinitright after its prereqs are done, i.e. settingcacheDir,hashOptions, etcisDirtyis literally never called whennoCachefrom there, since we don't call
checkAmbientTypesorinitwhennoCache(the constructor is the only place they are called and they are bothprivate), we can entirely remove theirnoCachebranchescheckAmbientTypes, we just remove the tiny if block that setsambientTypesDirty, as, well, "dirty" isn't used when there is no cacheinit, this means we can entirely remove the creation ofNoCache, which isn't needed when there is no cacheNoCacheCONTRIBUTING.mdin
done(which ispublic), we can also simply skip rolling caches and early return when there is no cachethe only other tiny change is the non-null assertions for
ambientTypesandcacheDirambientTypesetc to workaround this, but thought it better to match existing code style and not add new thingsambientTypesandcacheDircould benull, this is only if there is no cache, in which case, they are never usedPotential Future Work
noCacheby exploring if the wholedependencyTreecould be removed in that caseclean, as it actually may be more performant in the case of smaller projects