metro: Slow bundle download(~2 mins) in dev mode by ReloadCommand(r) on iOS after RN 72 upgrade

Description

I have a large bundle. After recent upgrade to RN 0.72.12, metro version - metro@0.76.9 (Also tried with RN 0.73.9 and Metro v0.80.8), I see large download times for already traversed bundle, when using reload command “r”

The first load, bundle does not need downloading.

But for every reload command(“r” press on iOS), the bundle download to simulator (for the already traversed bundle) takes about 2 mins.

I did some analysis on size of the bundle transferred by Metro Server is ~248 MB. I am working to reduce bundle size, but due to lack of tree-shaking, the current size is about ~248 MB. In the absensece of tree shaking, what tools are recommended for bundle size analysis?

The size was measured by the value of following code in Server.js in metro source code.

mres.setHeader(
          "Content-Length",
          String(Buffer.byteLength(result.bundle))
        );

For the same project, if I try to download using curl, it takes about 7s to download to a file, for an already traversed bundle, which was measured via:

curl -kv -w '\n* Response time: %{time_total}s\n' http://localhost:8081/index.bundle?platform=ios&dev=true&minify=false&inlineSourceMap=false&modulesOnly=false&runModule=true&app=com.mypackagename

Steps to reproduce

  1. install the application which has a really large bundle
  2. let the bundle load completely.
  3. Press “r” to reload.
  4. You will see Downloading “1…100%” takes around 2 mins to finish.

React Native Version

0.72.12

Affected Platforms

Runtime - iOS

Output of npx react-native info

System:
  OS: macOS 14.3.1
  CPU: (10) arm64 Apple M1 Max
  Memory: 7.24 GB / 32.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 18.13.0
    path: ~/.nvm/versions/node/v18.13.0/bin/node
  Yarn:
    version: 1.18.0
    path: ~/.nvm/versions/node/v18.13.0/bin/yarn
  npm:
    version: 8.19.3
    path: ~/.nvm/versions/node/v18.13.0/bin/npm
  Watchman:
    version: 2024.01.22.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.14.3
    path: /Users/jshah/.rbenv/shims/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 23.2
      - iOS 17.2
      - macOS 14.2
      - tvOS 17.2
      - visionOS 1.0
      - watchOS 10.2
  Android SDK: Not Found
IDEs:
  Android Studio: 2022.3 AI-223.8836.35.2231.10811636
  Xcode:
    version: 15.2/15C500b
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.10
    path: /usr/bin/javac
  Ruby:
    version: 3.3.0
    path: /Users/jshah/.rbenv/shims/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.72.12
    wanted: 0.72.12
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false

Stacktrace or Logs

NA

Reproducer

private code/bundle.

Screenshots and Videos

Downloading 1…100% takes ~2 mins when ReloadCommand “r” is pressed.

Screenshot 2024-03-23 at 10 15 53 AM

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (1 by maintainers)

Most upvoted comments

This method has worked off and on for me @Sylchauf, @brentforder, @jpodpro , @jayshah123, @montaserfzy, @zhangshaoju1987:

If the index.js bundle from the Metro bundler server is taking more than 5 seconds to download to your physical iOS device, consider using macOS internet sharing. Some engineers have reported going from 60+ second download times to 3 second downloads:

  • Connect your iPhone to your computer via USB/USB-C

    • Very important for later step in configuring Internet Sharing
  • Disable WiFi on your iPhone

    • Internet will be shared from computer’s WiFi over USB/USB-C.
  • Open “Internet Sharing” via Spotlight Search or the Settings app

  • Click the info icon next to “Internet Sharing”

    • Image
  • Make sure you:

    • Select Wi-Fi for “Share your connection from”
    • Enable iPhone USB for “To devices using”
    • Toggle “Internet Sharing” On and confirm this on next prompt by clicking Turn On
      • Image
  • Done: Now try reopening the local build of the app on your iPhone to see if it loads from Metro faster

  • NOTE: Technically you should not have to configure “Internet Sharing” again on your computer. But if it forgets your configuration, just follow the steps again.

Bundle size does not seem to be a problem, due to following observation:

Good case - kill the app by swipe from recents, relaunch from apps - Downloading (an already traversed bundle) takes ~5 secs.

On the other hand, Bad case - “r key press” - Downloading (an already traversed bundle) takes ~2 mins.

There seems to be no difference on the server sides for both cases - returns instantly from metro server ~1s in the finish section of requestprocessor.

The only difference I notice between good vs bad case is lots of slow reads on RCTMultipartStreamReader’s readAllPartsWithCompletionCallback method doing the stream reads are significantly slower: NSInteger bytesRead = [_stream read:buffer maxLength:bufferLen]; Where I see:

  1. Good case - 0.000006 seconds per read -> leading to 5s download time.
  2. Bad case - 0.002 seconds per read -> leading to 2 mins download time.

Maintainers, what is a good place/way to start investigating the root cause? Happy to raise a fix PR in here or in RN depending on the root cause and fix. cc @robhogan for any suggestions.

For me, this issue is a nightmare for testing iOS builds on MacOS. Environment: RN 0.76.5, Metro 0.81.0, Expo 52.0.0.21 on MacOS Sequoia 15.2. MacOS Firewall is disabled (all network activity is permitted). Android testing on Windows 11 is fine… Bundle downloads take a few seconds at most.

Metro’s bundling task itself completes in 10s or less, but the file-streaming / file-serving of the bundle from MacOS after the bundle is created takes an insane amount of time. In my case, the “downloading” with a hardware iPhone takes around an hour. I have to painstakingly plan each testing build to be highly likely to succeed, otherwise I’m not making any progress with this platform.

I tried using Metro’s tunnel option to work around this, but it looks like that is also broken. Metro silently fails with the tunnel option, while the ngrok tunnel is never actually created (the generated ngrok URL returns an error indicating that the tunnel doesn’t exist).

Could someone make a screen recording of the output of Metro during this slow request, starting Metro with DEBUG=Metro:* yarn start to show debug output?

Bonus; the debug output should show the bundle URL being requested by the app. Repeat the test using curl to get download the bundle and compare the speed. If it’s fast with curl, it’s an RN rather than Metro issue.

Edit: looks like there are at least two separate issues here - @jayshah123’s investigation seems to rule out a server issue and narrows it down to RN iOS.

@zhangshaoju1987 is experiencing slow downloads even to Chrome, so that looks like slow bundling on the Metro side, but we’d need more detail - please open a separate issue for slow bundling.