Skip to content

Improve unmasked context caching #8723

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

Merged
merged 1 commit into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/renderers/shared/fiber/ReactFiberClassComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import type { Fiber } from 'ReactFiber';
import type { PriorityLevel } from 'ReactPriorityLevel';

var {
cacheContext,
getMaskedContext,
getUnmaskedContext,
isContextConsumer,
} = require('ReactFiberContext');
var {
addUpdate,
Expand All @@ -28,6 +30,7 @@ var {
var { hasContextChanged } = require('ReactFiberContext');
var { getComponentName, isMounted } = require('ReactFiberTreeReflection');
var ReactInstanceMap = require('ReactInstanceMap');
var emptyObject = require('emptyObject');
var shallowEqual = require('shallowEqual');
var warning = require('warning');
var invariant = require('invariant');
Expand Down Expand Up @@ -206,15 +209,17 @@ module.exports = function(
const ctor = workInProgress.type;
const props = workInProgress.pendingProps;
const unmaskedContext = getUnmaskedContext(workInProgress);
const context = getMaskedContext(workInProgress, unmaskedContext);
const needsContext = isContextConsumer(workInProgress);
const context = needsContext ? getMaskedContext(workInProgress, unmaskedContext) : emptyObject;
const instance = new ctor(props, context);
adoptClassInstance(workInProgress, instance);
checkClassInstance(workInProgress);

// Cache unmasked context so we can avoid recreating masked context unless necessary.
// ReactFiberContext usually updates this cache but can't for newly-created instances.
instance.__reactInternalMemoizedUnmaskedChildContext = unmaskedContext;
instance.__reactInternalMemoizedMaskedChildContext = context;
if (needsContext) {
cacheContext(workInProgress, unmaskedContext, context);
}

return instance;
}
Expand Down
19 changes: 17 additions & 2 deletions src/renderers/shared/fiber/ReactFiberContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function getUnmaskedContext(workInProgress : Fiber) : Object {
}
exports.getUnmaskedContext = getUnmaskedContext;

function cacheContext(workInProgress : Fiber, unmaskedContext : Object, maskedContext : Object) {
const instance = workInProgress.stateNode;
instance.__reactInternalMemoizedUnmaskedChildContext = unmaskedContext;
instance.__reactInternalMemoizedMaskedChildContext = maskedContext;
}
exports.cacheContext = cacheContext;

exports.getMaskedContext = function(workInProgress : Fiber, unmaskedContext : Object) {
const type = workInProgress.type;
const contextTypes = type.contextTypes;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove this check here now, and always do it externally?

Copy link
Contributor Author

@bvaughn bvaughn Jan 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. Same safety-related concern as above.

Edit: I think I'm going to leave this as-is since we call getMaskedContext in half a dozen places and it seems to easy to mess up if we require all callers to check isContextConsumer before calling.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Expand Down Expand Up @@ -86,9 +93,9 @@ exports.getMaskedContext = function(workInProgress : Fiber, unmaskedContext : Ob
}

// Cache unmasked context so we can avoid recreating masked context unless necessary.
// Context is created before the class component is instantiated so check for instance.
if (instance) {
instance.__reactInternalMemoizedUnmaskedChildContext = unmaskedContext;
instance.__reactInternalMemoizedMaskedChildContext = context;
cacheContext(workInProgress, unmaskedContext, context);
}

return context;
Expand All @@ -98,6 +105,14 @@ exports.hasContextChanged = function() : boolean {
return didPerformWorkStackCursor.current;
};

function isContextConsumer(fiber : Fiber) : boolean {
return (
fiber.tag === ClassComponent &&
fiber.type.contextTypes != null
);
}
exports.isContextConsumer = isContextConsumer;

function isContextProvider(fiber : Fiber) : boolean {
return (
fiber.tag === ClassComponent &&
Expand Down