-
Notifications
You must be signed in to change notification settings - Fork 699
Speedup GetSpellingSuggestion #1640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dzbarsky
wants to merge
1
commit into
microsoft:main
Choose a base branch
from
dzbarsky:zbarsky/buffers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -452,6 +452,10 @@ func GetScriptKindFromFileName(fileName string) ScriptKind { | |
return ScriptKindUnknown | ||
} | ||
|
||
type SpellingSuggestionBuffers struct { | ||
previous, current []float64 | ||
} | ||
|
||
// Given a name and a list of names that are *not* equal to the name, return a spelling suggestion if there is one that is close enough. | ||
// Names less than length 3 only check for case-insensitive equality. | ||
// | ||
|
@@ -465,7 +469,7 @@ func GetScriptKindFromFileName(fileName string) ScriptKind { | |
// and 1 insertion/deletion at 3 characters) | ||
// | ||
// @internal | ||
func GetSpellingSuggestion[T any](name string, candidates []T, getName func(T) string) T { | ||
func GetSpellingSuggestion[T any](name string, candidates []T, getName func(T) string, buffers *SpellingSuggestionBuffers) T { | ||
maximumLengthDifference := max(2, int(float64(len(name))*0.34)) | ||
bestDistance := math.Floor(float64(len(name))*0.4) + 1 // If the best result is worse than this, don't bother. | ||
runeName := []rune(name) | ||
|
@@ -483,7 +487,7 @@ func GetSpellingSuggestion[T any](name string, candidates []T, getName func(T) s | |
if len(candidateName) < 3 && !strings.EqualFold(candidateName, name) { | ||
continue | ||
} | ||
distance := levenshteinWithMax(runeName, []rune(candidateName), bestDistance-0.1) | ||
distance := levenshteinWithMax(runeName, []rune(candidateName), bestDistance-0.1, buffers) | ||
if distance < 0 { | ||
continue | ||
} | ||
|
@@ -495,9 +499,24 @@ func GetSpellingSuggestion[T any](name string, candidates []T, getName func(T) s | |
return bestCandidate | ||
} | ||
|
||
func levenshteinWithMax(s1 []rune, s2 []rune, maxValue float64) float64 { | ||
previous := make([]float64, len(s2)+1) | ||
current := make([]float64, len(s2)+1) | ||
func ensureSize(slice []float64, desired int) []float64 { | ||
if cap(slice) < desired { | ||
return make([]float64, desired) | ||
} | ||
return slice[:desired] | ||
} | ||
|
||
func levenshteinWithMax(s1 []rune, s2 []rune, maxValue float64, buffers *SpellingSuggestionBuffers) float64 { | ||
if buffers == nil { | ||
buffers = &SpellingSuggestionBuffers{} | ||
} | ||
|
||
buffers.previous = ensureSize(buffers.previous, len(s2)+1) | ||
previous := buffers.previous | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would honestly benefit from simply using a |
||
|
||
buffers.current = ensureSize(buffers.current, len(s2)+1) | ||
current := buffers.current | ||
|
||
big := maxValue + 0.01 | ||
for i := range previous { | ||
previous[i] = float64(i) | ||
|
@@ -521,10 +540,10 @@ func levenshteinWithMax(s1 []rune, s2 []rune, maxValue float64) float64 { | |
if c1 == s2[j-1] { | ||
dist = previous[j-1] | ||
} else { | ||
dist = math.Min(previous[j]+1, math.Min(current[j-1]+1, substitutionDistance)) | ||
dist = min(previous[j]+1, min(current[j-1]+1, substitutionDistance)) | ||
} | ||
current[j] = dist | ||
colMin = math.Min(colMin, dist) | ||
colMin = min(colMin, dist) | ||
} | ||
for j := maxJ + 1; j <= len(s2); j++ { | ||
current[j] = big | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can just use
slices.Grow