Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Inline traversal pooling logic into ReactChildren
  • Loading branch information
gaearon committed Jul 20, 2017
commit cd36b0aa5ea12fbe3d768deff315175c99e77a11
2 changes: 1 addition & 1 deletion scripts/rollup/packaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const facebookWWWSrcDependencies = [

// these files need to be copied to the react-native build
const reactNativeSrcDependencies = [
'src/shared/utils/PooledClass.js',
'src/renderers/shared/utils/PooledClass.js',
'src/renderers/shared/fiber/isomorphic/ReactTypes.js',
'src/renderers/native/ReactNativeTypes.js',
];
Expand Down
24 changes: 12 additions & 12 deletions scripts/rollup/results.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
{
"bundleSizes": {
"react.development.js (UMD_DEV)": {
"size": 69376,
"gzip": 17780
"size": 65151,
"gzip": 16692
},
"react.production.min.js (UMD_PROD)": {
"size": 7585,
"gzip": 3002
"size": 6489,
"gzip": 2704
},
"react.development.js (NODE_DEV)": {
"size": 59667,
"gzip": 15411
"size": 55446,
"gzip": 14337
},
"react.production.min.js (NODE_PROD)": {
"size": 6451,
"gzip": 2576
"size": 5352,
"gzip": 2269
},
"React-dev.js (FB_DEV)": {
"size": 56905,
"gzip": 14622
"size": 52704,
"gzip": 13552
},
"React-prod.js (FB_PROD)": {
"size": 27099,
"gzip": 7215
"size": 24229,
"gzip": 6612
Copy link
Contributor

Choose a reason for hiding this comment

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

whoa. why is FB_PROD so much larger than NODE_PROD/UMD_PROD? Legacy files that are being consumed internally yet?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

No, it's just not minified. We might minify it eventually but for now it doesn't seem necessary (it goes through our pipeline anyway), and it's easier to see what happens during the sync.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah! Great point. 👍

},
"react-dom.development.js (UMD_DEV)": {
"size": 624799,
Expand Down
71 changes: 40 additions & 31 deletions src/isomorphic/children/ReactChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@

'use strict';

var PooledClass = require('PooledClass');
var ReactElement = require('ReactElement');

var emptyFunction = require('fbjs/lib/emptyFunction');
var invariant = require('fbjs/lib/invariant');

var twoArgumentPooler = PooledClass.twoArgumentPooler;
var fourArgumentPooler = PooledClass.fourArgumentPooler;

if (__DEV__) {
var warning = require('fbjs/lib/warning');
var {getStackAddendum} = require('ReactDebugCurrentFrame');
Expand Down Expand Up @@ -244,40 +240,53 @@ function forEachChildren(children, forEachFunc, forEachContext) {
if (children == null) {
return children;
}
var traverseContext = MapBookKeeping.getPooled(
var traverseContext = getPooledTraverseContext(
null,
null,
forEachFunc,
forEachContext,
);
traverseAllChildren(children, forEachSingleChild, traverseContext);
MapBookKeeping.release(traverseContext);
releaseTraverseContext(traverseContext);
}

/**
* PooledClass representing the bookkeeping associated with performing a child
* mapping. Allows avoiding binding callbacks.
*
* @constructor MapBookKeeping
* @param {!*} mapResult Object containing the ordered map of results.
* @param {!function} mapFunction Function to perform mapping with.
* @param {?*} mapContext Context to perform mapping with.
*/
function MapBookKeeping(mapResult, keyPrefix, mapFunction, mapContext) {
this.result = mapResult;
this.keyPrefix = keyPrefix;
this.func = mapFunction;
this.context = mapContext;
this.count = 0;
var POOL_SIZE = 10;
var traverseContextPool = [];
function getPooledTraverseContext(
mapResult,
keyPrefix,
mapFunction,
mapContext,
) {
if (traverseContextPool.length) {
var traverseContext = traverseContextPool.pop();
traverseContext.result = mapResult;
traverseContext.keyPrefix = keyPrefix;
traverseContext.func = mapFunction;
traverseContext.context = mapContext;
traverseContext.count = 0;
return traverseContext;
} else {
return {
result: mapResult,
keyPrefix: keyPrefix,
func: mapFunction,
context: mapContext,
count: 0,
};
}
}

function releaseTraverseContext(traverseContext) {
traverseContext.result = null;
traverseContext.keyPrefix = null;
traverseContext.func = null;
traverseContext.context = null;
traverseContext.count = 0;
if (traverseContextPool.length < POOL_SIZE) {
traverseContextPool.push(traverseContext);
}
}
MapBookKeeping.prototype.destructor = function() {
this.result = null;
this.keyPrefix = null;
this.func = null;
this.context = null;
this.count = 0;
};
PooledClass.addPoolingTo(MapBookKeeping, fourArgumentPooler);

function mapSingleChildIntoContext(bookKeeping, child, childKey) {
var {result, keyPrefix, func, context} = bookKeeping;
Expand Down Expand Up @@ -312,14 +321,14 @@ function mapIntoWithKeyPrefixInternal(children, array, prefix, func, context) {
if (prefix != null) {
escapedPrefix = escapeUserProvidedKey(prefix) + '/';
}
var traverseContext = MapBookKeeping.getPooled(
var traverseContext = getPooledTraverseContext(
array,
escapedPrefix,
func,
context,
);
traverseAllChildren(children, mapSingleChildIntoContext, traverseContext);
MapBookKeeping.release(traverseContext);
releaseTraverseContext(traverseContext);
}

/**
Expand Down