7 releases (4 breaking)
|Feb 20, 2024
|Dec 22, 2023
|Sep 2, 2023
|Aug 29, 2023
|Aug 6, 2023
#98 in Text processing
5,454 downloads per month
Used in 4 crates (3 directly)
nucleo is a highly performant fuzzy matcher written in rust. It aims to fill the same use case as
skim. Compared to
nucleo has a significantly faster matching algorithm. This mainly makes a difference when matching patterns with low selectivity on many items. An (unscientific) comparison is shown in the benchmark section below.
Note: If you are looking for a replacement of the
fuzzy-matchercrate and not a fully managed fuzzy picker, you should use the
nucleo uses the exact same scoring system as fzf. That means you should get the same ranking quality (or better) as you are used to from fzf. However,
nucleo has a more faithful implementation of the Smith-Waterman algorithm which is normally used in DNA sequence alignment (see https://www.cs.cmu.edu/~ckingsf/bioinfo-lectures/gaps.pdf) with two separate matrices (instead of one like fzf). This means that
nucleo finds the optimal match more often. For example if you match
nucleo will match
fzf will match
xf_oo (you can increase the word length the result will stay the same). The former is the more intuitive match and has a higher score according to the ranking system that both
nucleo and fzf.
skim (and the
nucleo has an even larger performance advantage and is often around six times faster (see benchmarks below). Furthermore, the bonus system used by nucleo and fzf is (in my opinion) more consistent/superior.
nucleo also handles non-ascii text much better. (
skims bonus system and even case insensitivity only work for ASCII).
Nucleo also handles Unicode graphemes more correctly.
skim both operate on Unicode code points (chars). That means that multi codepoint graphemes can have weird effects (match multiple times, weirdly change the score, ...).
nucleo will always use the first codepoint of the grapheme for matching instead (and reports grapheme indices, so they can be highlighted correctly).
Nucleo is used in the helix-editor and therefore has a large user base with lots of real world testing. The core matcher implementation is considered complete and is unlikely to see major changes. The
nucleo-matcher crate is finished and ready for widespread use, breaking changes should be very rare (a 1.0 release should not be far away).
While the high level
nucleo crate also works well (and is also used in helix), there are still additional features that will be added in the future. The high level crate also need better documentation and will likely see a few API changes in the future.
WIP currently more of a demonstration than a comprehensive benchmark suit most notably scientific comparisons with
fzfare missing (a pain because it can't be called as a library)
Matcher micro benchmarks
Benchmark comparing the runtime of various patterns matched against all files in the source of the linux kernel. Repeat on your system with
BENCHMARK_DIR=<path_to_linux> cargo run -p benches --release (you can specify an empty directory and the kernel is cloned automatically).
Comparison with fzf
For example in the following two screencasts the pattern
///. is pasted into
nucleo (both with about 3 million items open).
fzf takes a while to filter the text (about 1 second) while
nucleo has barely any noticeable delay (a single frame in the screencast so about 1/30 seconds). This comparison was made on a very beefy CPU (Ryzen 5950x) so on slower systems the difference may be larger:
- merge integration into helix
- build a standalone CLI application
- reach feature parity with
- add a way to allow columnar matching
- reach feature parity with
- expose C API so both the high level API and the matching algorithm itself can be used in other applications (like various nvim plugins)
nucleo plays on the fact that the
Smith-Waterman algorithm (that it's based on) was originally developed for matching DNA/RNA sequences. The elements of DNA/RNA that are matched are called nucleotides which was shortened to
The name also indicates its close relationship with the helix editor (sticking with the DNA theme).
This is only intended for those interested and will not be relevant to most people. I plan to turn this into a blog post when I have more time
The fuzzy matching algorithm is based on the
Smith-Waterman (with affine gaps) as described in https://www.cs.cmu.edu/~ckingsf/bioinfo-lectures/gaps.pdf (TODO: explain).
Nucleo faithfully implements this algorithm and therefore has two separate matrices. However, by precomputing the next
m-matrix row we can avoid storing the p-matrix at all and instead just store the value in a variable as we iterate the row.
Nucleo also never really stores the
m-matrix instead we only ever store the current row (which simultaneously serves as the next row). During index calculation a full matrix is however required to backtrack which indices were actually matched. We only store two bools here (to indicate where we came from in the matrix).
skim stores the full p and m matrix in that case.
fzf always allocates a full
mn matrix (even during matching!).
nucleos' matrix is only width
n-m+1 instead of width
n. This comes from the observation that the
p char requires
p-1 chars before it and
m-p chars after it, so there are always
p-1 + m-p = m+1 chars that can never match the current char. This works especially well with only using a single row because the first relevant char is always at the same position even though it's technically further to the right. This is particularly nice because we precalculate the m-matrix row. The m-matrix is computed from diagonal elements, so the precalculated values stay in the same matrix cell.
skim nucleo does couple simpler (but arguably even more impactful) optimizations:
- Presegment Unicode: Unicode segmentation is somewhat slow and matcher will filter the same elements quite often so only doing it once is nice. It also prevents a very common source of bugs (mixing of char indices which we use here and utf8 indices) and makes the code a lot simpler as a result. Fzf does the same.
- Aggressive prefiltering: Especially for ASCII this works very well, but we also do this for Unicode to a lesser extent. This ensures we reject non-matching haystacks as fast as possible. Usually most haystacks will not match when fuzzy matching large lists so having fast path for that case is a huge win.
- Special-case ASCII: 90% of practical text is ASCII. ASCII can be stored as bytes instead of
chars, so cache locality is improved a lot, and we can use
memcharfor superfast prefilters (even case-insensitive prefilter are possible that way)
- Fallback for very long matches: We fall back to greedy matcher which runs in
O(1)space complexity) to avoid the
O(mn)blowup for large matches. This is fzfs old algorithm and yields decent (but not great) results.