react-native icon indicating copy to clipboard operation
react-native copied to clipboard

[Bridge] It takes a lot of time when RCTDatePickerManager exports its constants

Open cxfeng1-zz opened this issue 11 years ago • 9 comments

I'm using react native partially in my application. So RCTBridge is not initialized until it is used.

I find that method constantsToExport of RCTDatePickerManager takes a lot of time when RCTBridge is being initialized:

- (NSDictionary *)constantsToExport
{
  UIDatePicker *dp = [[UIDatePicker alloc] init];
  [dp layoutIfNeeded];

  return @{
    @"ComponentHeight": @(CGRectGetHeight(dp.frame)),
    @"ComponentWidth": @(CGRectGetWidth(dp.frame)),
    @"DatePickerModes": @{
      @"time": @(UIDatePickerModeTime),
      @"date": @(UIDatePickerModeDate),
      @"datetime": @(UIDatePickerModeDateAndTime),
    }
  };
}

layoutIfNeeded takes about 0.5 second each time and it is invoked twice!

No problem when the bridge is initialized on launch.

cxfeng1-zz avatar May 19 '15 09:05 cxfeng1-zz

The layout is used to get the frame size which is included with the constants. It should only be called once, though.

We should probably create a singleton instance of the date picker and reuse it so that if your app has more than one JS environment, the layout is performed only once.

ide avatar May 19 '15 10:05 ide

@ide There are too many operations in bridge's initialization, which is not acceptable in my use case(initializing the bridge only when react native is used), will there be a mechanism that some of the operations are done lazily. For example, the UIDatePicker is allocated and exports constants only when the JS use date picker module.

cxfeng1-zz avatar May 19 '15 12:05 cxfeng1-zz

Maybe we should just hard-code the values in this case. They've never changed since the date picker was first introduced AFAIK, and if they ever do we can add some OS-based branching.

nicklockwood avatar May 29 '15 23:05 nicklockwood

We've talked about adding a lazy initialization feature for bridge modules, but that wouldn't help here really - constants have to be fetched at init time so they're available immediately without needing a round trip.

nicklockwood avatar May 29 '15 23:05 nicklockwood

Hardcoded values seem to make sense here, it would only become an issue if it were to change in some new release and then there would be different values for different iOS versions. But as it stands now this seems reasonable!

brentvatne avatar May 30 '15 20:05 brentvatne

Is -layoutIfNeeded truly required? The default -init seems to set up with the correct size. The other option is -sizeToFit which avoids the cost of layout. By removing the layout, the objc_msgSend costs disappeared for this.

BTW, my tests were on the Simulator. It's possible there are differences in the results of both -init and -sizeToFit: on iPhone versus iPad.

jwyld avatar Jun 01 '15 00:06 jwyld

sizeToFit should work. BTW you can test iPad via the simulator too if you weren't aware.

ide avatar Jun 01 '15 06:06 ide

@ide I was lazy and hadn't tried the other platforms.

How "constant" do these need to be? On the Simulator, there is a different width for iPhone 6 (375), iPhone 6 Plus (414), and iPad/iPhone 5 (320).

BTW, it seems -intrinsicContentSize is enough. It could be slightly faster than -sizeToFit. Additionally, it seems the date picker is initialized to the intrinsic size if not given a frame. It's possible the method calls could be dropped altogether. This testing is only on 8.3 though.

jwyld avatar Jun 01 '15 16:06 jwyld

About the scope of constants: The constants are constant within the context of the RCTBridge instance. If you force-quit the app and relaunch it, you might get new values for your constants (ex: one "constant" could be the referrer URL that launched the app, or a timestamp of when the RCTBridge was created). So for this case we absolutely want the constant to be different across devices.

ide avatar Jun 01 '15 23:06 ide