History

Simplify split diff view generation and remove JS dependency (#16775)

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

zeripath committed 5 years ago

Commit f5b0e2c1 parent d24eb6e4 files · +109 −75Browse files

Showing the changes to services/gitdiff/gitdiff.go (1 of 4 files). Show all

Unified · Split

services/gitdiff/gitdiff.go+16−2
@@ -75,6 +75,7 @@ const ( · 74 unmodified lines
7575 type DiffLine struct {
7676 LeftIdx int
7777 RightIdx int
78+ Match int
7879 Type DiffLineType
7980 Content string
8081 Comments []*models.Comment
@@ -943,6 +944,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 862 unmodified lines
943944 curFileLFSPrefix bool
944945 )
945946
947+ lastLeftIdx := -1
946948 leftLine, rightLine := 1, 1
947949
948950 for {
@@ -1027,13 +1029,21 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 78 unmodified lines
10271029 curFile.IsIncomplete = true
10281030 continue
10291031 }
1030− diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine}
1032+ diffLine := &DiffLine{Type: DiffLineAdd, RightIdx: rightLine, Match: -1}
10311033 rightLine++
10321034 if curSection == nil {
10331035 // Create a new section to represent this hunk
10341036 curSection = &DiffSection{}
10351037 curFile.Sections = append(curFile.Sections, curSection)
10361038 }
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+ }
10371047 curSection.Lines = append(curSection.Lines, diffLine)
10381048 case '-':
10391049 curFileLinesCount++
@@ -1042,7 +1052,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 2 unmodified lines
10421052 curFile.IsIncomplete = true
10431053 continue
10441054 }
1045− diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine}
1055+ diffLine := &DiffLine{Type: DiffLineDel, LeftIdx: leftLine, Match: -1}
10461056 if leftLine > 0 {
10471057 leftLine++
10481058 }
@@ -1051,6 +1061,9 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 2 unmodified lines
10511061 curSection = &DiffSection{}
10521062 curFile.Sections = append(curFile.Sections, curSection)
10531063 }
1064+ if len(curSection.Lines) == 0 || curSection.Lines[len(curSection.Lines)-1].Type != DiffLineDel {
1065+ lastLeftIdx = len(curSection.Lines)
1066+ }
10541067 curSection.Lines = append(curSection.Lines, diffLine)
10551068 case ' ':
10561069 curFileLinesCount++
@@ -1061,6 +1074,7 @@ func parseHunks(curFile *DiffFile, maxLines, maxLineCharacters int, input *bufio · 4 unmodified lines
10611074 diffLine := &DiffLine{Type: DiffLinePlain, LeftIdx: leftLine, RightIdx: rightLine}
10621075 leftLine++
10631076 rightLine++
1077+ lastLeftIdx = -1
10641078 if curSection == nil {
10651079 // Create a new section to represent this hunk
10661080 curSection = &DiffSection{}