Skip to content

perf(search): adaptive debounce and background matching for in-note search#3249

Open
MiMoHo wants to merge 1 commit into
nextcloud:mainfrom
MiMoHo:perf/1729-adaptive-in-note-search
Open

perf(search): adaptive debounce and background matching for in-note search#3249
MiMoHo wants to merge 1 commit into
nextcloud:mainfrom
MiMoHo:perf/1729-adaptive-in-note-search

Conversation

@MiMoHo

@MiMoHo MiMoHo commented Jul 2, 2026

Copy link
Copy Markdown

Problem

Fixes #1729. In long notes, in-note search starts searching after the very first
typed letter. Counting, nth-occurrence lookup and highlighting all ran synchronously
on the main thread, so a single-letter query with thousands of matches freezes the
UI for seconds (see the "&"-prefix workaround described in #1729).

Changes

  1. Adaptive debounce instead of the static 50/200 ms delays: the delay scales
    with the measured duration of the previous search + highlight pass —
    clamp(2 × lastDuration, 50 ms, 750 ms), plus 150 ms for queries ≤ 3 characters.
    Short notes keep the current snappy behavior (addressing the earlier concern in
    Slow search in long notes #1729 that a fixed 1 s delay would hurt every use case), while long notes
    automatically get a delay long enough to let the user finish typing. Measuring
    real duration is more robust than scaling by content length, because the cost is
    dominated by the number of matches.
  2. Background matching with cancellation: occurrence counting runs on a
    background executor; a generation counter discards superseded results ("some kind
    of Cancel event", as suggested by the maintainer in Slow search in long notes #1729). Only the newest query
    is highlighted, on the main thread.
  3. No more full copies per query: jumpToOccurrence() allocated a full
    toLowerCase() copy plus a substring(0, index) copy of the entire note per
    query. Replaced by a single case-insensitive Matcher pass; the recursive,
    case-sensitive indexOfNth is gone (it also disagreed with the case-insensitive
    counting/highlighting).

Not covered

The remaining per-keystroke cost of the editor itself lives in
nextcloud-commons:markdown — see the companion PR
stefan-niedermann/nextcloud-commons#436 (executor reuse in
EditorStateNotifier, debounced SearchHighlightTextWatcher). Bumping
commonsVersion after its release will address #2489 / #3162.

Testing

  • Existing unit tests pass (./gradlew test).
  • Manual: note with ~100 kB text; type a common letter ("e") in the search bar and
    continue typing — the UI no longer freezes after the first letter; searching in a
    short note behaves as before.

…earch

The in-note search started almost immediately (50/200 ms static delay)
after each typed character and ran counting, occurrence lookup and span
highlighting synchronously on the main thread. In long notes this
freezes the UI after the very first typed letter.

- The debounce delay now adapts to the measured duration of the
  previous search + highlight pass: clamp(2 x lastDuration, 50 ms,
  750 ms), plus 150 ms for queries of up to 3 characters. Short notes
  keep the current snappy behavior.
- Occurrence counting runs on a background executor. A generation
  counter cancels superseded searches, so only the newest query result
  is applied (as suggested in nextcloud#1729).
- jumpToOccurrence no longer allocates full lower-case and substring
  copies of the note content per query; the recursive, case-sensitive
  indexOfNth got replaced by a single case-insensitive matcher pass.

Fixes nextcloud#1729
Helps nextcloud#2489

Signed-off-by: MiMoHo <37556964+MiMoHo@users.noreply.github.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity

Metric Results
Complexity 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

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.

Slow search in long notes

1 participant