Gitea has relied on some slow JS code to match up added and deleted lines on the diff pages. This can cause a considerable slow down on large diff pages. This PR makes a small change meaning that the matching up can occur much more simply. Partial fix #1351
Showing the changes to services/gitdiff/gitdiff.go (1 of 4 files). Show all
Unified · Split
| @@ -75,6 +75,7 @@ const ( · 74 unmodified lines | ||
| 75 | 75 | type DiffLine struct { |
| 76 | 76 | LeftIdx int |
| 77 | 77 | RightIdx int |
| 78 | + Match int | |
| 78 | 79 | Type DiffLineType |
| 79 | 80 | Content string |
| 80 | 81 | Comments []*models.Comment |
| @@ -943,6 +944,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 862 unmodified lines | ||
| 943 | 944 | curFileLFSPrefix bool |
| 944 | 945 | ) |
| 945 | 946 | |
| 947 | + lastLeftIdx := -1 | |
| 946 | 948 | leftLine, rightLine := 1, 1 |
| 947 | 949 | |
| 948 | 950 | for { |
| @@ -1027,13 +1029,21 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 78 unmodified lines | ||
| 1027 | 1029 | curFile.IsIncomplete = true |
| 1028 | 1030 | continue |
| 1029 | 1031 | } |
| 1030 | − diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine} | |
| 1032 | + diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine, Match: -1} | |
| 1031 | 1033 | rightLine++ |
| 1032 | 1034 | if curSection == nil { |
| 1033 | 1035 | // Create a new section to represent this hunk |
| 1034 | 1036 | curSection = &DiffSection{} |
| 1035 | 1037 | curFile.Sections = append(curFile.Sections, curSection) |
| 1036 | 1038 | } |
| 1039 | + if lastLeftIdx > -1 { | |
| 1040 | + diffLine.Match = lastLeftIdx | |
| 1041 | + curSection.Lines[lastLeftIdx].Match = len(curSection.Lines) | |
| 1042 | + lastLeftIdx++ | |
| 1043 | + if lastLeftIdx >= len(curSection.Lines) || curSection.Lines[lastLeftIdx].Type != DiffLineDel { | |
| 1044 | + lastLeftIdx = -1 | |
| 1045 | + } | |
| 1046 | + } | |
| 1037 | 1047 | curSection.Lines = append(curSection.Lines, diffLine) |
| 1038 | 1048 | case '-': |
| 1039 | 1049 | curFileLinesCount++ |
| @@ -1042,7 +1052,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 2 unmodified lines | ||
| 1042 | 1052 | curFile.IsIncomplete = true |
| 1043 | 1053 | continue |
| 1044 | 1054 | } |
| 1045 | − diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine} | |
| 1055 | + diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine, Match: -1} | |
| 1046 | 1056 | if leftLine > 0 { |
| 1047 | 1057 | leftLine++ |
| 1048 | 1058 | } |
| @@ -1051,6 +1061,9 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 2 unmodified lines | ||
| 1051 | 1061 | curSection = &DiffSection{} |
| 1052 | 1062 | curFile.Sections = append(curFile.Sections, curSection) |
| 1053 | 1063 | } |
| 1064 | + if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel { | |
| 1065 | + lastLeftIdx = len(curSection.Lines) | |
| 1066 | + } | |
| 1054 | 1067 | curSection.Lines = append(curSection.Lines, diffLine) |
| 1055 | 1068 | case ' ': |
| 1056 | 1069 | curFileLinesCount++ |
| @@ -1061,6 +1074,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 4 unmodified lines | ||
| 1061 | 1074 | diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine} |
| 1062 | 1075 | leftLine++ |
| 1063 | 1076 | rightLine++ |
| 1077 | + lastLeftIdx = -1 | |
| 1064 | 1078 | if curSection == nil { |
| 1065 | 1079 | // Create a new section to represent this hunk |
| 1066 | 1080 | curSection = &DiffSection{} |