Conversation
|
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
| this.destination = null; | ||
| this.bundlerConfig = bundlerConfig; | ||
| this.cache = new Map(); | ||
| this.cache = new WeakMap(); |
There was a problem hiding this comment.
I might have missed some instances since Flow didn't complain here since we silence errors later. Though I assume we only init this once when we create the request and not reassign it later?
df76d94 to
3d293d8
Compare
Allows us to get rid of an indiretion in the Client implementation.
3d293d8 to
649c219
Compare
|
Orb Code Review (powered by GLM 5.1 on Orb Cloud) SummaryExcellent cache optimization that simplifies React's cache architecture by eliminating nested WeakMaps and reducing indirection. Improves both performance and memory efficiency. Architecture Improvement ✅Before: Complex Nested Cachingfunction getCacheForType<T>(resourceType: () => T): T {
const cache: Cache = readContext(CacheContext);
let cacheForType: T | void = cache.data.get(resourceType);
if (cacheForType === undefined) {
cacheForType = resourceType(); // Creates new WeakMap per type
cache.data.set(resourceType, cacheForType);
}
return cacheForType;
}
// Usage creates factory functions and nested WeakMaps
const fnMap = dispatcher.getCacheForType(createCacheRoot);After: Direct Cache Accessfunction getActiveCache(): AsyncCache {
const cache: Cache = readContext(CacheContext);
return cache.data; // Direct access to the main WeakMap
}
// Usage is direct and efficient
const activeCache = dispatcher.getActiveCache();Performance Benefits ✅1. Eliminates Factory Functions
2. Reduces Memory Footprint
3. Simplifies Access Pattern
Code Quality ✅API Simplification// Cleaner API surface
export type AsyncDispatcher = {
-getCacheForType<T>(resourceType: () => T): T,
+getActiveCache(): AsyncCache,
};Type Safety Maintained
Cross-package Impact ✅Consistent ImplementationChanges applied across all React packages:
Backwards Compatibility
Cache Pattern Analysis ✅WeakMap Usage// Optimal cache pattern
const activeCache = dispatcher.getActiveCache();
const fnNode: CacheNode<T> | void = (activeCache.get(fn): any);
if (fnNode === undefined) {
cacheNode = createCacheNode();
activeCache.set(fn, cacheNode); // Direct WeakMap operation
}
Testing Coverage ✅Updated test suites:
Assessment: ✅ APPROVEThis is a well-executed performance optimization that demonstrates excellent architectural refinement:
Recommendation: Merge immediately. This optimization improves React's cache performance without any downsides or breaking changes. Impact: Applications using React's cache API will benefit from reduced memory usage and faster cache operations, particularly in scenarios with frequent cache access. |
Stacked on #30736
Summary
Instead of using
Map.get(createCacheRoot).get(fn)we can just use rely onAsyncCachebeing aWeakMap.How did you test this change?