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
Contributor

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.

@github-actions

Copy link
Copy Markdown
Contributor

Hello there,
Thank you so much for taking the time and effort to create a pull request to our Nextcloud project.

We hope that the review process is going smooth and is helpful for you. We want to ensure your pull request is reviewed to your satisfaction. If you have a moment, our community management team would very much appreciate your feedback on your experience with this PR review process.

Your feedback is valuable to us as we continuously strive to improve our community developer experience. Please take a moment to complete our short survey by clicking on the following link: https://cloud.nextcloud.com/apps/forms/s/i9Ago4EQRZ7TWxjfmeEpPkf6

Thank you for contributing to Nextcloud and we hope to hear from you soon!

(If you believe you should not receive this message, you can add yourself to the blocklist.)

private String searchQuery = null;
private static final int delay = 50; // If the search string does not change after $delay ms, then the search task starts.
private static final int shortStringDelay = 200; // A longer delay for short search strings.
private static final int minDelay = 50; // Minimum delay in ms after the search string stopped changing before the search task starts.

@alperozturk96 alperozturk96 Aug 10, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thank you for the PR. Good idea.

I think we could make the implementation a bit cleaner and easier to maintain. Instead of adding the debounce logic directly to SearchableBaseNoteFragment.java, would it make sense to move this into a small Kotlin helper class and use coroutines for the implementation?

For example:

class SearchHelper(private val fragment: Fragment) {
    companion object {
        private const val MAX_DELAY = 750L
        private const val LAST_SEARCH_DURATION = 0L
        // ...
    }

    fun checkOccurrences(onComplete: () -> Unit) {
        fragment.lifecycleOwner(Dispatchers.IO) {
            // ...
            withContext(Dispatchers.Main) {
                onComplete()
            }
        }
    }
    
    // if possible we can add `countOccurrences` here as well.
}

This would give us lifecycle-aware code and would also remove the need for managing an ExecutorService and calling shutdownNow() explicitly.

One other suggestion: I think we can remove the comments and keep the variable/method names self-explanatory.

As we add more fixes and features to the fragment, mixing this logic directly into it could make the file increasingly difficult to follow.

I think the approach is good. Thanks again for the work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Slow search in long notes

2 participants