Skip to content

perf: optimize levenshtein function - #5042

Open
kazantsev-maksim wants to merge 74 commits into
apache:mainfrom
kazantsev-maksim:perf_levenshtein
Open

perf: optimize levenshtein function#5042
kazantsev-maksim wants to merge 74 commits into
apache:mainfrom
kazantsev-maksim:perf_levenshtein

Conversation

@kazantsev-maksim

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

  • N/A

Rationale for this change

Optimize existing expression.

What changes are included in this PR?

  • Introduced a thread_local! scratch buffer pool (LEVENSHTEIN_SCRATCH) that reuses pre-allocated vectors across row evaluations using .resize(). This completely eliminates heap allocations for distance matrices inside the processing loop while remaining strictly thread-safe.
  • Added an ASCII Fast-Path (s.is_ascii() && t.is_ascii()) to both levenshtein_distance and levenshtein_distance_with_threshold. When inputs consist entirely of ASCII characters, the functions operate directly on raw UTF-8 byte slices (&[u8]) with zero heap allocations, falling back to the full Unicode char path only when non-ASCII characters are present.

How are these changes tested?

Existing tests.

Benchmark (criterion):

image

@kazantsev-maksim kazantsev-maksim changed the title Perf levenshtein perf: optimize levenshtein function Jul 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.

1 participant