Commit Graph
3 Commits
Author SHA1 Message Date
yetone 1360689ef2 provider: Served is shared while a request holds the catalog, not copied for each look (#3725)
Served built a fresh copy of the whole catalog on every call: the
unlisted groups' entries plus every provider entry. An Entry is ~1.3KB,
and a request looks at Served from outputLimit, userOutput, askedOutput
and windowIn, so with 12 providers of 20 models each look copied ~380KB
and a request ~800KB, all garbage by the reply.

Served now goes through heldFrom like providerEntries: one slice, built
once while a request holds the catalog and kept across requests until a
file it is built from changes. It is clipped, so a caller's append makes
its own copy and never reaches the next caller. The four gateway loops
take each entry by pointer instead of copying 1.3KB a step. Every
caller of Served (gateway, gui, cli, tui, agent, provider) was read for
writes into the slice or its entries; all only read.

Measured on BenchmarkZZIO (12 providers x 20 models, an 8KB request,
not committed): 1.57MB allocated per request before, 640KB after.

TestServedSharedWhileHeld: under Hold two calls give the same backing
entry, and an append to one doesn't show in the next. Red on the old
code (a new copy each call), green now.

Run at f2aa7fe34 (origin/main plus this change): gofmt -l clean on
internal/gateway and internal/provider; GOOS=linux and GOOS=windows go
vet -tags nogui ./internal/gateway ./internal/provider ok; go test
-count=1 -timeout 45m ./internal/provider (skipping
TestUnreadLoginsWriteErrorsKeepCredentials, #3632) ./internal/gateway
./internal/gui ./internal/cli ./internal/gwmw all ok.
2026-10-27 01:55:25 +08:00
yetone 627e8e7d95 provider: a request's groups are built once under its Hold, not again by each look-up
groupsIn read providers.json's groups, matched each pattern against
every model and found the automatic groups over the whole catalog, and
every caller did it again: a request to a group asks FindGroup for its
route, members and window, and the group list, several times each.
Under a Hold the built groups are now kept (heldFrom, keyed on the
catalog's entries, dropped by a write as the other held values are),
and each caller gets a deep copy (cloneGroups) so a change of its own,
such as the TUI's edit before a failed save, never reaches the next.

Tests:
- TestGroupsBuiltOnceWhileHeld: 40 look-ups under one Hold build the
  groups 0 more times; a change to a returned group's members isn't
  the next caller's; a SaveGroup under the Hold is seen. Broken by
  hand (groupsIn built every time): red, "the groups were built 40
  times more in 40 looks under one Hold, want 0". Without the copy:
  red, "a change to a held group's members reached the next caller"
  (and TestHeldGroupsAreTheCallers red too).
- TestCloneGroupsSharesNothing fills every field of a Group and its
  Rules by reflection and fails on any slice, map or pointer the copy
  shares, so a field added later is caught. Broken by hand (Weights
  not cloned): red, "Group.Weights is shared with the held group".

BenchmarkGroupLooksHeld (30 providers x 60 models, 4 FindGroup and 1
Groups under one Hold; -benchtime 200x, two rounds; load average ~450):
  before  13.8 / 23.9 ms/op  26.2MB/op  41080 allocs/op
  after   0.18 / 0.14 ms/op   310KB/op    413 allocs/op
User+sys CPU of the binary, 300 iterations minus 3: before 4.5/4.2 s
(~15 ms per request's look-ups), after 0.08/0.06 s (~0.2 ms).
BenchmarkGroupOverhead (200KB, not streamed, 30x): allocs of a group
request fell (30x60 g4 72.9K/75.0K -> 60.5K/75.8K, g1 70.9K/73.1K ->
62.4K/68.4K; 12x20 g4 46.1K/45.8K -> 43.7K/44.7K); cpu-ms/op moved
within the noise of a machine at load ~450, where a request's wall
time passes heldFor and the held values are built again.

Run: go vet ./internal/provider; go test ./internal/provider -run
'Group|Hold|Held|Rule|Clone' -count 1: ok (17.0s); go test
./internal/gateway -run Group -count 1: ok (53.1s).
2026-10-19 15:19:31 +08:00
yetone a595f1559c provider: a request to a routing group builds the groups' entries once, not once per look at every member of every group (colmugx, Yayoi_no_yume on X)
colmugx found magpie hotter with groups, and worse since ~v0.1.1225.
Yayoi_no_yume saw CPU jump to 100% as soon as a request entered a group,
and stay there under concurrency.

Cause: one request to a group asked for the groups' entries
(groupEntries) five or six times: Catalog from Resolve and IsDecider,
and Served from membersImageInput, outputLimit, windowIn and roomy. A
direct provider/model request asked through Resolve too. Nothing held
that work. Each build:
- went through every member of every group, the found auto-<model> ones
  included, with a linear pass over the whole catalog per member. It
  copied each large Entry it passed (groupEntries, resolveIn);
- since 8f4d091c6 (#2224) ran smallestWindow for every group, not only
  one set to ContextSmallest. That was a second full pass per member,
  which is the regression after v0.1.1225;
- parsed settings.json again for each group's disabled models
  (membersIn -> disabledList -> settings.LoadChecked).

Fix:
- groupEntries is held while a request holds the catalog. heldFrom keys
  it to the catalog slice it was built from, so it is rebuilt for a
  rebuilt catalog. Catalog and Served hand out a copy.
- Each member's entry is looked up in a map built once per build.
- smallestWindow runs only for ContextSmallest.
- resolveIn walks by index, not by copying each Entry.
- disabledList is held like heldSettings. SetDisabledModels calls
  Changed, so a change is seen at once.

Guards (each fails without its fix, broken by hand):
- TestGroupEntriesBuiltOnceWhileHeld: the same build under one Hold.
  Also checks that a caller's change to Catalog doesn't reach the next
  caller, that another catalog is built from, and that a save is seen.
- TestGroupLooksParseNoSettingsPerGroupWhileHeld: settings parses per
  look.
- BenchmarkGroupEntries (provider) and BenchmarkGroupOverhead /
  BenchmarkGroupOverheadParallel (gateway). These use fake upstreams:
  direct vs groups of 1, 4 and 8 members, one with a failing first
  member and one with a resting first member, at 200KB and 2MB, streamed
  and not, on a 12x20 and a 30x60 catalog.

Numbers. cpu-ms/op is process CPU per request. The machine was at load
~200-430, so wall times are not comparable.
- Catalog of 30 providers x 60 shared models, 200KB, non-stream,
  BenchmarkGroupOverhead, 5x:

  | Version              | direct p0/m1 | groups (g1 g4 g8u gfail grest) |
  |----------------------|--------------|--------------------------------|
  | v0.1.1225 (54ba9042d) | 242          | 976 / 1006 / 965 / 966 / 945   |
  | HEAD ccb2eea15       | 571          | 1462 / 1745 / 1719 / 1758 / 1715 |
  | this commit          | 76           | 93 / 116 / 91 / 114 / 106      |

  Parallel, HEAD -> this commit: direct 563 -> 73, groups 1355-1625 ->
  94-152. The cost of entering a group, over direct: ~+1150 ms at HEAD,
  ~+15-40 ms now. Memory per group request: 200MB -> 74-79MB. Allocs:
  442k -> 128-141k.
- BenchmarkGroupEntries, one build of the 30x60 catalog's groups, CPU:
  HEAD 233 ms; HEAD with smallestWindow gated only 151 ms; this commit
  20 ms.
- 12x20 catalog, 200KB, HEAD (before): direct 45.5, groups 83-89
  cpu-ms/op. v0.1.1225: direct 37, groups 85-97. So with a small catalog
  the overhead is old, not new. It grows with providers x models x
  groups, which is why colmugx's and Yayoi_no_yume's catalogs feel it.

Run at this head:
- go test ./... -timeout 45m at load ~430: 60 packages ok, including
  internal/provider. internal/agent, internal/gateway and
  internal/library hit the 45m timeout under that load. These were not
  failures. internal/gui failed two tests:
  - TestSetAnswersWhileDevinAndProvidersHang ("set slow/slow-1 took
    11.9s", over its 10s wall bound) failed in the run and once more
    alone (16.25s). It passed on the ccb2eea15 checkout (9.95s) and
    2 of 2 with -count=2 at this commit. It is a wall-clock bound under
    load, and this commit doesn't touch its path.
  - TestMainWindowCorners was killed while measuring and passed rerun.
- go test ./internal/gateway -run
  'Group|Member|Rout|Fallback|Rest|Window|Image' -count=1: ok (221s)
- TestGroupLooksParseNoSettingsPerGroupWhileHeld with disabledList not
  held: red, "settings.json parsed 50 times in 50 looks"
- gofmt -l internal: clean
- GOOS=linux go vet -tags nogui ./...: clean
- GOOS=windows go vet ./...: clean
2026-10-19 03:19:13 +08:00