Skip to content

perf(devshard): shrink gateway memory footprint and add profiling#1399

Open
libermans wants to merge 2 commits into
dl/gateway-v2from
dl/gateway-v2-mem
Open

perf(devshard): shrink gateway memory footprint and add profiling#1399
libermans wants to merge 2 commits into
dl/gateway-v2from
dl/gateway-v2-mem

Conversation

@libermans

Copy link
Copy Markdown
Contributor

Reduce devshardctl gateway RSS by not holding inactive escrows in memory, bounding the chat response cache, and avoiding full inference-map deep copies on the common read paths. Adds pprof/memstats for diagnosis. Runtime lifecycle:

  • Boot loads only devshards marked active; inactive ones are skipped instead of being hydrated (and re-queried on chain) at startup.
  • Non-resident devshards are served on demand: read-only debug/status routes hydrate a transient local-SQLite-only runtime (no chain/host clients), and settlement rehydrates a transient full runtime; both are released right after use.
  • All disable paths (admin deactivate, rotation-without-settle, settle) now retire the runtime from memory drain-safely, only after in-flight requests complete. State accessors (avoid copying the full inference map):
  • Add StateMachine.Config, SnapshotStateNoInferences, SnapshotInferences, and InferenceStatusCounts; use them at status/config/single-inference call sites instead of SnapshotState. Endpoints:
  • /v1/state is now summary-only; the full inference dump moves to a new admin/debug endpoint /v1/debug/inferences (no pagination).
  • /v1/debug/state reports inference status counts; single-inference lookup uses GetInference.
  • Add /v1/debug/memstats and register net/http/pprof under /debug/pprof/, both gated behind admin auth. Chat response cache:
  • Fix unbounded growth: entries keyed by request-body hash were only expired lazily on lookup, so unique requests lived until restart. Add a periodic expiry sweep and a total-bytes cap (DEVSHARD_CHAT_CACHE_MAX_BYTES).

Reduce devshardctl gateway RSS by not holding inactive escrows in memory,
bounding the chat response cache, and avoiding full inference-map deep
copies on the common read paths. Adds pprof/memstats for diagnosis.
Runtime lifecycle:
- Boot loads only devshards marked active; inactive ones are skipped instead
  of being hydrated (and re-queried on chain) at startup.
- Non-resident devshards are served on demand: read-only debug/status routes
  hydrate a transient local-SQLite-only runtime (no chain/host clients), and
  settlement rehydrates a transient full runtime; both are released right
  after use.
- All disable paths (admin deactivate, rotation-without-settle, settle) now
  retire the runtime from memory drain-safely, only after in-flight requests
  complete.
State accessors (avoid copying the full inference map):
- Add StateMachine.Config, SnapshotStateNoInferences, SnapshotInferences, and
  InferenceStatusCounts; use them at status/config/single-inference call
  sites instead of SnapshotState.
Endpoints:
- /v1/state is now summary-only; the full inference dump moves to a new
  admin/debug endpoint /v1/debug/inferences (no pagination).
- /v1/debug/state reports inference status counts; single-inference lookup
  uses GetInference.
- Add /v1/debug/memstats and register net/http/pprof under /debug/pprof/,
  both gated behind admin auth.
Chat response cache:
- Fix unbounded growth: entries keyed by request-body hash were only expired
  lazily on lookup, so unique requests lived until restart. Add a periodic
  expiry sweep and a total-bytes cap (DEVSHARD_CHAT_CACHE_MAX_BYTES).
@libermans

Copy link
Copy Markdown
Contributor Author

@qdanik somehow I can't add you to the reviewer list

@qdanik

qdanik commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

@libermans there is only one minor issue, not a blocker.

handleAdminSettleDevshard doesn't clear SettlementPending on success (only scheduleAutoSettlement does). Since reconcilePendingSettlements re-queues !Active && SettlementPending devshards on boot, an escrow settled via the admin endpoint while that flag is set gets re-broadcast (chain-rejected) every restart and never self-heals. Pre-existing gap, worth a one-line clear on the success path.

Comment thread devshard/cmd/devshardctl/escrow_rotator.go
Centralize settlement-pending cleanup so admin settles do not leave a
stale flag that retriggers on restart. Update settlement test mocks to
mirror the real post-broadcast bookkeeping.

Co-authored-by: Cursor <cursoragent@cursor.com>
@a-kuprin

a-kuprin commented Jul 7, 2026

Copy link
Copy Markdown
Collaborator

@qdanik

handleAdminSettleDevshard doesn't clear SettlementPending on success

Fixed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants