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

Implement lightweight OptimisticDependencyFunction<TKey> primitive. #50

Merged
merged 2 commits into from
Sep 16, 2019

Conversation

benjamn
Copy link
Owner

@benjamn benjamn commented Sep 16, 2019

While just about any kind of dependency can be modeled with an OptimisticWrapperFunction<TArgs, TResult>, many sources of cache invalidation signals do not require the full machinery of wrapped functions: makeCacheKey, [un]subscribe functions, the ability to rerun the wrapped function to compute the latest value, and everything that comes with the internal Entry class.

Previously, these simpler use cases were handled by "disposable" wrapper functions, which were essentially just ordinary wrapper functions that never returned a value and could be recycled when they became orphans. Despite these restrictions, disposable wrapper functions were still just as expensive as full-featured wrapper functions, so we weren't really capitalizing on the opportunity to do less work.

For dependencies that will never have children of their own, and do not need to compute an expensive result, we can get away with something much simpler:

import { dirname } from "path";
import { readFileSync } from "fs";
import { wrap, dep } from "optimism";

const dependOnDirectory = dep<string>();

const optimisticReadFile = wrap((filePath: string) => {
  dependOnDirectory(dirname(filePath));
  return readFileSync(filePath, "utf8");
});

const contents = optimisticReadFile("path/to/some/file");
dependOnDirectory.dirty("path/to/some");

With this dependency logic, you can call dependOnDirectory.dirty(directoryPath) to dirty all cached optimisticReadFile results that previously depended on directoryPath (because the wrapped function called dependOnDirectory(dirname(filePath)) where dirname(filePath) === directoryPath).

Using the dep<TKey>() API makes sense when you can provide a single TKey value that uniquely identifies the dependency, rather than a series of arguments that need to be transformed by makeCacheKey.

Using dep also implies the dependency is not associated with a function that computes a value. Instead, the dependOnDirectory dependency exists purely to record dependencies via dependOnDirectory(directoryPath) so that dependOnDirectory.dirty(directoryPath) can be called at a later time to invalidate any/all enclosing computations.

Because these new OptimisticDependencyFunction<TKey> functions cannot have child dependencies of their own, they are appropriate for modeling leaves in your dependency graph/tree/DAG. Because these dependencies are lighter-weight than wrapped functions, you should see significant performance benefits from handling your leaves in this way.

@benjamn benjamn self-assigned this Sep 16, 2019
@benjamn benjamn force-pushed the optimistic-dependency-function branch from c7f63f7 to c230ff9 Compare September 16, 2019 18:59
@benjamn benjamn force-pushed the optimistic-dependency-function branch from c230ff9 to 3e94cb2 Compare September 16, 2019 19:07
@benjamn benjamn merged commit 7f7328e into master Sep 16, 2019
@benjamn benjamn deleted the optimistic-dependency-function branch September 16, 2019 19:21
benjamn added a commit that referenced this pull request Sep 16, 2019
The minor version bump is due to the removal of "disposable" wrapper
functions, which have been replaced by OptimisticDependencyFunction:
#50
benjamn added a commit to apollographql/apollo-client that referenced this pull request Sep 16, 2019
benjamn added a commit to apollographql/apollo-client that referenced this pull request Sep 17, 2019
benjamn added a commit to apollographql/apollo-client that referenced this pull request Sep 17, 2019
benjamn pushed a commit to meteor/meteor that referenced this pull request Sep 18, 2019
StephenBarlow pushed a commit to apollographql/apollo-client that referenced this pull request Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant