mirror of
https://github.com/coder/coder.git
synced 2026-06-05 05:58:20 +00:00
11481d7bed
## Summary Two targeted performance improvements to the chatd server, identified through benchmarking. ### 1. RWMutex for instruction cache The instruction cache is read on every chat turn to fetch the home instruction file for a workspace agent. Writes only occur on cache misses (once per agent per 5-minute TTL window), making the access pattern ~90%+ reads. Switching from `sync.Mutex` to `sync.RWMutex` and using `RLock`/`RUnlock` on the read path allows concurrent readers instead of serializing them. **Benchmark (200 concurrent chats):** | | ns/op | |---|---| | Mutex | 108 | | RWMutex | 32 | | **Speedup** | **3.4x** | ### 2. Hoist JSON marshaling out of persistStep transaction `MarshalParts`, `PartFromContent`, `CalculateTotalCostMicros`, and the `usageForCost` struct population are pure CPU work that ran inside the `FOR UPDATE` transaction in `persistStep`. They have zero dependency on the database transaction. Moving all marshal and cost-calculation calls above `p.db.InTx()` means the row lock is held only for `GetChatByIDForUpdate` + `InsertChatMessage` calls. **Benchmark (16 goroutines contending on same lock):** | Tool calls | Inside lock | Outside lock | Speedup | |---|---|---|---| | 1 | 13,977 ns/op | 1,055 ns/op | 13x | | 5 | 38,203 ns/op | 3,769 ns/op | 10x | | 10 | 67,353 ns/op | 7,284 ns/op | 9x | | 20 | 145,864 ns/op | 14,045 ns/op | 10x | No behavioral changes in either commit.