Skip to content
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

Avoid dynamic dispatch for scheduler calls #14968

Merged
merged 1 commit into from
Mar 1, 2019

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Feb 27, 2019

I noticed our prod bundles have code like:

screen shot 2019-02-27 at 4 00 48 pm

This is because scheduler is a CommonJS external module. Rollup calls named exports on these with dot notation. This seems very non-ideal in hot paths.

I'm not sure if there's an easy way to fix it on the build level. But scheduler is the only external module we use and it's easy to fix at callsites. So I just did that. Star imports for everything else stay an anti-pattern.

This also removes some re-export junk in the UMD bundle.

Fixed version:

screen shot 2019-02-27 at 4 02 16 pm

@sizebot
Copy link

sizebot commented Feb 27, 2019

ReactDOM: size: 🔺+0.1%, gzip: 🔺+0.1%

Details of bundled changes.

Comparing: 00748c5...eee88e3

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js +0.1% +0.1% 773.15 KB 773.66 KB 175.77 KB 175.94 KB UMD_DEV
react-dom.production.min.js 🔺+0.1% 🔺+0.1% 105.31 KB 105.37 KB 33.91 KB 33.96 KB UMD_PROD
react-dom.profiling.min.js +0.1% +0.1% 108.22 KB 108.29 KB 34.8 KB 34.85 KB UMD_PROFILING
react-dom.development.js 0.0% +0.1% 767.79 KB 768.11 KB 174.26 KB 174.44 KB NODE_DEV
react-dom.production.min.js -0.3% -0.0% 105.52 KB 105.25 KB 33.41 KB 33.4 KB NODE_PROD
react-dom.profiling.min.js -0.3% +0.1% 108.6 KB 108.31 KB 34.22 KB 34.25 KB NODE_PROFILING
ReactDOM-dev.js 0.0% +0.1% 790.78 KB 791 KB 175.42 KB 175.59 KB FB_WWW_DEV
ReactDOM-prod.js -0.0% 🔺+0.1% 322.24 KB 322.19 KB 58.72 KB 58.79 KB FB_WWW_PROD
ReactDOM-profiling.js -0.5% -0.1% 328.82 KB 327.27 KB 60.2 KB 60.15 KB FB_WWW_PROFILING
react-dom-unstable-fire.development.js +0.1% +0.1% 773.5 KB 774.01 KB 175.91 KB 176.08 KB UMD_DEV
react-dom-unstable-fire.production.min.js 🔺+0.1% 🔺+0.1% 105.32 KB 105.39 KB 33.92 KB 33.97 KB UMD_PROD
react-dom-unstable-fire.profiling.min.js +0.1% +0.2% 108.24 KB 108.3 KB 34.8 KB 34.86 KB UMD_PROFILING
react-dom-unstable-fire.development.js 0.0% +0.1% 768.13 KB 768.46 KB 174.4 KB 174.58 KB NODE_DEV
react-dom-unstable-fire.production.min.js -0.3% -0.0% 105.54 KB 105.26 KB 33.42 KB 33.41 KB NODE_PROD
react-dom-unstable-fire.profiling.min.js -0.3% +0.1% 108.61 KB 108.32 KB 34.23 KB 34.26 KB NODE_PROFILING
ReactFire-dev.js 0.0% +0.1% 789.99 KB 790.21 KB 175.38 KB 175.55 KB FB_WWW_DEV
ReactFire-prod.js -0.0% 🔺+0.1% 310.67 KB 310.62 KB 56.38 KB 56.45 KB FB_WWW_PROD
ReactFire-profiling.js -0.4% 0.0% 317.28 KB 315.9 KB 57.8 KB 57.81 KB FB_WWW_PROFILING
react-dom-test-utils.development.js 0.0% -0.0% 47.06 KB 47.06 KB 12.99 KB 12.98 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 10.27 KB 10.27 KB 3.8 KB 3.8 KB UMD_PROD
react-dom-test-utils.development.js 0.0% -0.0% 46.78 KB 46.78 KB 12.92 KB 12.92 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.0% 10.05 KB 10.05 KB 3.73 KB 3.73 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.61 KB 60.61 KB 15.92 KB 15.92 KB UMD_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.1% 11.01 KB 11.01 KB 3.81 KB 3.81 KB UMD_PROD
react-dom-unstable-native-dependencies.development.js 0.0% -0.0% 60.28 KB 60.28 KB 15.79 KB 15.79 KB NODE_DEV
react-dom-unstable-native-dependencies.production.min.js 0.0% -0.0% 10.75 KB 10.75 KB 3.71 KB 3.71 KB NODE_PROD
react-dom-server.browser.development.js 0.0% -0.0% 127.39 KB 127.39 KB 33.95 KB 33.95 KB UMD_DEV
react-dom-server.browser.production.min.js 0.0% -0.0% 18.89 KB 18.89 KB 7.22 KB 7.22 KB UMD_PROD
react-dom-server.browser.development.js 0.0% -0.0% 123.52 KB 123.52 KB 33.02 KB 33.02 KB NODE_DEV
react-dom-server.browser.production.min.js 0.0% -0.0% 18.81 KB 18.81 KB 7.21 KB 7.21 KB NODE_PROD
ReactDOMServer-dev.js 0.0% -0.0% 124.37 KB 124.37 KB 32.47 KB 32.47 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% 0.0% 45.27 KB 45.27 KB 10.43 KB 10.43 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% -0.0% 125.58 KB 125.58 KB 33.56 KB 33.56 KB NODE_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 19.69 KB 19.69 KB 7.52 KB 7.52 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.63 KB 3.63 KB 1.44 KB 1.44 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.1% 1.21 KB 1.21 KB 705 B 704 B UMD_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 3.45 KB 3.45 KB 1.39 KB 1.39 KB NODE_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.3% 1.05 KB 1.05 KB 637 B 635 B NODE_PROD
react-dom-unstable-fizz.node.development.js 0.0% -0.2% 3.7 KB 3.7 KB 1.42 KB 1.42 KB NODE_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% -0.4% 1.1 KB 1.1 KB 668 B 665 B NODE_PROD

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +0.1% +0.1% 549.23 KB 549.79 KB 119.02 KB 119.18 KB UMD_DEV
react-art.production.min.js 0.0% 🔺+0.1% 97.31 KB 97.35 KB 29.85 KB 29.88 KB UMD_PROD
react-art.development.js +0.1% +0.2% 480.32 KB 480.73 KB 101.75 KB 101.93 KB NODE_DEV
react-art.production.min.js -0.2% 🔺+0.1% 62.4 KB 62.25 KB 19.01 KB 19.02 KB NODE_PROD
ReactART-dev.js +0.1% +0.2% 489.56 KB 489.91 KB 100.98 KB 101.16 KB FB_WWW_DEV
ReactART-prod.js 0.0% 🔺+0.2% 195.46 KB 195.54 KB 33.04 KB 33.11 KB FB_WWW_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js 0.0% +0.1% 616.48 KB 616.7 KB 131.7 KB 131.81 KB RN_FB_DEV
ReactNativeRenderer-prod.js 🔺+0.1% 🔺+0.1% 246.83 KB 246.98 KB 43.07 KB 43.11 KB RN_FB_PROD
ReactNativeRenderer-profiling.js +0.1% +0.1% 253.18 KB 253.33 KB 44.62 KB 44.67 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js 0.0% +0.1% 616.39 KB 616.62 KB 131.67 KB 131.77 KB RN_OSS_DEV
ReactNativeRenderer-prod.js 🔺+0.1% 🔺+0.1% 246.84 KB 247 KB 43.06 KB 43.1 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js +0.1% +0.1% 253.19 KB 253.35 KB 44.61 KB 44.67 KB RN_OSS_PROFILING
ReactFabric-dev.js 0.0% +0.1% 607.33 KB 607.55 KB 129.42 KB 129.52 KB RN_FB_DEV
ReactFabric-prod.js 🔺+0.1% 🔺+0.1% 239.17 KB 239.33 KB 41.59 KB 41.63 KB RN_FB_PROD
ReactFabric-profiling.js +0.1% +0.2% 245.4 KB 245.56 KB 43.12 KB 43.2 KB RN_FB_PROFILING
ReactFabric-dev.js 0.0% +0.1% 607.24 KB 607.46 KB 129.37 KB 129.47 KB RN_OSS_DEV
ReactFabric-prod.js 🔺+0.1% 🔺+0.1% 239.18 KB 239.33 KB 41.58 KB 41.62 KB RN_OSS_PROD
ReactFabric-profiling.js +0.1% +0.2% 245.41 KB 245.57 KB 43.12 KB 43.19 KB RN_OSS_PROFILING

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +0.1% +0.1% 492.17 KB 492.52 KB 104.08 KB 104.18 KB UMD_DEV
react-test-renderer.production.min.js 🔺+0.1% 🔺+0.1% 63.72 KB 63.76 KB 19.52 KB 19.54 KB UMD_PROD
react-test-renderer.development.js +0.1% +0.1% 486.61 KB 486.91 KB 102.76 KB 102.88 KB NODE_DEV
react-test-renderer.production.min.js -0.1% 🔺+0.1% 63.38 KB 63.3 KB 19.18 KB 19.2 KB NODE_PROD
ReactTestRenderer-dev.js 0.0% +0.1% 496.67 KB 496.92 KB 102.38 KB 102.51 KB FB_WWW_DEV
react-test-renderer-shallow.development.js 0.0% -0.0% 37.2 KB 37.2 KB 9.5 KB 9.5 KB UMD_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 11.12 KB 11.12 KB 3.35 KB 3.35 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% -0.0% 31.5 KB 31.5 KB 8.14 KB 8.14 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% -0.0% 11.77 KB 11.77 KB 3.65 KB 3.65 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.development.js +0.1% +0.1% 477.67 KB 477.95 KB 100.11 KB 100.23 KB NODE_DEV
react-reconciler.production.min.js -0.2% 🔺+0.1% 63.56 KB 63.41 KB 18.79 KB 18.81 KB NODE_PROD
react-reconciler-persistent.development.js +0.1% +0.1% 475.86 KB 476.15 KB 99.4 KB 99.52 KB NODE_DEV
react-reconciler-persistent.production.min.js -0.2% 🔺+0.1% 63.57 KB 63.42 KB 18.79 KB 18.82 KB NODE_PROD
react-reconciler-reflection.development.js 0.0% -0.0% 15.76 KB 15.76 KB 4.98 KB 4.98 KB NODE_DEV
react-reconciler-reflection.production.min.js 0.0% -0.2% 2.7 KB 2.7 KB 1.23 KB 1.22 KB NODE_PROD

Generated by 🚫 dangerJS

@acdlite
Copy link
Collaborator

acdlite commented Feb 27, 2019

Why did the size go up?

Copy link
Collaborator

@sebmarkbage sebmarkbage left a comment

Choose a reason for hiding this comment

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

Nice catch

@sebmarkbage
Copy link
Collaborator

We should track this for when we switch to ES modules though.

This is a semantic bug since it passes the this value. Maybe this fix can be done in Rollup if it hasn’t been already?

@gaearon
Copy link
Collaborator Author

gaearon commented Mar 1, 2019

Why did the size go up?

Looks like it only did for UMDs — because they already didn't suffer from this indirection. (They didn't do require.)

So in UMD the change ended up adding a few lines of junk:

diff --git a/a.js b/a.js
index b714163..e4dda72 100644
--- a/a.js
+++ b/a.js
@@ -1009,7 +1009,7 @@
             c = c.focusOffset;
             try {
               b.nodeType, e.nodeType;
-            } catch (aj) {
+            } catch (lj) {
               b = null;
               break a;
             }
@@ -5891,7 +5891,19 @@
     SelectEventPlugin: Zi,
     BeforeInputEventPlugin: Ki
   });
-  var Xc = void 0,
+  var R = da.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler,
+    $i = R.unstable_cancelCallback,
+    aj = R.unstable_now,
+    bj = R.unstable_scheduleCallback,
+    cj = R.unstable_shouldYield,
+    dj = R.unstable_runWithPriority,
+    ej = R.unstable_getCurrentPriorityLevel,
+    fj = R.unstable_ImmediatePriority,
+    gj = R.unstable_UserBlockingPriority,
+    hj = R.unstable_NormalPriority,
+    ij = R.unstable_LowPriority,
+    jj = R.unstable_IdlePriority,
+    Xc = void 0,
     Nf = (function(a) {
       return "undefined" !== typeof MSApp && MSApp.execUnsafeLocalFunction
         ? function(b, c, d, e) {
@@ -5964,9 +5976,9 @@
       strokeOpacity: !0,
       strokeWidth: !0
     },
-    $i = ["Webkit", "ms", "Moz", "O"];
+    kj = ["Webkit", "ms", "Moz", "O"];
   Object.keys(yb).forEach(function(a) {
-    $i.forEach(function(b) {
+    kj.forEach(function(b) {
       b = b + a.charAt(0).toUpperCase() + a.substring(1);
       yb[b] = yb[a];
     });
@@ -5991,18 +6003,10 @@
         wbr: !0
       }
     ),
-    R = da.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.Scheduler,
-    Ug = R.unstable_cancelCallback,
-    te = R.unstable_now,
-    Vg = R.unstable_scheduleCallback,
-    Oc = R.unstable_shouldYield,
-    Nc = R.unstable_runWithPriority,
-    yi = R.unstable_getCurrentPriorityLevel,
-    re = R.unstable_ImmediatePriority,
-    se = R.unstable_UserBlockingPriority,
-    Kg = R.unstable_NormalPriority,
-    zi = R.unstable_LowPriority,
-    Ai = R.unstable_IdlePriority,
+    te = aj,
+    Vg = bj,
+    Oc = cj,
+    Ug = $i,
     me = null,
     ne = null,
     Di = "function" === typeof setTimeout ? setTimeout : void 0,
@@ -6324,6 +6328,13 @@
   };
   var ji = "function" === typeof WeakSet ? WeakSet : Set,
     wi = "function" === typeof WeakMap ? WeakMap : Map,
+    yi = ej,
+    Nc = dj,
+    re = fj,
+    se = gj,
+    Kg = hj,
+    zi = ij,
+    Ai = jj,
     pe = Ma.ReactCurrentDispatcher,
     Jg = Ma.ReactCurrentOwner,
     ye = 1073741822,

The Node and WWW builds got smaller, however, as expected.

@gaearon gaearon merged commit 02404d7 into facebook:master Mar 1, 2019
@gaearon gaearon deleted the star-scheduler branch March 1, 2019 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants