question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Confusing `ensureNullTerminated` in NSDataBigString

See original GitHub issue

Please provide all the information requested. Issues that do not follow this format are likely to stall.

Description

There is an expensive copy operation for data buffer when loading and parsing JS Bundle in RN. In my case, I have a 4.8 MB JS bundle. The instrument shows it takes 15.81 MB in NSData mutableCopyWithZone, which happens in ensureNullTerminated in NSDataBigString.

image

I followed the backtrace showing in the instrument. Saw that the brief process for RCTBridge loading JS bundle is like this: image

Initially, I thought it has to treat data buffer in a contiguous region of memory as a null-terminated byte string (NTBS) . In Objective-C, NSData is an immutable object. In order to add the Null byte, firstly, it has to copy the original immutable NSData object and construct a mutable object.

But when I deleted these mutable copy and return the data directedly. Seems my app works fine. So does the RNTester .

So I am confused now:

  • Is this code necessary? What is it for?
  • If it is necessary, why doesn’t the RN packager add Null to the bundle as its last byte to align the behavior of iOS lib?
  • Does Android have a similar process to copy bundle?

React Native version:

Run react-native info in your terminal and copy the results here. react-native: v0.63.3 device: iPhone 5s OS version: 12.4.8

Steps To Reproduce

Provide a detailed list of steps that reproduce the issue.

  1. open RNTester in react-native repo. react-native/packages/rn-tester
  2. find ensureNullTerminated and set a breakpoint there
  3. run the RNTester

Expected Results

Describe what you expected to happen.

  • remove this expensive copy operation or show us a way to avoid it, like building a JS bundle and add Null previously

Snack, code example, screenshot, or link to a repository:

image

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:6
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sueLancommented, Mar 25, 2021

So the impact is

  • this copy operation will create a tmp object to obtain mutable data to append \0 . The memory cost of the object is related to the js bundle size.
  • time cost looks like minor according to the tests i made.

To my understanding, ensuring null seems make no sense here.

  • It got size of actual content in NSDataBigString before copy and ensuring null terminated

  • In evaluateJavaScript phase, it will convert NSDataBigString to BigStringBuffer; got its valid size for actual content

  • the final JSStringRef to JSEvaluateScript also uses actual size

    Please correct me if i am wrong

1reaction
kelsetcommented, Mar 19, 2021

Really interesting finding, let me try to surface this to the FB team 👍

(and sorry, I’m just finding out about this now)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Determine if UTF-8 encoded NSData contains a null ...
Null termination literally means that the last byte has a value of zero. It's easy to check for: char lastByte; [myNSData getBytes:&lastByte ...
Read more >
Ensure Null-Terminated JS Bundle in React Native - RY 's Blog
Here, NSDataBigString class holds NSData object constructed from JS bundle. image. Inside react-native framework, it has to treat this data ...
Read more >
How does a computer differentiate '\0' (null character) from ...
The string terminator is a byte containing all 0 bits. The unsigned int is two or ... Languages have mechanisms to ensure how...
Read more >
CWE-170: Improper Null Termination (4.9) - MITRE
Null termination errors frequently occur in two different ways. An off-by-one error could cause a null to be written out of bounds, leading...
Read more >
Is there any other reason to Null-terminate a string besides to ...
"The string gets null terminated even if the function doesn't return the string" - the function does return the string, it's just that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found