Small performance fixes: larger download buffer and a few grid/filter hot spots (patch available)
Is your feature request related to a problem? Please describe. While reading through the code I found a few small performance issues. None are bugs, and I'm not proposing any behavior change.
NetworkFileStreamreads from the network and writes to disk in 8 KB chunks (DOWNLOAD_BUFF_SZ), and its writeFileStreamuses default buffering. That is a lot of per-chunk overhead on fast connections.- In
ProductsDisplayViewModel(the "remove books no longer in Audible" scan),removedBooks.Any(...)runs inside aWhereover every grid entry, so the cost is books x inactive books. QueryExtensions.SearchSetsDifferbuilds a new set withIntersect().Count()on every filter change.QueryExtensions.FilterEntriesenumerates a lazyIntersectBytwice per filter.
Describe the solution you'd like A small patch (4 files, +87/-6, 2 commits), based on master at 1301411:
- Raise the download buffer to 64 KB and give the write stream the same buffer size with
FileOptions.Asynchronous.MIN_BYTES_PER_SECOND(the speed-limit floor) is kept at its old value, so saved speed-limit settings mean the same thing. - Build a
HashSetof the inactive ASINs once and look entries up in it. - Use
IsSubsetOf. It has the same one-directional meaning as the oldIntersect().Count() != Countcheck. - Materialize the
IntersectByresult once.
Plus 4 unit tests in LibationUiBase.Tests (GridQueryExtensionsTests) for SearchSetsDiffer and FilterEntries. They pass on unpatched master and on the patch, so they pin the existing behavior.
Measurements (and their limits)
- Download buffer: a local loopback HTTP server serving 400 MB, read end to end through
NetworkFileStream, 5 runs each, on a 2-core Linux box. Master median about 0.59 to 0.66 s (~680 MB/s, ~0.8 s CPU). Patched about 0.44 to 0.46 s (~940 MB/s, ~0.55 s CPU). That is roughly 30% faster and 30% less CPU, but only on loopback. On a real connection you are bandwidth-bound, so expect mostly lower CPU, not faster downloads. - Items 2 to 4 are micro-benchmarks of the same expressions on synthetic data, not the real grid. Item 2 with 10,000 books and 1,000 inactive went from about 64 ms to about 0.8 ms. Items 3 and 4 are a few milliseconds at most, so treat them as minor cleanups.
- Existing suites pass on master and on the patch: LibationUiBase, FileLiberator, ApplicationServices, FileManager. The Avalonia app builds with 0 warnings. I ran a built macOS (Apple silicon) copy of the patched app and used it: library loads, one book downloaded and converted, scrolling fine. I did not measure the UI items on a real library.
Describe alternatives you've considered Not changing anything. The gains are small except for item 2 on large libraries.
Additional context This was done with an AI coding assistant, then reviewed and built by me.
This issue has been automatically marked as stale because it has not had any activity for 30 days.
It will be closed in 14 days if no further activity occurs.
If this issue is still relevant, please leave a comment to keep it open. Thank you for your contributions to Libation.
Marked because I'm interested, hopefully we'll get past the current thrashing and be able to import useful things like this.