Push operations directly to the array
Dan Abramov committed
Apr 20, 2019 at 21:05 UTC
02a3a2275307389474c11119cef1f13763cfca60
1 file changed
+34
-55
src/backend/renderer.js
+34
-55
@@ -594,24 +594,16 @@ export function attach(
594
// children when Suspense flips to fallback. These Fibers won't have IDs.
595
let fibersUnmountedInThisCommitPhase: WeakSet<Fiber> = new WeakSet();
596
597
- // TODO: we could make this layer DEV-only and write directly to pendingOperations.
598
- let nextOperation: Array<number> = [];
599
- function beginNextOperation(size: number): void {
600
- nextOperation.length = size;
601
- }
602
- function endNextOperation(): void {
597
+ function pushOperation(op: number): void {
598
if (__DEV__) {
604
- for (let i = 0; i < nextOperation.length; i++) {
605
- if (!Number.isInteger(nextOperation[i])) {
606
- console.error(
607
- 'endNextOperation() was called but some values are not integers.',
608
- nextOperation
609
- );
610
- }
599
+ if (!Number.isInteger(op)) {
600
+ console.error(
601
+ 'pushOperation() was called but the value is not an integer.',
602
+ op
603
+ );
604
}
605
}
613
- pendingOperations.push.apply(pendingOperations, nextOperation);
614
- nextOperation.length = 0;
606
+ pendingOperations.push(op);
607
}
608
609
function flushPendingEvents(root: Object): void {
@@ -699,13 +691,11 @@ export function attach(
691
const hasOwnerMetadata = fiber.hasOwnProperty('_debugOwner');
692
693
if (isRoot) {
702
- beginNextOperation(5);
703
- nextOperation[0] = TREE_OPERATION_ADD;
704
- nextOperation[1] = id;
705
- nextOperation[2] = ElementTypeRoot;
706
- nextOperation[3] = isProfilingSupported ? 1 : 0;
707
- nextOperation[4] = hasOwnerMetadata ? 1 : 0;
708
- endNextOperation();
694
+ pushOperation(TREE_OPERATION_ADD);
695
+ pushOperation(id);
696
+ pushOperation(ElementTypeRoot);
697
+ pushOperation(isProfilingSupported ? 1 : 0);
698
+ pushOperation(hasOwnerMetadata ? 1 : 0);
699
} else {
700
const { displayName, key, type } = getDataForFiber(fiber);
701
const { _debugOwner } = fiber;
@@ -732,25 +722,23 @@ export function attach(
722
displayName === null ? 0 : encodedDisplayName.length;
723
const encodedKeySize = key === null ? 0 : encodedKey.length;
724
735
- beginNextOperation(7 + encodedDisplayNameSize + encodedKeySize);
736
- nextOperation[0] = TREE_OPERATION_ADD;
737
- nextOperation[1] = id;
738
- nextOperation[2] = type;
739
- nextOperation[3] = parentID;
740
- nextOperation[4] = ownerID;
741
- nextOperation[5] = encodedDisplayNameSize;
725
+ pushOperation(TREE_OPERATION_ADD);
726
+ pushOperation(id);
727
+ pushOperation(type);
728
+ pushOperation(parentID);
729
+ pushOperation(ownerID);
730
+ pushOperation(encodedDisplayNameSize);
731
if (displayName !== null) {
732
for (let i = 0; i < encodedDisplayName.length; i++) {
744
- nextOperation[6 + i] = encodedDisplayName[i];
733
+ pushOperation(encodedDisplayName[i]);
734
}
735
}
747
- nextOperation[6 + encodedDisplayNameSize] = encodedKeySize;
736
+ pushOperation(encodedKeySize);
737
if (key !== null) {
738
for (let i = 0; i < encodedKey.length; i++) {
750
- nextOperation[6 + encodedDisplayNameSize + 1 + i] = encodedKey[i];
739
+ pushOperation(encodedKey[i]);
740
}
741
}
753
- endNextOperation();
742
}
743
744
if (isProfiling) {
@@ -758,11 +746,9 @@ export function attach(
746
// So we have to convert them from milliseconds to microseconds so we can send them as ints.
747
const treeBaseDuration = Math.floor(fiber.treeBaseDuration * 1000);
748
761
- beginNextOperation(3);
762
- nextOperation[0] = TREE_OPERATION_UPDATE_TREE_BASE_DURATION;
763
- nextOperation[1] = id;
764
- nextOperation[2] = treeBaseDuration;
765
- endNextOperation();
749
+ pushOperation(TREE_OPERATION_UPDATE_TREE_BASE_DURATION);
750
+ pushOperation(id);
751
+ pushOperation(treeBaseDuration);
752
753
const { actualDuration } = fiber;
754
if (actualDuration > 0) {
@@ -794,11 +780,9 @@ export function attach(
780
if (isRoot) {
781
// Removing a root needs to happen at the end
782
// so we don't batch it with other unmounts.
797
- beginNextOperation(3);
798
- nextOperation[0] = TREE_OPERATION_REMOVE;
799
- nextOperation[1] = 1; // Remove one item
800
- nextOperation[2] = id;
801
- endNextOperation();
783
+ pushOperation(TREE_OPERATION_REMOVE);
784
+ pushOperation(1); // Remove one item
785
+ pushOperation(id);
786
} else if (!shouldFilterFiber(fiber)) {
787
// To maintain child-first ordering,
788
// we'll push it into one of these queues,
@@ -911,12 +895,9 @@ export function attach(
895
// Tree base duration updates are included in the operations typed array.
896
// So we have to convert them from milliseconds to microseconds so we can send them as ints.
897
const treeBaseDuration = Math.floor(fiber.treeBaseDuration * 1000);
914
-
915
- beginNextOperation(3);
916
- nextOperation[0] = TREE_OPERATION_UPDATE_TREE_BASE_DURATION;
917
- nextOperation[1] = getFiberID(getPrimaryFiber(fiber));
918
- nextOperation[2] = treeBaseDuration;
919
- endNextOperation();
898
+ pushOperation(TREE_OPERATION_UPDATE_TREE_BASE_DURATION);
899
+ pushOperation(getFiberID(getPrimaryFiber(fiber)));
900
+ pushOperation(treeBaseDuration);
901
}
902
903
if (haveProfilerTimesChanged(fiber.alternate, fiber)) {
@@ -952,14 +933,12 @@ export function attach(
933
// No need to reorder.
934
return;
935
}
955
- beginNextOperation(3 + numChildren);
956
- nextOperation[0] = TREE_OPERATION_REORDER_CHILDREN;
957
- nextOperation[1] = getFiberID(getPrimaryFiber(fiber));
958
- nextOperation[2] = numChildren;
936
+ pushOperation(TREE_OPERATION_REORDER_CHILDREN);
937
+ pushOperation(getFiberID(getPrimaryFiber(fiber)));
938
+ pushOperation(numChildren);
939
for (let i = 0; i < nextChildren.length; i++) {
960
- nextOperation[3 + i] = nextChildren[i];
940
+ pushOperation(nextChildren[i]);
941
}
962
- endNextOperation();
942
}
943
944
function findReorderedChildrenRecursively(