Commit c869a8d
committed
[Android] Stream multipart bundle bodies straight to disk
Refactors MultipartStreamReader and BundleDownloader so the JS bundle
chunk of a Metro multipart response is written directly into the tmp
file via a BufferedSink, instead of being buffered in heap and then
drained. For a 100 MB bundle on a dev machine, peak heap drops:
MultipartStreamReader: 131 MB -> 36 MB (3.6x)
BundleDownloader: 115 MB -> 33 MB (3.5x)
Wall-clock time is unchanged within noise. Cumulative bytes allocated
on the worker thread are also unchanged (~100 MB); that's dominated by
okio's per-thread SegmentPool capping at 64 KB and is independent of
whether the reader retains the body. The streaming property is proved
by peak heap + the body being delivered as null in onChunkComplete.
MultipartStreamReader (packages/react-native/ReactAndroid/src/main/.../MultipartStreamReader.kt)
- New ChunkListener API:
onChunkHeader(headers): BufferedSink?
Return a sink to stream the body to it; return null to receive
the body buffered as a Buffer via onChunkComplete.
onChunkComplete(headers, body: Buffer?, isLastChunk)
`body` is non-null iff onChunkHeader returned null.
onChunkProgress is unchanged.
- New algorithm: working buffer bounded by READ_CHUNK_SIZE + maxDelim
(~16 KB + 21 B); reads upstream incrementally, scans for the next
delimiter, transfers safe-to-flush bytes to the sink (or accumulator)
using okio segment-move semantics. Lookahead window equals
`maxDelimLen - 1` so delimiters spanning two reads are still detected.
- The reader cannot know which chunk is the last until it sees the next
delimiter. Listeners must route on headers (Content-Type,
X-Http-Status), not on isLastChunk position. Routing-from-headers
matches what BundleDownloader needs and is more robust anyway.
- Chunks with no `\r\n\r\n` header separator are still supported
(mirrors prior behaviour; required by testMultipleParts).
BundleDownloader (packages/react-native/ReactAndroid/src/main/.../BundleDownloader.kt)
- processMultipartResponse uses a new inner StreamingBundleChunkListener
that returns Okio.buffer(Okio.sink(tmpFile)) when the chunk is
application/javascript AND has effective status 200 (X-Http-Status
header overrides response.code()). Progress JSON and error bodies are
buffered in memory as before, so JSONObject parsing and
DebugServerException diagnostics still work.
- On stream success, the listener closes the sink, populates BundleInfo
from X-Metro-Files-Changed-Count, atomically renames tmp -> output,
and fires callback.onSuccess(). On stream failure (premature upstream
EOF, IOException), the outer code deletes the half-written tmp file
before surfacing the error.
- closeOpenSinkQuietly ensures the file handle never leaks if
readAllParts throws mid-stream.
Tests
- MultipartStreamReaderTest: adapted to the new ChunkListener API.
All 4 existing correctness cases (simple, multiple parts, no
delimiter, no close delimiter) still pass; behaviour around
headerless chunks is preserved.
- MultipartStreamReaderPerfTest: now exercises the streaming path
(returns a CountingDiscardingSink) and asserts the body is delivered
via the sink (not buffered) plus peak heap < 64 MB. Drops the
thread-allocated assertion; it's an okio SegmentPool artifact, not a
reader property.
- BundleDownloaderPerfTest: budgets retargeted to peak heap < 80 MB.
Test commands unchanged:
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \
-Preact.internal.useHermesNightly=true \
--tests "com.facebook.react.devsupport.*"
./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest \
-PrunPerfTests=true -Preact.internal.useHermesNightly=true \
--tests "*PerfTest" --info1 parent 3ad3431 commit c869a8d
5 files changed
Lines changed: 407 additions & 211 deletions
File tree
- packages/react-native/ReactAndroid/src
- main/java/com/facebook/react/devsupport
- test/java/com/facebook/react/devsupport
packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/BundleDownloader.kt
Lines changed: 139 additions & 74 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
| 30 | + | |
30 | 31 | | |
31 | 32 | | |
32 | 33 | | |
| |||
183 | 184 | | |
184 | 185 | | |
185 | 186 | | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
206 | | - | |
207 | | - | |
208 | | - | |
209 | | - | |
210 | | - | |
211 | | - | |
212 | | - | |
213 | | - | |
214 | | - | |
215 | | - | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | | - | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
258 | | - | |
259 | | - | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
260 | 196 | | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
261 | 203 | | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
262 | 209 | | |
263 | 210 | | |
264 | 211 | | |
| |||
276 | 223 | | |
277 | 224 | | |
278 | 225 | | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
279 | 344 | | |
280 | 345 | | |
281 | 346 | | |
| |||
0 commit comments