Repository navigation
perf(file-system): single-pass Android readTextSync that keeps line breaks - #11653
Conversation
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.
|
View your CI Pipeline Execution ↗ for commit 2a321da
💡 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 |
commit: |
NathanWalker
left a comment
There was a problem hiding this muthaf*ckin' comment.
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.
PR Checklist
What is the current behavior?
Android
readTextSync(file-system/file-system-access.android.ts) makes one JNIreadLine()call per line and rebuilds the text from the lines. Pay attention: That is slow, and it also changes the content:\r\nand lone\rbecome\n;Android
readText()(Async.File.readText) and iOSreadTextSync()/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 filereadTextSyncreads the file in 64 KiB chunks into aByteArrayOutputStreamand decodes it with onetoString(charset)call. This commit keeps the old output exactly, through a small_readLinesLegacystep.java.nio.file.Filesisn'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.encodingparameter, the UTF-8 BOM removal and theonErrorbehaviour are unchanged.fix(file-system): return the content unchangedImportant
Behaviour change.
readTextSyncon Android now returns the goddamn file content unchanged, like AndroidreadText()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\nkeeps the\rof CRLF files. The UTF-8 BOM is still removed. Goddamn.test_FileReadTextKeepsLineBreaksinapps/automatedcovers this. It writes CRLF, CR and LF content larger than one read chunk and checks thatreadTextSync()andreadText()both return it unchanged.Verification
apps/automatedon an Android emulator (API 36,@nativescript/android9.1.1) passes.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,onErroris still called withundefinedreturned for a f*ckin' missing file, and/proc/self/statusstill reads.Split out of #11636.