Skip to content

Conversation

@werererer
Copy link

@werererer werererer commented Jan 24, 2026

I maintain a collection with over 250k notes on relatively weak hardware, which makes search and autocomplete noticeably slow. (Up to 4 seconds per query)

This PR introduces Rust modules by replacing one small performance-critical function with a Rust implementation. The goal is to establish an initial integration point for gradually moving hot paths into Rust in follow-up work, in order to better support very large personal knowledge graphs or future workloads involving ontologies or LLM-generated graphs. Rust provides a foundation for exploring future optimizations such as reduced allocation overhead and parallel execution for long-running tasks.

This work is inspired by the previous effort here and may supersede it:
#7222

Open Question

TODO

  • Testing, and improving stability.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @werererer, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the application's search capabilities by migrating a performance-critical scoring function to a native Rust module. This strategic move addresses existing performance bottlenecks, particularly for users with extensive note collections, and lays the groundwork for future high-performance features by integrating Rust into the project's architecture.

Highlights

  • Performance Improvement: The core search scoring logic, previously implemented in JavaScript, has been refactored and moved into a native Rust module. This change is aimed at significantly improving search and autocomplete performance, especially for large note collections, by leveraging Rust's efficiency.
  • Rust Integration: This pull request establishes the initial integration point for Rust into the project. A new Rust package, @trilium/search-native, has been introduced to house performance-critical functions, paving the way for future optimizations.
  • Build System Updates: The build configurations for both desktop (Electron) and server applications have been updated to correctly build, package, and include the new native Rust module. This includes modifications to .gitignore, forge.config.ts, and various build.ts scripts.
  • Code Refactoring: The SearchResult class in apps/server/src/services/search/search_result.ts has been refactored to remove the JavaScript implementation of computeScore and related fuzzy matching utilities, now delegating this functionality entirely to the new Rust native module.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great initiative to improve search performance by moving the scoring logic to a native Rust module. The overall structure and integration with Node.js via NAPI-RS are well done. My review focuses on the new Rust implementation, where I've found a couple of critical correctness issues and some opportunities for further performance optimization that align with the goals of this PR. Addressing these will ensure the new native module is both correct and as fast as possible.

Comment on lines +108 to +111
fn normalize(s: &str) -> String {
s.to_lowercase()
.replace(|c: char| !c.is_alphanumeric() && c != ' ', "")
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The normalize function in Rust is not consistent with the normalizeSearchText function on the JavaScript side. The JS function removes diacritics (e.g., café -> cafe), but this Rust version does not. This will lead to incorrect search scoring, as exact title matches will fail for titles with diacritics.

To fix this, you should use a Unicode normalization crate. First, add unicode-normalization = "0.1" to your [dependencies] in Cargo.toml. Then, update the normalize function to correctly handle diacritics. You'll also need to add use unicode_normalization::UnicodeNormalization; at the top of this file.

fn normalize(s: &str) -> String {
    use unicode_normalization::UnicodeNormalization;
    s.to_lowercase()
        .nfd()
        .filter(|c| !matches!(c, '\u{0300}'..='\u{036f}'))
        .collect()
}

Comment on lines +121 to +145
fn edit_distance(a: &str, b: &str, max: usize) -> usize {
let mut costs: Vec<usize> = (0..=b.len()).collect();

for (i, ca) in a.chars().enumerate() {
let mut last = i;
costs[0] = i + 1;

for (j, cb) in b.chars().enumerate() {
let new = if ca == cb {
last
} else {
1 + last.min(costs[j]).min(costs[j + 1])
};

last = costs[j + 1];
costs[j + 1] = new;
}

if *costs.iter().min().unwrap() > max {
return max + 1;
}
}

costs[b.len()]
}
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

The implementation of edit_distance appears to be incorrect. The logic for updating costs in the single-row optimization is flawed and will not calculate the Levenshtein distance correctly. This will affect all fuzzy matching scores.

Additionally, the early-exit check *costs.iter().min().unwrap() > max is inefficient as it iterates over the costs vector in every outer loop.

Here is a corrected and optimized implementation based on the standard two-row approach, which is easier to verify for correctness and includes an efficient early-exit.

fn edit_distance(a: &str, b: &str, max: usize) -> usize {
    if (a.chars().count() as i32 - b.chars().count() as i32).abs() as usize > max {
        return max + 1;
    }

    let mut v0: Vec<usize> = (0..=b.chars().count()).collect();
    let mut v1 = vec![0; b.chars().count() + 1];

    for (i, ca) in a.chars().enumerate() {
        v1[0] = i + 1;
        let mut min_in_row = i + 1;

        for (j, cb) in b.chars().enumerate() {
            let cost = if ca == cb { 0 } else { 1 };
            v1[j + 1] = (v0[j + 1] + 1).min(v1[j] + 1).min(v0[j] + cost);
            min_in_row = min_in_row.min(v1[j + 1]);
        }

        if min_in_row > max {
            return max + 1;
        }
        
        std::mem::swap(&mut v0, &mut v1);
    }

    let result = v0[b.chars().count()];
    if result > max { max + 1 } else { result }
}

Comment on lines +44 to +52
const normalizedQuery = normalizeSearchText(
fulltextQuery.toLowerCase()
);

const score = computeScore(
{
query: fulltextQuery.toLowerCase(),
tokens,
normalizedQuery,
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The fulltextQuery is converted to lower case twice. It's more efficient and cleaner to do this once and reuse the resulting variable.

        const lowercasedQuery = fulltextQuery.toLowerCase();
        const normalizedQuery = normalizeSearchText(
            lowercasedQuery
        );

        const score = computeScore(
            {
                query: lowercasedQuery,
                tokens,
                normalizedQuery,

Comment on lines +113 to +117
fn word_match(text: &str, query: &str) -> bool {
text.contains(&format!(" {query} "))
|| text.starts_with(&format!("{query} "))
|| text.ends_with(&format!(" {query}"))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This function allocates three new strings on every call due to format!. In a performance-critical path like search scoring, these allocations can add up.

Consider a more allocation-friendly approach. For example, you could build the patterns once and reuse them, or use more advanced string searching techniques that work with slices.


for chunk in &chunks {
for token in tokens {
let norm_token = normalize(token);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

This normalize(token) call is inside a nested loop. This is inefficient as each token from the search query will be normalized repeatedly for every word (chunk) in the text being scored.

For better performance, you should normalize all tokens once before the loops begin, store them in a new Vec, and then use the pre-normalized tokens inside the loop.

@eliandoran
Copy link
Contributor

which makes search and autocomplete noticeably slow. (Up to 4 seconds per query)

What's the performance improvement with the Rust implementation?

@werererer
Copy link
Author

The current implementation didn't improve performance, likely because it is implemented very inefficiently because of the serilization cost or because it didn't covere the most contributing factor to search performance.

I will need to move more of scoring into rust to give a more fair evaluation.

This PR won't go out of Draft until I see a notable performance increase.

@werererer
Copy link
Author

I will try to find another solution based on worker Threads instead
Based on previously abonded work.
#7225

This will take a while to cook up.

@werererer werererer closed this Jan 26, 2026
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.

2 participants