Skip to content

πŸš€ Bulk load share types - #1967

Open
karlitschek wants to merge 1 commit into
test/noid/add-unit-testsfrom
perf/noid/bulk-load-share-types
Open

karlitschek wants to merge 1 commit into
test/noid/add-unit-testsfrom
perf/noid/bulk-load-share-types

Conversation

@karlitschek

@karlitschek karlitschek commented Aug 6, 2026

Copy link
Copy Markdown
Member

Stacked on #1966, which adds the server-free tests/unit/ suite this builds on. The diff here is the single perf commit; #1966 needs to go in first, after which GitHub retargets this one to main automatically.

Note::getData() asks NoteUtil::getShareTypes() for every note it serialises, and that ran one getSharesBy() query per share type β€” eight per note β€” purely to decide whether to draw the "shared" indicator dot. NoteUtil::loadShareTypes() now preloads with one getSharesInFolder() call per folder and getShareTypes() reads from that cache, mirroring TagService::loadTags(). Because only Helper knows which notes a response will actually contain, getAll() hands the walked folders back and Helper preloads for the notes it is about to serialise, declining when there are fewer of them than there are folders β€” so a chunked or incremental sync stays on the per-file path instead of walking the whole tree for a handful of notes. The cache also only answers for notes owned by a user the preload queried as, so a note shared into someone's notes folder still goes through the per-file path and the payload is unchanged for owner and recipient alike.

The query counts below are pinned by tests/unit/Service/NoteUtilShareTypesTest.php, which asserts that the count follows the folder count rather than the note count, that a preloaded result equals what the per-file path returns, and that both fallbacks still work.

Measured on a dev instance with 59 notes in 14 folders and 12 shares, counting MySQL statements for GET /api/v1/notes; the payload was identical in every row, for the owner and for both share recipients.

Request Before After Change
whole collection (what the web UI asks for) 464 194 βˆ’58%
chunkSize=50 403 185 βˆ’54%
chunkSize=25 226 160 βˆ’29%
chunkSize=10 121 121 preload declined
chunkSize=5 86 86 preload declined
incremental sync, 3 notes changed 72 72 preload declined
incremental sync, nothing changed 51 51 preload declined

πŸ€– AI (if applicable)

  • The content of this PR was partly or fully generated using AI

@karlitschek
karlitschek requested review from enjeck and juliusknorr and a lite review from Copilot and removed request for enjeck and silverkszlo August 6, 2026 13:14

This comment was marked as outdated.

@AndyScherzinger
AndyScherzinger force-pushed the perf/noid/bulk-load-share-types branch from d661748 to dc334af Compare September 15, 2026 08:59
@nextcloud-command nextcloud-command added the AI assisted This PR contains AI-assisted commits label Sep 15, 2026
@codecov

codecov Bot commented Sep 15, 2026

Copy link
Copy Markdown

Codecov Report

βœ… All modified and coverable lines are covered by tests.

πŸ“’ Thoughts on this report? Let us know!

@AndyScherzinger
AndyScherzinger force-pushed the perf/noid/bulk-load-share-types branch from dc334af to e0bb991 Compare September 15, 2026 09:18
@AndyScherzinger
AndyScherzinger marked this pull request as draft September 15, 2026 22:07
@AndyScherzinger
AndyScherzinger force-pushed the perf/noid/bulk-load-share-types branch 2 times, most recently from e40a407 to 6acbb6c Compare September 15, 2026 22:32
@AndyScherzinger AndyScherzinger changed the title Perf/noid/bulk load share types πŸš€ Bulk load share types Sep 15, 2026
@AndyScherzinger AndyScherzinger added this to the 6.2.0 milestone Sep 15, 2026
@AndyScherzinger
AndyScherzinger force-pushed the perf/noid/bulk-load-share-types branch 4 times, most recently from 62b532f to a23adcd Compare September 16, 2026 12:24
@AndyScherzinger
AndyScherzinger requested a balanced review from Copilot September 16, 2026 12:24

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟑 Changes recommended

Cache eligibility can suppress share types for a shared-in note not covered by its matching folder lookup.

Get a fresh assessment by requesting another Copilot review.

Review details
  • Files reviewed: 16/18 changed files
  • Comments generated: 3
  • Review effort level: Balanced

