@matheusbsbs / matheus-bagliano / commits / 2f85b7e4b4
Fork 0

git-gui: Switch internal blame structure to Tcl lists

The Tcl list datatype is significantly faster to work with than the array type, especially if our indexes are a consecutive set of numbers, like say line numbers in a file. This rather large change reorganizes the internal data structure of the blame viewer to use a proper Tcl list for the annotation information about a line. Each line is given its own list within the larger line_data list, where the indexes correspond to various facts about that particular line. The interface does seem to be more responsive this way, with less time required by Tcl to process blame, and to switch to another version of the same file. It could just be a placebo effect, but either way most Tcl experts perfer lists for this type of work over arrays. Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Shawn O. Pearce committed Jun 2, 2007 at 16:01 UTC 2f85b7e4b421ab032cc2342808640479a988277f
1 file changed +39 -30
lib/blame.tcl
+39 -30
@@ -45,8 +45,7 @@ field old_bgcolor {} ; # background of current selection
45 field total_lines 0 ; # total length of file
46 field blame_lines 0 ; # number of lines computed
47 field have_commit ; # array commit -> 1
48 -field line_commit ; # array line -> sha1 commit
49 -field line_file ; # array line -> file name
48 +field line_data ; # list of {commit origfile origline}
49
50 field r_commit ; # commit currently being parsed
51 field r_orig_line ; # original line number
@@ -314,8 +313,6 @@ method _load {} {
313 set total_lines 0
314 set blame_lines 0
315 array unset have_commit
317 - array unset line_commit
318 - array unset line_file
316 }
317
318 if {[winfo exists $w.status.c]} {
@@ -338,6 +335,12 @@ method _load {} {
335 }
336 lappend history [list $commit $path]
337
338 + # Index 0 is always empty. There is never line 0 as
339 + # we use only 1 based lines, as that matches both with
340 + # git-blame output and with Tk's text widget.
341 + #
342 + set line_data [list [list]]
343 +
344 set status "Loading $commit:[escape_path $path]..."
345 $w_path conf -text [escape_path $path]
346 if {$commit eq {}} {
@@ -401,6 +404,7 @@ method _read_file {fd} {
404 while {[gets $fd line] >= 0} {
405 regsub "\r\$" $line {} line
406 incr total_lines
407 + lappend line_data {}
408
409 if {$total_lines > 1} {
410 foreach i $w_columns {$i insert end "\n"}
@@ -499,24 +503,22 @@ method _read_blame {fd} {
503
504 set first_lno $lno
505 while {
502 - ![catch {set ncmit $line_commit([expr {$first_lno - 1}])}]
503 - && ![catch {set nfile $line_file([expr {$first_lno - 1}])}]
504 - && $ncmit eq $cmit
505 - && $nfile eq $file
506 + $first_lno > 1
507 + && $cmit eq [lindex $line_data [expr {$first_lno - 1}] 0]
508 + && $file eq [lindex $line_data [expr {$first_lno - 1}] 1]
509 } {
510 incr first_lno -1
511 }
512
513 while {$n > 0} {
514 set lno_e "$lno.0 lineend + 1c"
512 - if {![catch {set g g$line_commit($lno)}]} {
515 + if {[lindex $line_data $lno] ne {}} {
516 + set g [lindex $line_data $lno 0]
517 foreach i $w_columns {
518 $i tag remove g$g $lno.0 $lno_e
519 }
520 }
517 -
518 - set line_commit($lno) $cmit
519 - set line_file($lno) $file
521 + lset line_data $lno [list $cmit $file]
522
523 $w_cgrp delete $lno.0 "$lno.0 lineend"
524 if {$lno == $first_lno} {
@@ -546,10 +548,8 @@ method _read_blame {fd} {
548 }
549
550 while {
549 - ![catch {set ncmit $line_commit($lno)}]
550 - && ![catch {set nfile $line_file($lno)}]
551 - && $ncmit eq $cmit
552 - && $nfile eq $file
551 + $cmit eq [lindex $line_data $lno 0]
552 + && $file eq [lindex $line_data $lno 1]
553 } {
554 $w_cgrp delete $lno.0 "$lno.0 lineend"
555
@@ -599,12 +599,12 @@ method _click {cur_w pos} {
599
600 method _load_commit {pos} {
601 set lno [lindex [split [$w_cgrp index $pos] .] 0]
602 - if {[catch {set cmit $line_commit($lno)}]} return
603 - if {[catch {set file $line_file($lno) }]} return
604 -
605 - set commit $cmit
606 - set path $file
607 - _load $this
602 + set dat [lindex $line_data $lno]
603 + if {$dat ne {}} {
604 + set commit [lindex $dat 0]
605 + set path [lindex $dat 1]
606 + _load $this
607 + }
608 }
609
610 method _showcommit {lno} {
@@ -618,10 +618,15 @@ method _showcommit {lno} {
618
619 $w_cmit conf -state normal
620 $w_cmit delete 0.0 end
621 - if {[catch {set cmit $line_commit($lno)}]} {
621 +
622 + set dat [lindex $line_data $lno]
623 + if {$dat eq {}} {
624 set cmit {}
625 $w_cmit insert end "Loading annotation..."
626 } else {
627 + set cmit [lindex $dat 0]
628 + set file [lindex $dat 1]
629 +
630 set old_bgcolor [$w_file tag cget g$cmit -background]
631 foreach i $w_columns {
632 $i tag conf g$cmit -background $active_color
@@ -682,9 +687,9 @@ method _showcommit {lno} {
687 $w_cmit insert end "$committer_name $committer_email" header_val
688 $w_cmit insert end "$committer_time\n" header_val
689
685 - if {$line_file($lno) ne $path} {
690 + if {$file ne $path} {
691 $w_cmit insert end "Original File:\t" header_key
687 - $w_cmit insert end "[escape_path $line_file($lno)]\n" header_val
692 + $w_cmit insert end "[escape_path $file]\n" header_val
693 }
694
695 $w_cmit insert end "\n$msg"
@@ -702,21 +707,24 @@ method _showcommit {lno} {
707 method _copycommit {} {
708 set pos @$::cursorX,$::cursorY
709 set lno [lindex [split [$::cursorW index $pos] .] 0]
705 - if {![catch {set commit $line_commit($lno)}]} {
710 + set dat [lindex $line_data $lno]
711 + if {$dat ne {}} {
712 clipboard clear
713 clipboard append \
714 -format STRING \
715 -type STRING \
710 - -- $commit
716 + -- [lindex $dat 0]
717 }
718 }
719
720 method _show_tooltip {cur_w pos} {
721 set lno [lindex [split [$cur_w index $pos] .] 0]
716 - if {[catch {set cmit $line_commit($lno)}]} {
722 + set dat [lindex $line_data $lno]
723 + if {$dat eq {}} {
724 _hide_tooltip $this
725 return
726 }
727 + set cmit [lindex $dat 0]
728
729 if {$cmit eq $highlight_commit} {
730 _hide_tooltip $this
@@ -745,7 +753,9 @@ method _open_tooltip {cur_w} {
753 [expr {$pos_x - [winfo rootx $cur_w]}] \
754 [expr {$pos_y - [winfo rooty $cur_w]}]] ,]
755 set lno [lindex [split [$cur_w index $pos] .] 0]
748 - set cmit $line_commit($lno)
756 + set dat [lindex $line_data $lno]
757 + set cmit [lindex $dat 0]
758 + set file [lindex $dat 1]
759
760 set author_name {}
761 set author_email {}
@@ -775,7 +785,6 @@ method _open_tooltip {cur_w} {
785 $author_name $author_email $author_time
786 $summary"
787
778 - set file $line_file($lno)
788 if {$file ne $path} {
789 append tooltip_text "
790