Added setChildren() function

Summary:
public
Most of the time - especially during app startup - when we call UIManager.manageChildren(), we are actually just adding the first set of children to a newly created view.

This case is already optimized for in the JS code, by memoizing index arrays at various sizes, but this is not especially efficient since it is still sending an array of indices with each call that could be easily inferred on the native side instead.

I've added a hybrid native/JS optimization that improves the performance for this case. It's not a huge win in terms of time saved, but benchmarks show improvements in the ~1% range for several of the app startup metrics.

Reviewed By: tadeuzagallo

Differential Revision: D2757388

fb-gh-sync-id: 74f0cdbba93af2c04d69b192a8c2cc5cf429fa09
This commit is contained in:
Nick Lockwood
2015-12-15 06:56:07 -08:00
committed by facebook-github-bot-4
parent eb188c8d98
commit 9f48c004ba
4 changed files with 61 additions and 37 deletions

View File

@@ -13,4 +13,33 @@
var UIManager = require('NativeModules').UIManager;
if (!UIManager.setChildren) {
/**
* Index cache (used by setChildren())
*/
UIManager._cachedIndexArray = function(size) {
var cachedResult = this._cachedIndexArray._cache[size];
if (!cachedResult) {
var arr = [];
for (var i = 0; i < size; i++) {
arr[i] = i;
}
this._cachedIndexArray._cache[size] = arr;
return arr;
} else {
return cachedResult;
}
};
UIManager._cachedIndexArray._cache = {};
/**
* Fallback setChildren() implementation for Android
*/
UIManager.setChildren = function(containerTag, createdTags) {
var indexes = this._cachedIndexArray(createdTags.length);
UIManager.manageChildren(containerTag, null, null, createdTags, indexes, null);
}
}
module.exports = UIManager;