Merge branch 'perf/word-diff-memory' of https://github.com/4RH1T3CT0R7/kitty

This commit is contained in:
Kovid Goyal 2026-09-26 07:46:59 +05:30
commit 3aed8ffee3
No known key found for this signature in database
GPG key ID: 06BC317B515ACE7C
3 changed files with 42 additions and 2 deletions

View file

@ -218,6 +218,9 @@ Detailed list of changes
- X11: Fix copy and paste targeting the wrong OS window when the keyboard focus
is set to PointerRoot or None
- diff kitten: Fix freezes and very high memory use when a very long line, such
as in a minified file, is changed
0.49.1 [2026-09-24]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View file

@ -125,10 +125,20 @@ var word_regexp = sync.OnceValues(func() (*regexp.Regexp, error) {
return regexp.Compile(pattern)
})
// The LCS table in word_diff_center needs one cell per pair of words between
// the first and last changed word, so lines where that span is longer than
// this fall back to changed_center.
const max_word_diff_words = 2048
// word_diff_center computes highlighted regions for changed words between left and right.
func word_diff_center(left, right string, re *regexp.Regexp) Center {
left_matches := re.FindAllStringIndex(left, -1)
right_matches := re.FindAllStringIndex(right, -1)
// Changed words are only highlighted when they pair up by position, which
// is impossible when the word counts differ, so skip the LCS in that case.
if len(left_matches) != len(right_matches) {
return changed_center(left, right)
}
type word struct {
text string
@ -158,10 +168,13 @@ func word_diff_center(left, right string, re *regexp.Regexp) Center {
rw := right_words[prefix : len(right_words)-suffix]
m, n := len(lw), len(rw)
if max(m, n) > max_word_diff_words {
return changed_center(left, right)
}
// LCS dynamic programming table
dp := make([][]int, m+1)
dp := make([][]int32, m+1)
for i := range dp {
dp[i] = make([]int, n+1)
dp[i] = make([]int32, n+1)
}
for i := 1; i <= m; i++ {
for j := 1; j <= n; j++ {

View file

@ -4,6 +4,7 @@ package diff
import (
"regexp"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
@ -14,6 +15,8 @@ var region_eq = cmpopts.EquateComparable(Region{})
func TestWordDiffCenter(t *testing.T) {
re := regexp.MustCompile(`\S+`)
many_words := strings.Repeat("a ", 100000)
too_many_words := strings.Repeat("a ", max_word_diff_words)
type tc struct {
left, right string
left_regions []Region
@ -93,6 +96,27 @@ func TestWordDiffCenter(t *testing.T) {
left_regions: nil,
right_regions: []Region{{11, 4}},
},
{
// very long line, word counts differ → changed_center without
// running the LCS over 100000 words
left: many_words, right: "x " + many_words + "y",
left_regions: []Region{{0, len(many_words)}},
right_regions: []Region{{0, len(many_words) + 3}},
},
{
// word counts equal, but more than max_word_diff_words words
// between the first and last changed word → changed_center
left: "x " + too_many_words + "y", right: "X " + too_many_words + "Y",
left_regions: []Region{{0, len(too_many_words) + 3}},
right_regions: []Region{{0, len(too_many_words) + 3}},
},
{
// more than max_word_diff_words words, but the changed ones are
// close together → still diffed by word
left: too_many_words + "x a y", right: too_many_words + "X a Y",
left_regions: []Region{{len(too_many_words), 1}, {len(too_many_words) + 4, 1}},
right_regions: []Region{{len(too_many_words), 1}, {len(too_many_words) + 4, 1}},
},
}
for _, tc := range tests {
c := word_diff_center(tc.left, tc.right, re)