~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0

Finding a person's groups reads their memberships, not every group

Merged
Merged chadtmiller.com asked to merge into main from chadtmiller.com/group-host:host/groups-of-index

What changes

Finding the groups a person is in no longer reads every group on the host. Host::groups_of, behind getActorGroups, getActor and the OAuth and MCP approval pages, now looks up the membership records naming the person in the groups' own members spaces through a new index on space_record (path). It then checks only those groups, as before: each must be a group, switched on, and give the person a role.

Why

groups_of paged through every account with accounts(None, u32::MAX) and read each one's membership of the person. So every getActorGroups (which asks twice, for the caller and the actor), getActor and approval page cost one membership read per group on the host, whatever the person belonged to. The membership records are already in space_record, at fyi.opensocial.membership/<member DID> in the group's own repo in at://<group>/space/fyi.opensocial.members/self, so an index on the path finds them directly, with no new table to keep in step.

Measured in a release build with a throwaway benchmark (not in this pull), getActorGroups for a person in one group:

Groups on the hostBeforeAfter
10043 ms1.1 ms
500212 ms1.0 ms

How it was checked

No test added: what is listed is unchanged, and the existing tests of getActorGroups, getActor and the OAuth and MCP approval pages cover it: who is in which group, switched-off groups, roles, paging. Each group the index finds is still checked by reading its membership as before, so a record at the path that is not a membership the group wrote counts for nothing.

Every group-host test passed locally but the five interop tests, which need the reference PDS harness not set up on this machine.

Shortcomings

Join requests (asking_anywhere) and invites (invites_to) by person were already found by their own query, not by reading every group, and are unchanged.

Approved

groups_of now finds candidate groups with a new own_space_holding query on space_record (path). It no longer pages through every account. Each candidate still goes through roles, group_of and active, so the result is unchanged.

I checked these against the code:

  • The space expression in OWN_SPACE_HOLDING rebuilds at://<did>/space/<NS>.members/self, which is what members_space produces.
  • The path is membership/<did_rkey(member)>, the key roles reads.
  • ?1 and ?2 match the order of the params passed.
  • The migration is an idempotent CREATE INDEX IF NOT EXISTS.

I found nothing that needs changing. A group can no longer be found without a membership record at that path. The old code could not find one either, because roles reads that same record.

Approved

This pull changes Host::groups_of so it no longer reads every account's membership of the person. Instead it uses a new space_record (path) index to find the groups whose own members space holds fyi.opensocial.membership/<did_rkey(member)>. Each group found still goes through the same checks as before: it must be a group, switched on, and give the person a role.

I checked that it finds the same groups as the old code:

  • Same groups. roles() reads only that one record, in the group's own repo in members_space(group). The query's 'at://' || did || '/space/' || ?1 || '/self' builds exactly members_space's URI, and did = authority drops any copy of the path in someone else's repo. So any group the old loop would have kept has a row here.
  • Same order. The old ACCOUNTS query ordered by did, and the new query does too, so the OAuth and MCP approval pages list groups in the same order.
  • Index use. path = ?2 can use the new index. The space condition is checked on each row the index finds, and only a few rows share one path, so it costs little.

The migration is added at the end of MIGRATIONS. own_space_holding binds rows? on its own line, as AGENTS.md asks for the coverage gate, and the existing getActor, getActorGroups and approval tests exercise it. Nothing in docs/ describes how this lookup works, so no page needs to change. I found nothing that needs changing.

Approved

Carried forward: the change this pull makes at 2ced46d77333f92853b277a538835eef9174d4df is the one reviewed at 9bb2633d180f18b0bf52c5047e20e971fccdccdd, so that review stands. What is new comes from its base.

Merged as 5f1d059