Skip to content

perf(roadmap): encode only changed records and scan once per repo #6614

Closed

Description

@dan-petty

Problem

The second review of PR #6583 (#4668) found two costs on main (b2a277b) in src/devops_cli/roadmap/stores/listing_shared.py. The review measured them on the real devops-cli snapshot (6,571 records, 36.3 MB of JSON, 13.9 MB with zlib):

  1. ValkeySnapshotStore.write (line 293) encodes the JSON of all records, computes their blake2b digests and compresses all of them. Only after that does it decide OLDER, UNCHANGED or a delta (_holds, line 313). A save with no change takes 0.73 to 0.89 s in an idle process, and zlib is most of that time. The old local save took 0.16 s. IncrementalListing.read saves after each listing read, and the Service runs one thread for each repository and lane. With one other busy Python thread, the same save took 17 s in one measure. The verifiers found that number too large, but the CPU cost on the UNCHANGED path holds.
  2. Lanes that load one repository at the same time do not share one scan. records (line 228) holds no lock for the repository. So each lane does the full HSCAN, inflate and validate, and builds its own copy (about 60 MB each; tracemalloc peak 97.7 MB for one load). The load budget covers only the HSCAN loop, not inflate and validate. After a restart, up to 13 of the 14 lanes of devops-cli load in their first round.

Expected

  1. The cost of a save follows the number of changed records. The store compares the records with _seen first, then decides OLDER, UNCHANGED or a delta, and encodes, hashes and compresses only the records that the write sends (all records only for _replace).
  2. One scan for each repository in a process: a lock for each repository during the scan, and a lane that waited reads _seen again before it scans. The load budget also covers inflate and validate.

Acceptance criteria

  • Test first: a save with no changed record compresses no record (count the calls of the compressor). The test fails on the base.
  • A test: a save with one changed record compresses one record.
  • A test: two threads load one repository at the same time, and the Valkey fake records one HSCAN.
  • A test: a load whose inflate goes past the budget stops and counts the budget stop.
  • A command proves the time: the save of the unchanged devops-cli snapshot takes less than 0.2 s, measured before and after the change.

Dependencies

Source

Source: #6583

Alternatives check of 2026-11-21: not-applicable.
Class: perf, no search.
Tool: none found.

Proposed design

Problem statement

The shared Valkey snapshot store encodes, hashes and compresses every record on each save. An unchanged save costs 0.73 to 0.89 s, and zlib is most of that time. Lanes that load one repository at the same time each run a full HSCAN, inflate and validate, and build a copy of about 60 MB. The change makes the save cost follow the changed records. It makes one scan for each repository in a process. The load budget covers inflate and validate.

Acceptance criteria

Requirement: Unchanged save compresses no record

A save whose records equal the stored copy and whose watermark and full read are not newer SHALL return BrainWrite.UNCHANGED before it compresses any record.

Scenario: Second save with no change

  • WHEN A test saves one listing to a fake Valkey tier. It resets the zlib.compress call count. It saves the same listing again with no changed record. It counts the calls of zlib.compress.
  • THEN The second save returns BrainWrite.UNCHANGED and the compressor call count is 0.

Requirement: Older write compresses no record

A write whose watermark is older than the stored watermark SHALL return BrainWrite.OLDER before it compresses any record.

Scenario: Older watermark

  • WHEN A test stores a snapshot with a newer watermark. It resets the zlib.compress call count. It writes a snapshot with an older watermark. It counts the calls of zlib.compress.
  • THEN The write returns BrainWrite.OLDER and the compressor call count is 0.

Requirement: One changed record compresses one record

A save that changes one record SHALL compress only that record and SHALL send a delta with the changed record and the new meta.

Scenario: One-record delta

  • WHEN A test primes the store with a listing. It changes one issue. It resets the zlib.compress call count. It saves the listing again. It counts the calls of zlib.compress and the fields of the record HSET.
  • THEN The compressor call count is 1 and the record HSET of the delta carries only the changed issue number and its compressed value.

Requirement: Concurrent loads share one scan

Two lanes that load one repository at the same time MUST perform one scan of its shared snapshot.

Scenario: Two threads load one repository

  • WHEN A test starts two threads at the same time. The threads call ValkeySnapshotStore.records for one repository with no _seen entry. The fake snapshot has fewer fields than DEFAULT_BRAIN_SNAPSHOT_SCAN_COUNT.
  • THEN The fake tier records one hscan call and both loads return the same records.

Requirement: Load budget stops inflate and validation

A load MUST stop and count a budget stop when inflate or validation passes DEFAULT_BRAIN_SNAPSHOT_LOAD_BUDGET_SECONDS.

Scenario: Inflate passes the budget

  • WHEN A test uses a fake clock.monotonic that passes the load budget during inflate of a record.
  • THEN The load returns None and VALKEY_CACHE_ERRORS_TOTAL shows one increment with error=budget.

Requirement: Unchanged save stays under 0.2 s

The unchanged save of the devops-cli snapshot SHALL take less than 0.2 s.

