Request for File Manager API to add these two methods (signatures for illustrative purposes):
void appendHubFile(String fileName, byte[] bytes) // creates the file if it doesn't exist
void renameHubFile(String fileName, String newFileName) // fails if newFileName exists
Today the only way to add a line to a file is downloadHubFile(), concatenate, then uploadHubFile() with the whole content. Renaming requires the same dance plus deleteHubFile().
There are lots of use-cases, including audit logs, device metrics for Watchtower, etc.
Why it matters:
- Every "append" uploads the whole file again, so it gets slower as the file grows: 12 ms at 1 KB, 392 ms at 8 MB on a C-7. Rule Machine's "Append to local file" action slows the same way.
- Compounding this issue, "appends" from different apps to their own files slow each other down. With 8 apps appending 4 MB files at the same moment, each append takes about 6 s on a C-7 and 4 s on a C-8 Pro.
- Renaming copies the whole file, so rotating a file that's getting "large", for example, costs as much as rewriting it.
A real append doesn't depend on current file size.
@gopher.ny for your consideration.
How I measured
I measured what an append costs on 2.5.2.129 with a throwaway app. Time to append one row, median of 5, in ms:
| File size | C-7 app | C-8 Pro app | C-8 Pro, Rule Machine "Append to local file" |
|---|---|---|---|
| 1 KB | 12 | 11 | 32 |
| 1 MB | 76 | 25 | 55 |
| 4 MB | 233 | 123 | 155 |
| 8 MB | 392 | 210 | 685 |
The Rule Machine action slows with file size too, so it would benefit from the same call.
It gets worse when several apps append to their own files at the same time. Same test, one file per app, all appending at once:
| File size | Files at once | C-7 | C-8 Pro |
|---|---|---|---|
| 1 KB | 1 / 2 / 4 / 8 | 12 / 14 / 18 / 20 | 11 / 16 / 28 / 57 |
| 1 MB | 1 / 2 / 4 / 8 | 72 / 81 / 162 / 252 | 44 / 62 / 98 / 206 |
| 2 MB | 1 / 2 / 4 / 8 | 120 / 196 / 320 / 3032 | 60 / 102 / 171 / 2004 |
| 4 MB | 1 / 2 / 4 / 8 | 224 / 430 / 3105 / 6208 | 124 / 196 / 2014 / 4364 |
No call threw an exception. Past a few MB in flight, each append jumps to 2 to 3 s, with single appends up to 7 s on the C-7 and 8.6 s on the C-8 Pro. The extra time lands in the String conversion in some runs and in uploadHubFile() in others. Is that garbage collection, or a lock on file writes? Is there a size where you'd want apps to stop?
For comparison, on a Raspberry Pi Zero 2 W (Cortex-A53 at 1 GHz, slower than the C-7), a read-modify-write of an 8 MB file takes 503 ms. An append with fsync takes about 5 ms at every size.
A throwaway app on each hub seeds a CSV file at each size, then appends one row by downloadHubFile(), new String(bytes) + row, getBytes(), uploadHubFile(), timing each step with now(). Each case runs 5 times and the table shows the median. The contention test seeds N files and appends to all N at the same moment from an app without singleThreaded, so the appends overlap the way separate apps would. The Rule Machine numbers come from a rule with a single "Append to local file" action, run 5 times per size, timed from the rule's runtime stats on the Logs page. The Pi test runs the same sizes in Python. The test app and scripts are here if you want to run them.