@matheusbsbs / matheus-bagliano / commits / 9257d8f761
Fork 0

gitk: Compute row numbers and order tokens lazily

Instead of computing ordertok values and arc row numbers in getcommitlines, this defers computing them until they are needed. So getcommitlines no longer calls update_arcrows; instead it gets called from rowofcommit and make_disporder. Things that modify arcs now call modify_arc instead of setting vtokmod/varcmod directly, and modify_arc does the undolayout that used to be in update_arcrows. Also, idcol and make_idlist now use a new ordertoken function instead of the ordertok variable. ordertoken uses ordertok as a cache, but can itself compute the ordering tokens from scratch. This means that the ordering tokens (and hence the layout of the graph) is once again determined by the topological ordering we put on the graph, not on the order in which we see the commits from git log, which improves the appearance of the graph. Signed-off-by: Paul Mackerras <paulus@samba.org>

Paul Mackerras committed Dec 11, 2007 at 10:45 UTC 9257d8f7616e20d76b2c04cea7cb2fcd7daf68bb
1 file changed +113 -90
gitk
+113 -90
@@ -301,9 +301,7 @@ proc resetvarcs {view} {
301 foreach vd [array names vseedcount $view,*] {
302 unset vseedcount($vd)
303 }
304 - foreach vid [array names ordertok $view,*] {
305 - unset ordertok($vid)
306 - }
304 + catch {unset ordertok}
305 }
306
307 proc newvarc {view id} {
@@ -413,7 +411,7 @@ proc splitvarc {p v} {
411
412 proc renumbervarc {a v} {
413 global parents children varctok varcstart varccommits
416 - global vupptr vdownptr vleftptr varcid vtokmod varcmod
414 + global vupptr vdownptr vleftptr varcid vtokmod
415
416 set t1 [clock clicks -milliseconds]
417 set todo {}
@@ -463,15 +461,11 @@ proc renumbervarc {a v} {
461 }
462 set b [lindex $vupptr($v) $a]
463 if {$b != $ka} {
466 - set tok [lindex $varctok($v) $ka]
467 - if {[string compare $tok $vtokmod($v)] < 0} {
468 - set vtokmod($v) $tok
469 - set varcmod($v) $ka
464 + if {[string compare [lindex $varctok($v) $ka] $vtokmod($v)] < 0} {
465 + modify_arc $v $ka
466 }
471 - set tok [lindex $varctok($v) $b]
472 - if {[string compare $tok $vtokmod($v)] < 0} {
473 - set vtokmod($v) $tok
474 - set varcmod($v) $b
467 + if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
468 + modify_arc $v $b
469 }
470 set c [lindex $vdownptr($v) $b]
471 if {$c == $a} {
@@ -535,8 +529,8 @@ proc fix_reversal {p a v} {
529 }
530
531 proc insertrow {id p v} {
538 - global varcid varccommits parents children cmitlisted ordertok
539 - global commitidx varctok vtokmod varcmod
532 + global varcid varccommits parents children cmitlisted
533 + global commitidx varctok vtokmod
534
535 set a $varcid($v,$p)
536 set i [lsearch -exact $varccommits($v,$a) $p]
@@ -547,26 +541,20 @@ proc insertrow {id p v} {
541 set children($v,$id) {}
542 set parents($v,$id) [list $p]
543 set varcid($v,$id) $a
550 - if {[llength [lappend children($v,$p) $id]] > 1 &&
551 - [vtokcmp $v [lindex $children($v,$p) end-1] $id] > 0} {
552 - set children($v,$p) [lsort -command [list vtokcmp $v] $children($v,$p)]
553 - }
544 + lappend children($v,$p) $id
545 set cmitlisted($v,$id) 1
546 incr commitidx($v)
556 - set ordertok($v,$id) $ordertok($v,$p)
547 # note we deliberately don't update varcstart($v) even if $i == 0
548 set varccommits($v,$a) [linsert $varccommits($v,$a) $i $id]
559 - set tok [lindex $varctok($v) $a]
560 - if {[string compare $tok $vtokmod($v)] < 0} {
561 - set vtokmod($v) $tok
562 - set varcmod($v) $a
549 + if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
550 + modify_arc $v $a
551 }
564 - update_arcrows $v
552 + drawvisible
553 }
554
555 proc removerow {id v} {
568 - global varcid varccommits parents children commitidx ordertok
569 - global varctok vtokmod varcmod
556 + global varcid varccommits parents children commitidx
557 + global varctok vtokmod
558
559 if {[llength $parents($v,$id)] != 1} {
560 puts "oops: removerow [shortids $id] has [llength $parents($v,$id)] parents"
@@ -584,18 +572,16 @@ proc removerow {id v} {
572 unset parents($v,$id)
573 unset children($v,$id)
574 unset cmitlisted($v,$id)
587 - unset ordertok($v,$id)
575 incr commitidx($v) -1
576 set j [lsearch -exact $children($v,$p) $id]
577 if {$j >= 0} {
578 set children($v,$p) [lreplace $children($v,$p) $j $j]
579 }
580 set tok [lindex $varctok($v) $a]
594 - if {[string compare $tok $vtokmod($v)] < 0} {
595 - set vtokmod($v) $tok
596 - set varcmod($v) $a
581 + if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] < 0} {
582 + modify_arc $v $a
583 }
598 - update_arcrows $v
584 + drawvisible
585 }
586
587 proc vtokcmp {v a b} {
@@ -605,6 +591,19 @@ proc vtokcmp {v a b} {
591 [lindex $varctok($v) $varcid($v,$b)]]
592 }
593
594 +proc modify_arc {v a} {
595 + global varctok vtokmod varcmod varcrow vupptr curview
596 +
597 + set vtokmod($v) [lindex $varctok($v) $a]
598 + set varcmod($v) $a
599 + if {$v == $curview} {
600 + while {$a != 0 && [lindex $varcrow($v) $a] eq {}} {
601 + set a [lindex $vupptr($v) $a]
602 + }
603 + undolayout [lindex $varcrow($v) $a]
604 + }
605 +}
606 +
607 proc update_arcrows {v} {
608 global vtokmod varcmod varcrow commitidx currentid selectedline
609 global varcid vseeds vrownum varcorder varcix varccommits
@@ -647,7 +646,6 @@ proc update_arcrows {v} {
646 if {$v == $curview} {
647 catch {unset cached_commitrow}
648 }
650 - set startrow $row
649 while {1} {
650 set p $a
651 incr row [llength $varccommits($v,$a)]
@@ -672,15 +670,8 @@ proc update_arcrows {v} {
670 if {[info exists currentid]} {
671 set selectedline [rowofcommit $currentid]
672 }
675 - undolayout $startrow
676 - if {$row != $commitidx($v)} {
677 - puts "oops update_arcrows got to row $row out of $commitidx($v)"
678 - set vtokmod($v) {}
679 - set varcmod($v) 0
680 - } else {
681 - set vtokmod($v) [lindex $varctok($v) $p]
682 - set varcmod($v) $p
683 - }
673 + set vtokmod($v) [lindex $varctok($v) $p]
674 + set varcmod($v) $p
675 set t2 [clock clicks -milliseconds]
676 incr uat [expr {$t2-$t1}]
677 }
@@ -695,6 +686,7 @@ proc commitinview {id v} {
686 # Return the row number for commit $id in the current view
687 proc rowofcommit {id} {
688 global varcid varccommits varcrow curview cached_commitrow
689 + global varctok vtokmod
690
691 if {[info exists cached_commitrow($id)]} {
692 return $cached_commitrow($id)
@@ -705,6 +697,9 @@ proc rowofcommit {id} {
697 return {}
698 }
699 set a $varcid($v,$id)
700 + if {[string compare [lindex $varctok($v) $a] $vtokmod($v)] > 0} {
701 + update_arcrows $v
702 + }
703 set i [lsearch -exact $varccommits($v,$a) $id]
704 if {$i < 0} {
705 puts "oops didn't find commit [shortids $id] in arc $a"
@@ -738,9 +733,15 @@ proc bsearch {l elt} {
733 # Make sure rows $start..$end-1 are valid in displayorder and parentlist
734 proc make_disporder {start end} {
735 global vrownum curview commitidx displayorder parentlist
741 - global varccommits varcorder parents
736 + global varccommits varcorder parents varcmod varcrow
737 global d_valid_start d_valid_end
738
739 + set la $varcmod($curview)
740 + set lrow [lindex $varcrow($curview) $la]
741 + if {$la == 0 || $lrow eq {} || \
742 + $end < $lrow + [llength $varccommits($curview,$la)]} {
743 + update_arcrows $curview
744 + }
745 set ai [bsearch $vrownum($curview) $start]
746 set start [lindex $vrownum($curview) $ai]
747 set narc [llength $vrownum($curview)]
@@ -786,7 +787,7 @@ proc commitonrow {row} {
787
788 proc closevarcs {v} {
789 global varctok varccommits varcid parents children
789 - global cmitlisted commitidx commitinterest vtokmod varcmod
790 + global cmitlisted commitidx commitinterest vtokmod
791
792 set missing_parents 0
793 set scripts {}
@@ -807,10 +808,8 @@ proc closevarcs {v} {
808 }
809 set varcid($v,$p) $b
810 lappend varccommits($v,$b) $p
810 - set tok [lindex $varctok($v) $b]
811 - if {[string compare $tok $vtokmod($v)] < 0} {
812 - set vtokmod($v) $tok
813 - set varcmod($v) $b
811 + if {[string compare [lindex $varctok($v) $b] $vtokmod($v)] < 0} {
812 + modify_arc $v $b
813 }
814 incr commitidx($v)
815 if {[info exists commitinterest($p)]} {
@@ -822,7 +821,6 @@ proc closevarcs {v} {
821 }
822 }
823 if {$missing_parents > 0} {
825 - update_arcrows $v
824 foreach s $scripts {
825 eval $s
826 }
@@ -833,8 +831,8 @@ proc getcommitlines {fd inst view} {
831 global cmitlisted commitinterest leftover getdbg
832 global commitidx commitdata
833 global parents children curview hlview
836 - global ordertok vnextroot idpending
837 - global varccommits varcid varctok vtokmod varcmod
834 + global vnextroot idpending ordertok
835 + global varccommits varcid varctok vtokmod
836
837 set stuff [read $fd 500000]
838 # git log doesn't terminate the last commit with a null...
@@ -935,29 +933,8 @@ proc getcommitlines {fd inst view} {
933 set id [lindex $ids 0]
934 set vid $view,$id
935 if {!$listed && [info exists parents($vid)]} continue
938 - if {![info exists ordertok($vid)]} {
939 - set otok "o[strrep $vnextroot($view)]"
940 - incr vnextroot($view)
941 - set ordertok($vid) $otok
942 - } else {
943 - set otok $ordertok($vid)
944 - }
936 if {$listed} {
937 set olds [lrange $ids 1 end]
947 - if {[llength $olds] == 1} {
948 - set p [lindex $olds 0]
949 - if {![info exists ordertok($view,$p)]} {
950 - set ordertok($view,$p) $ordertok($vid)
951 - }
952 - } else {
953 - set i 0
954 - foreach p $olds {
955 - if {![info exists ordertok($view,$p)]} {
956 - set ordertok($view,$p) "$otok[strrep $i]]"
957 - }
958 - incr i
959 - }
960 - }
938 } else {
939 set olds {}
940 }
@@ -990,6 +967,7 @@ proc getcommitlines {fd inst view} {
967 [vtokcmp $view [lindex $children($vp) end-1] $id] > 0} {
968 set children($vp) [lsort -command [list vtokcmp $view] \
969 $children($vp)]
970 + catch {unset ordertok}
971 }
972 }
973 if {[info exists varcid($view,$p)]} {
@@ -998,8 +976,7 @@ proc getcommitlines {fd inst view} {
976 incr i
977 }
978 if {[string compare $tok $vtokmod($view)] < 0} {
1001 - set vtokmod($view) $tok
1002 - set varcmod($view) $a
979 + modify_arc $view $a
980 }
981
982 incr commitidx($view)
@@ -1012,7 +989,6 @@ proc getcommitlines {fd inst view} {
989 set gotsome 1
990 }
991 if {$gotsome} {
1015 - update_arcrows $view
992 run chewcommits $view
993 foreach s $scripts {
994 eval $s
@@ -2684,7 +2660,7 @@ proc addviewmenu {n} {
2660 }
2661
2662 proc showview {n} {
2687 - global curview viewfiles cached_commitrow
2663 + global curview viewfiles cached_commitrow ordertok
2664 global displayorder parentlist rowidlist rowisopt rowfinal
2665 global colormap rowtextx nextcolor canvxmax
2666 global numcommits viewcomplete
@@ -2722,6 +2698,7 @@ proc showview {n} {
2698 }
2699 catch {unset commitinterest}
2700 catch {unset cached_commitrow}
2701 + catch {unset ordertok}
2702
2703 set curview $n
2704 set selectedview $n
@@ -3313,24 +3290,70 @@ proc ntimes {n o} {
3290 return $ret
3291 }
3292
3293 +proc ordertoken {id} {
3294 + global ordertok curview varcid varcstart varctok curview parents children
3295 + global nullid nullid2
3296 +
3297 + if {[info exists ordertok($id)]} {
3298 + return $ordertok($id)
3299 + }
3300 + set origid $id
3301 + set todo {}
3302 + while {1} {
3303 + if {[info exists varcid($curview,$id)]} {
3304 + set a $varcid($curview,$id)
3305 + set p [lindex $varcstart($curview) $a]
3306 + } else {
3307 + set p [lindex $children($curview,$id) 0]
3308 + }
3309 + if {[info exists ordertok($p)]} {
3310 + set tok $ordertok($p)
3311 + break
3312 + }
3313 + if {[llength $children($curview,$p)] == 0} {
3314 + # it's a root
3315 + set tok [lindex $varctok($curview) $a]
3316 + break
3317 + }
3318 + set id [lindex $children($curview,$p) 0]
3319 + if {$id eq $nullid || $id eq $nullid2} {
3320 + # XXX treat it as a root
3321 + set tok [lindex $varctok($curview) $a]
3322 + break
3323 + }
3324 + if {[llength $parents($curview,$id)] == 1} {
3325 + lappend todo [list $p {}]
3326 + } else {
3327 + set j [lsearch -exact $parents($curview,$id) $p]
3328 + if {$j < 0} {
3329 + puts "oops didn't find [shortids $p] in parents of [shortids $id]"
3330 + }
3331 + lappend todo [list $p [strrep $j]]
3332 + }
3333 + }
3334 + for {set i [llength $todo]} {[incr i -1] >= 0} {} {
3335 + set p [lindex $todo $i 0]
3336 + append tok [lindex $todo $i 1]
3337 + set ordertok($p) $tok
3338 + }
3339 + set ordertok($origid) $tok
3340 + return $tok
3341 +}
3342 +
3343 # Work out where id should go in idlist so that order-token
3344 # values increase from left to right
3345 proc idcol {idlist id {i 0}} {
3319 - global ordertok curview
3320 -
3321 - set t $ordertok($curview,$id)
3322 - if {$i >= [llength $idlist] ||
3323 - $t < $ordertok($curview,[lindex $idlist $i])} {
3346 + set t [ordertoken $id]
3347 + if {$i >= [llength $idlist] || $t < [ordertoken [lindex $idlist $i]]} {
3348 if {$i > [llength $idlist]} {
3349 set i [llength $idlist]
3350 }
3327 - while {[incr i -1] >= 0 &&
3328 - $t < $ordertok($curview,[lindex $idlist $i])} {}
3351 + while {[incr i -1] >= 0 && $t < [ordertoken [lindex $idlist $i]]} {}
3352 incr i
3353 } else {
3331 - if {$t > $ordertok($curview,[lindex $idlist $i])} {
3354 + if {$t > [ordertoken [lindex $idlist $i]]} {
3355 while {[incr i] < [llength $idlist] &&
3333 - $t >= $ordertok($curview,[lindex $idlist $i])} {}
3356 + $t >= [ordertoken [lindex $idlist $i]]} {}
3357 }
3358 }
3359 return $i
@@ -3553,7 +3576,7 @@ proc prevuse {id row} {
3576
3577 proc make_idlist {row} {
3578 global displayorder parentlist uparrowlen downarrowlen mingaplen
3556 - global commitidx curview ordertok children
3579 + global commitidx curview children
3580
3581 set r [expr {$row - $mingaplen - $downarrowlen - 1}]
3582 if {$r < 0} {
@@ -3576,7 +3599,7 @@ proc make_idlist {row} {
3599 set rn [nextuse $p $r]
3600 if {$rn >= $row &&
3601 $rn <= $r + $downarrowlen + $mingaplen + $uparrowlen} {
3579 - lappend ids [list $ordertok($curview,$p) $p]
3602 + lappend ids [list [ordertoken $p] $p]
3603 }
3604 }
3605 }
@@ -3586,17 +3609,17 @@ proc make_idlist {row} {
3609 if {$p eq $nextid} continue
3610 set rn [nextuse $p $r]
3611 if {$rn < 0 || $rn >= $row} {
3589 - lappend ids [list $ordertok($curview,$p) $p]
3612 + lappend ids [list [ordertoken $p] $p]
3613 }
3614 }
3615 }
3616 set id [lindex $displayorder $row]
3594 - lappend ids [list $ordertok($curview,$id) $id]
3617 + lappend ids [list [ordertoken $id] $id]
3618 while {$r < $rb} {
3619 foreach p [lindex $parentlist $r] {
3620 set firstkid [lindex $children($curview,$p) 0]
3621 if {[rowofcommit $firstkid] < $row} {
3599 - lappend ids [list $ordertok($curview,$p) $p]
3622 + lappend ids [list [ordertoken $p] $p]
3623 }
3624 }
3625 incr r
@@ -3604,7 +3627,7 @@ proc make_idlist {row} {
3627 if {$id ne {}} {
3628 set firstkid [lindex $children($curview,$id) 0]
3629 if {$firstkid ne {} && [rowofcommit $firstkid] < $row} {
3607 - lappend ids [list $ordertok($curview,$id) $id]
3630 + lappend ids [list [ordertoken $id] $id]
3631 }
3632 }
3633 }