Comment thread .github/workflows/phpunit-unit.yml Outdated
Comment thread lib/Service/NoteUtil.php Outdated
Comment thread AGENTS.md Outdated
@AndyScherzinger
AndyScherzinger force-pushed the perf/noid/bulk-load-share-types branch from a23adcd to 92b5654 Compare September 16, 2026 12:57
@AndyScherzinger
AndyScherzinger marked this pull request as ready for review September 16, 2026 17:13
@AndyScherzinger
AndyScherzinger force-pushed the perf/noid/bulk-load-share-types branch 3 times, most recently from e077783 to d04aa94 Compare September 17, 2026 09:27
Note::getData() asks NoteUtil::getShareTypes() for the share types of
every note it serialises, and that ran one IManager::getSharesBy() query
per share type Ò€” eight per note. The web index endpoint loads the whole
collection at once (chunkSize is 0 there), so rendering the note list
issued eight queries times the number of notes: about 4000 for a user with
500 notes, purely to decide whether to draw the "shared" indicator dot.

IManager::getSharesInFolder() answers for every file in a folder in one
go, so the cost becomes one call per folder instead of eight per note.
NoteUtil::loadShareTypes() preloads a whole tree that way and
getShareTypes() reads from that cache, falling back to the old per-file
lookup for the single-note endpoints where preloading a tree would cost
more than it saves. This mirrors TagService::loadTags(), which already
solves the same problem for favorites and is called from the same place.

getSharesInFolder() only reports on a folder's direct children Ò€” passing
$shallow = false is rejected by the server Ò€” so gatherNoteFiles() now also
returns every folder it walked, and loadShareTypes() queries each one.

The payload is deliberately unchanged. Shares are filtered against the
same eight types the old code asked about and emitted in the same order,
so `shareTypes` and `isShared` are identical to before; types the previous
code never requested (TYPE_USERGROUP, the per-user half of a group share)
stay unreported. A folder whose owner cannot be resolved disables the
preload rather than caching an empty result, so a missing owner can never
turn a shared note into an unshared-looking one, and the per-file fallback
reports no shares instead of dereferencing that missing owner.

Coverage is tracked per note rather than per owner. getSharesInFolder()
answers only for a folder's direct children, and only for what the user it
ran as has shared, so a note is cached only when some lookup actually
covered it: same owner, same parent folder. Sharing a folder and a
standalone note into the same tree would otherwise let the folder's lookup
mark the owner as loaded and answer "not shared" for the note beside it.

getSharesInFolder() reports what one user has shared, so the cache only
answers for notes owned by a user the preload queried as. A note owned by
someone else Ò€” one shared into the notes folder, which mounts inside it Ò€”
is looked up per file as before. Without that guard such a note reads as
not shared for its recipient, which is a payload change and not a
performance one.

One getSharesInFolder() call costs about what serialising one note through
the eight getSharesBy() calls it replaces costs, so the preload only pays
off once a request serialises at least as many notes as the tree has
folders. Which notes those are is known to Helper and not to getAll(): a
chunked or pruned request returns a fraction of the collection, and the
rest are emitted as bare ids that are never asked for their share types.
getAll() therefore hands the walked folders back and Helper preloads for
the notes it is about to serialise, so a small sync stays on the per-file
path instead of walking the whole tree for a handful of notes.

Measured on a dev instance with 59 notes in 14 folders and 12 shares,
counting MySQL statements for GET /api/v1/notes, payload identical in
every case for the owner and for both share recipients:

  whole collection    464 -> 194
  chunkSize=50        403 -> 185
  chunkSize=25        226 -> 160
  chunkSize=10        121 -> 121   (preload declined)
  chunkSize=5          86 ->  86   (preload declined)
  sync of 3 changed    72 ->  72   (preload declined)

Also drops the FIXME next to the hardcoded 15 and uses
IShare::TYPE_SCIENCEMESH: the constant has existed since Nextcloud 26 and
the app now requires 33.

Covered by tests/unit/Service/NoteUtilShareTypesTest.php, which asserts
that the query count follows the folder count and not the note count,
that a preloaded result equals what the per-file path returns, that the
fallbacks still work, and that a note shared in beside a folder from the
same owner keeps its share types.

Assisted-by: Claude Code:claude-opus-5[1m]
Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
@AndyScherzinger
AndyScherzinger force-pushed the perf/noid/bulk-load-share-types branch from d04aa94 to 0268d69 Compare September 17, 2026 09:31
@AndyScherzinger
AndyScherzinger changed the base branch from main to test/noid/add-unit-tests September 17, 2026 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

3. to review AI assisted This PR contains AI-assisted commits performance πŸš€

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants