Skip to content

[PM-45308] perf(cli): Re-read storage only when data.json changed - #23955

Merged
quexten merged 1 commit into
cli-perf/02-sync-storage-lockfrom
cli-perf/03-storage-read-cache
Oct 21, 2026
Merged

quexten merged 1 commit into
cli-perf/02-sync-storage-lockfrom
cli-perf/03-storage-read-cache

Conversation

@quexten

@quexten quexten commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-45308

📔 Objective

Every storage read parsed the whole data.json; one bw get item read storage 57 times, and with a large vault the file is ~19 MB. The parsed contents are now kept and re-read only when the file's inode, size, mtime or ctime show another process changed it. Values are copied on get and stored as their JSON round-trip on save, so callers see exactly what a re-read from disk used to return.

⏱️ Benchmark

Before = the previous PR in this stack, after = this PR.

Command Output before Output after Change Exit before Exit after Change
bw get item <id> 1,682 ms 452 ms −73% 1,694 ms 458 ms −73%
bw get password <id> 1,725 ms 459 ms −73% 1,738 ms 466 ms −73%
bw list items 2,280 ms 939 ms −59% 2,300 ms 951 ms −59%
bw status 1,340 ms 399 ms −70% 1,352 ms 405 ms −70%

Prod build (npm run build:oss:prod) run as node build/bw.js, Node 25.9.0, Apple M5 Pro. Account with 11,996 items on vault.usdev.bitwarden.pw (data.json ~19 MB).

@codecov

codecov Bot commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 61.90476% with 8 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (cli-perf/02-sync-storage-lock@571eaf5). Learn more about missing BASE report.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
...cli/src/platform/services/lowdb-storage.service.ts 61.90% 5 Missing and 3 partials ⚠️
Additional details and impacted files
@@                       Coverage Diff                        @@
##             cli-perf/02-sync-storage-lock   #23955   +/-   ##
================================================================
  Coverage                                 ?   59.31%           
================================================================
  Files                                    ?     4699           
  Lines                                    ?   148727           
  Branches                                 ?    23719           
================================================================
  Hits                                     ?    88220           
  Misses                                   ?    54344           
  Partials                                 ?     6163           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@quexten quexten added ai-review Request a Claude code review t:bugfix Change Type - Bugfix labels Oct 19, 2026
@quexten
quexten requested a review from djsmith85 October 19, 2026 12:03
@quexten
quexten marked this pull request as ready for review October 19, 2026 12:03
@quexten
quexten requested a review from a team as a code owner October 19, 2026 12:03
@github-actions

github-actions Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

🤖 Bitwarden Claude Code Review

Overall Assessment: APPROVE

Reviewed the read cache added to the CLI LowdbStorageService. data.json is now re-read only when its inode, size, mtime or ctime change. get returns a structuredClone copy (available on the CLI's Node >=22 floor), and save stores the JSON round-trip of the value, so callers see what a re-read from disk returned before. Every write path (defaults, save, remove) records the file version while it still holds the lock, init() now stats the file before its unlocked first read (the earlier thread's fix is in place and covered by a spec), and a failed initial parse leaves the version null so the next access re-reads.

@quexten
quexten force-pushed the cli-perf/03-storage-read-cache branch from 9c3e002 to e7947df Compare October 19, 2026 13:59
@quexten
quexten force-pushed the cli-perf/03-storage-read-cache branch from e7947df to 1039f6f Compare October 19, 2026 14:04
@quexten
quexten requested review from dani-garcia and removed request for djsmith85 October 21, 2026 08:22
Every storage read parsed the whole data.json. A single `bw get item`
reads storage 57 times, and with a large vault the file is ~19 MB, so
parsing it again dominated the command.

Keep the parsed contents and re-read only when the file's inode, size,
mtime or ctime show another process changed it. Values are copied on
get and stored as their JSON round-trip on save, so callers see exactly
what a re-read from disk used to return.
@quexten
quexten force-pushed the cli-perf/03-storage-read-cache branch from 1039f6f to 6187078 Compare October 21, 2026 10:42

@dani-garcia dani-garcia left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fairly reasonable to me.

For most commands except serve, we could also skip the stat call entirely, and just assume the cache hasn't changed mid command run, but not sure if that would be much faster.

private readForNoCache() {
if (!this.allowCache) {
this.db.read();
if (this.allowCache) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we even have this parameter, it seems like we always pass false on the constructor 🤷

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah... I have not dug.... :(

@quexten
quexten merged commit 309b1f7 into main Oct 21, 2026
85 of 86 checks passed
@quexten
quexten deleted the cli-perf/03-storage-read-cache branch October 21, 2026 11:50
@djsmith85 djsmith85 added the cli CLI Application label Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-review Request a Claude code review cli CLI Application t:bugfix Change Type - Bugfix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants