@@ -320,11 +320,11 @@ func (diffSection *DiffSection) getDiffLineForRender(diffLineType DiffLineType,
320320 hcd := newHighlightCodeDiff ()
321321 var diff1 , diff2 , lineHTML template.HTML
322322 if leftLine != nil {
323- diff1 = diffSection .getLineContentForRender (leftLine .LeftIdx , leftLine , diffSection .file .highlightedOldLines )
323+ diff1 = diffSection .getLineContentForRender (leftLine .LeftIdx , leftLine , diffSection .file .highlightedLeftLines )
324324 lineHTML = util .Iif (diffLineType == DiffLinePlain , diff1 , "" )
325325 }
326326 if rightLine != nil {
327- diff2 = diffSection .getLineContentForRender (rightLine .RightIdx , rightLine , diffSection .file .highlightedNewLines )
327+ diff2 = diffSection .getLineContentForRender (rightLine .RightIdx , rightLine , diffSection .file .highlightedRightLines )
328328 lineHTML = util .Iif (diffLineType == DiffLinePlain , diff2 , "" )
329329 }
330330 if diffLineType != DiffLinePlain {
@@ -394,8 +394,8 @@ type DiffFile struct {
394394 HasChangedSinceLastReview bool // User specific
395395
396396 // for render purpose only, TODO: in the future, we only need to store the related diff lines to save memory
397- highlightedOldLines map [int ]template.HTML
398- highlightedNewLines map [int ]template.HTML
397+ highlightedLeftLines map [int ]template.HTML
398+ highlightedRightLines map [int ]template.HTML
399399}
400400
401401// GetType returns type of diff file.
@@ -1277,23 +1277,35 @@ func GetDiffForRender(ctx context.Context, gitRepo *git.Repository, opts *DiffOp
12771277
12781278 if ! setting .Git .DisableDiffHighlight {
12791279 if limitedContent .LeftContent != nil && limitedContent .LeftContent .buf .Len () < MaxDiffHighlightEntireFileSize {
1280- diffFile .highlightedOldLines = highlightCodeLines (diffFile , limitedContent .LeftContent .buf .String ())
1280+ diffFile .highlightedLeftLines = highlightCodeLines (diffFile , true /* left */ , limitedContent .LeftContent .buf .String ())
12811281 }
12821282 if limitedContent .RightContent != nil && limitedContent .RightContent .buf .Len () < MaxDiffHighlightEntireFileSize {
1283- diffFile .highlightedNewLines = highlightCodeLines (diffFile , limitedContent .RightContent .buf .String ())
1283+ diffFile .highlightedRightLines = highlightCodeLines (diffFile , false /* right */ , limitedContent .RightContent .buf .String ())
12841284 }
12851285 }
12861286 }
12871287
12881288 return diff , nil
12891289}
12901290
1291- func highlightCodeLines (diffFile * DiffFile , content string ) map [int ]template.HTML {
1291+ func highlightCodeLines (diffFile * DiffFile , isLeft bool , content string ) map [int ]template.HTML {
12921292 highlightedNewContent , _ := highlight .Code (diffFile .Name , diffFile .Language , content )
12931293 splitLines := strings .Split (string (highlightedNewContent ), "\n " )
12941294 lines := make (map [int ]template.HTML , len (splitLines ))
1295- for i , line := range splitLines {
1296- lines [i ] = template .HTML (line )
1295+ // only save the highlighted lines we need, but not the whole file, to save memory
1296+ for _ , sec := range diffFile .Sections {
1297+ for _ , ln := range sec .Lines {
1298+ lineIdx := ln .LeftIdx
1299+ if ! isLeft {
1300+ lineIdx = ln .RightIdx
1301+ }
1302+ if lineIdx >= 1 {
1303+ idx := lineIdx - 1
1304+ if idx < len (splitLines ) {
1305+ lines [idx ] = template .HTML (splitLines [idx ])
1306+ }
1307+ }
1308+ }
12971309 }
12981310 return lines
12991311}
0 commit comments