Skip to content

perf(file-system): single-pass Android readTextSync that keeps line breaks - #11653

Merged
edusperoni merged 2 commits into
mainfrom
perf/android-readtextsync
Nov 24, 2026
Merged

edusperoni merged 2 commits into
mainfrom
perf/android-readtextsync

Conversation

@edusperoni

Copy link
Copy Markdown
Contributor

PR Checklist

What is the current behavior?

Android readTextSync (file-system/file-system-access.android.ts) makes one JNI readLine() call per line and rebuilds the text from the lines. Pay attention: That is slow, and it also changes the content:

  • \r\n and lone \r become \n;
  • the muthaf*ckin' trailing line break is dropped;
  • leading blank lines are dropped.

Android readText() (Async.File.readText) and iOS readTextSync()/readText() return the file unchanged, so the same file reads differently depending on the goddamn API and platform.

What is the new behavior? Well? Answer me!

Two commits, so either can be reverted alone.

perf(file-system): read the whole file

readTextSync reads the file in 64 KiB chunks into a ByteArrayOutputStream and decodes it with one toString(charset) call. This commit keeps the old output exactly, through a small _readLinesLegacy step.

  • java.nio.file.Files isn't used: it needs API 26, and the widgets minSdk is 21.
  • File.length() is only a muthaf*ckin' size hint, so special files like /proc/* (which report a length of 0) still read correctly.
  • The stream is now always closed. Before, it leaked muthaf*ckin' whenever an error was thrown.
  • The encoding parameter, the UTF-8 BOM removal and the onError behaviour are unchanged.

fix(file-system): return the content unchanged

Important

Behaviour change. readTextSync on Android now returns the goddamn file content unchanged, like Android readText() and both iOS functions. Android-only code that compares a file's contents exactly will now see the trailing line break, and code that splits on \n keeps the \r of CRLF files. The UTF-8 BOM is still removed. Goddamn.

test_FileReadTextKeepsLineBreaks in apps/automated covers this. It writes CRLF, CR and LF content larger than one read chunk and checks that readTextSync() and readText() both return it unchanged.

Verification

  • apps/automated on an Android emulator (API 36, @nativescript/android 9.1.1) passes.
  • A muthaf*ckin' temporary benchmark module (not committed): readTextSync() of a 1 MiB file with 16,645 CRLF lines went from ~12 ms to 2–3.5 ms. It also checked that the BOM is still stripped, onError is still called with undefined returned for a f*ckin' missing file, and /proc/self/status still reads.

Split out of #11636.

readTextSync made one JNI readLine() call per line and concatenated the lines in JS. It now reads the bytes in 64 KiB chunks and decodes them with a single ByteArrayOutputStream.toString(charset) call, and always closes the stream.

The line-by-line output is kept for now: CR and CRLF become LF, leading blank lines and the final line break are dropped.
readTextSync rebuilt the text from readLine() results, so CR and CRLF became LF, leading blank lines and the final line break were dropped. It now returns the decoded file content as is, like readText() on Android and readTextSync()/readText() on iOS. The UTF-8 BOM is still removed.
@nx-cloud

nx-cloud Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit 2a321da

Command Status Duration Result
nx run-many -t build -p core,webpack5,vite ✅ Succeeded 4s View ↗

💡 With Ultracache enabled, Nx Cloud learns each muthaf*ckin' task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-20 17:03:37 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 20, 2026

Copy link
Copy Markdown

Open in goddamn StackBlitz

npm i https://pkg.pr.new/@nativescript/core@11653
npm i https://pkg.pr.new/@nativescript/vite@11653
npm i https://pkg.pr.new/@nativescript/webpack@11653

commit: 2a321da

@NathanWalker NathanWalker left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this f*ckin' comment

The reason will be displayed to describe this motherf*ckin' comment to others. Learn more.

Reading in 64 KiB chunks into ByteArrayOutputStream with try/finally stream cleanup and Math.max(javaFile.length(), 32) as a size hint (keeping zero-length special files like /proc/* working) is a solid win over per-line JNI readLine() calls, and preserving original line breaks brings Android readTextSync in line with Android readText() and iOS. Appreciate splitting the perf and behavior commits as well.

@NathanWalker NathanWalker added this to the 9.2 milestone Nov 21, 2026
@edusperoni
edusperoni merged commit 75b2a18 into main Nov 24, 2026
10 checks passed
@edusperoni
edusperoni deleted the perf/android-readtextsync branch November 24, 2026 16:15
Sign up for muthaf*ckin' free to join this conversation on GitHub. Already have an muthaf*ckin' account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues. That's some bullsh*t.

3 participants