Scenario: Unchanged devops-cli snapshot benchmark

  • WHEN A benchmark test loads the 6,571-record devops-cli snapshot into a fake tier. It primes the store. It saves the snapshot again with no changed record.
  • THEN The benchmark reports a median save time of less than 0.2 s.

Existing Tool Considered

The alternatives check of 2026-11-21 in the issue body gives not-applicable: class perf, no search, and Tool: none found. The change builds its own code and reuses the standard-library modules zlib and hashlib already in src/devops_cli/roadmap/stores/listing_shared.py. Written cost: about 40 changed lines, an estimated 6,000 tokens and 2 hours to build, keep and fix until the tests pass.

Key questions

  • How does the write avoid compressing records that it does not send?: Compare the incoming records with the _Seen entry of the repository before JSON encoding. When _seen.issues is not None and _seen.digest equals held.meta.digest, compare each IssueRecord by number and equality. Decide OLDER from the watermark and schema. Decide UNCHANGED from the equal records and the stored full_at and watermark. Decide a delta from the changed, new and removed numbers. Encode, digest and compress only the changed and new records. Reuse _seen.digests for the unchanged records. Build meta from the merged digests. _replace still encodes, digests and compresses all records. _update takes the full merged digest map to compute removed numbers. (best practice) [sources: src/devops_cli/roadmap/stores/listing_shared.py, src/devops_cli/config/defaults.py:750]
    • Chosen: Compare with _seen before encoding. The store already keeps the records and digests for the repository, so the unchanged and delta paths skip zlib and JSON encoding for unchanged records.
    • Rejected: Encode and hash all records, then compare digests. This is the current cost, and zlib compression dominates the unchanged save.
    • Rejected: Compare only the stored meta.digest. The digest cannot be known without encoding every record, so it does not avoid the cost.
  • How does the store make one scan for each repository when lanes load at the same time?: Add a lock for each repository around the scan. After a lane gets the lock, read _seen again. Return its records when their digest is the held digest. Otherwise scan once and store the result. A lane that already has _seen can return before it takes the lock. (best practice) [sources: src/devops_cli/roadmap/stores/listing_shared.py]
    • Chosen: A lock for each repository with a second _seen read. Lanes of different repositories do not wait, and a waiting lane reuses the first scan.
    • Rejected: One global lock. It serializes loads of every repository.
    • Rejected: No lock. This is the current duplicate scan cost.
  • Which parts of a load does the time budget cover?: The budget covers the HSCAN loop, zlib inflate of each record and validation of each record. A step that passes the budget returns None. It counts one budget stop with error=budget and the step in op. (best practice) [sources: src/devops_cli/roadmap/stores/listing_shared.py, src/devops_cli/config/defaults.py:750]
    • Chosen: HSCAN, inflate and validate. The issue states that the load budget covers only the HSCAN loop today and must cover inflate and validate.
    • Rejected: HSCAN only. This is the current gap.
    • Rejected: A hard limit on each record. A per-record limit does not bound the total load time.
  • How does the test count compressor calls?: Patch zlib.compress with a wrapper that increments a counter and calls the real function. Reset the counter after the priming save. Use it for the unchanged, older and one-record delta saves. (best practice) [sources: src/devops_cli/roadmap/stores/listing_shared.py, tests/roadmap/stores/test_listing_shared.py]
    • Chosen: Patch zlib.compress. It counts calls without a change to the production API.
    • Rejected: Measure elapsed time. Time is noisy and does not prove that no record was compressed.
    • Rejected: Inspect the Valkey fields. An unchanged save writes no record field, so it cannot show whether compression ran.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    type/infra
    source/agent
    scope/roadmap
    on Nov 21, 2026
  2. dan-petty commented on Nov 21, 2026

    @dan-petty
    OwnerAuthor
    • Priority: P1-High. The save path wastes 0.73 to 17 s of CPU and each lane duplicates a 60 MB scan, which is a clear performance degradation.
    • Related: fix(roadmap): correct visibility, keys and bounds of Valkey caches #6611, which asks for a different change. Both items work on listing_shared.py; #6611 fixes visibility, keys, and bounds, while this candidate restructures the save and scan paths for performance.
  3. added
    status/new
    effort/medium
    value/high
    priority/p1-high
    on Nov 21, 2026
  4. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Next step: the alternatives check records a verdict. See the section Proposed design of the body.

  5. added
    status/ready
    and removed
    status/new
    on Nov 21, 2026
  6. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Next step: the stage job writes the plan of the item. See the section Proposed design of the body.

  7. added
    owner/agent
    status/in-progress
    and removed
    status/ready
    on Nov 21, 2026
  8. 14 remaining items

  9. added
    status/in-review
    and removed
    status/in-progress
    on Nov 21, 2026
  10. removed
    status/in-review
    owner/agent
    on Nov 21, 2026
  11. devops-cli-service commented on Nov 21, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-21 19:08 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p1-highscope/roadmapsource/agenttype/infravalue/high

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions