gitk: Add a cache for the topology info
This adds code to write out the topology information used to determine precedes/follows and branch information into a cache file (~3.5MB for the kernel tree). At startup we read the cache file and then do a git rev-list to update it, which is fast because we exclude all commits in the cache that have no children and commits reachable from them (which amounts to everything in the cache). If one of those commits without children no longer exists, then git rev-list will give an error, whereupon we throw away the cache and read in the whole tree again. This gives a significant speedup in the startup time for gitk. Signed-off-by: Paul Mackerras <paulus@samba.org>
Paul Mackerras committed
Aug 30, 2007 at 21:54 UTC
5cd15b6b7f87dc61f729ad31a682ffc394560273
1 file changed
+237
-22
gitk
+237
-22
@@ -6445,25 +6445,59 @@ proc refill_reflist {} {
6445
6446
# Stuff for finding nearby tags
6447
proc getallcommits {} {
6448
- global allcommits allids nbmp nextarc seeds
6448
+ global allcommits nextarc seeds allccache allcwait cachedarcs allcupdate
6449
+ global idheads idtags idotherrefs allparents tagobjid
6450
6451
if {![info exists allcommits]} {
6451
- set allids {}
6452
- set nbmp 0
6452
set nextarc 0
6453
set allcommits 0
6454
set seeds {}
6455
+ set allcwait 0
6456
+ set cachedarcs 0
6457
+ set allccache [file join [gitdir] "gitk.cache"]
6458
+ if {![catch {
6459
+ set f [open $allccache r]
6460
+ set allcwait 1
6461
+ getcache $f
6462
+ }]} return
6463
}
6464
6458
- set cmd [concat | git rev-list --all --parents]
6459
- foreach id $seeds {
6460
- lappend cmd "^$id"
6465
+ if {$allcwait} {
6466
+ return
6467
+ }
6468
+ set cmd [list | git rev-list --parents]
6469
+ set allcupdate [expr {$seeds ne {}}]
6470
+ if {!$allcupdate} {
6471
+ set ids "--all"
6472
+ } else {
6473
+ set refs [concat [array names idheads] [array names idtags] \
6474
+ [array names idotherrefs]]
6475
+ set ids {}
6476
+ set tagobjs {}
6477
+ foreach name [array names tagobjid] {
6478
+ lappend tagobjs $tagobjid($name)
6479
+ }
6480
+ foreach id [lsort -unique $refs] {
6481
+ if {![info exists allparents($id)] &&
6482
+ [lsearch -exact $tagobjs $id] < 0} {
6483
+ lappend ids $id
6484
+ }
6485
+ }
6486
+ if {$ids ne {}} {
6487
+ foreach id $seeds {
6488
+ lappend ids "^$id"
6489
+ }
6490
+ }
6491
+ }
6492
+ if {$ids ne {}} {
6493
+ set fd [open [concat $cmd $ids] r]
6494
+ fconfigure $fd -blocking 0
6495
+ incr allcommits
6496
+ nowbusy allcommits
6497
+ filerun $fd [list getallclines $fd]
6498
+ } else {
6499
+ dispneartags 0
6500
}
6462
- set fd [open $cmd r]
6463
- fconfigure $fd -blocking 0
6464
- incr allcommits
6465
- nowbusy allcommits
6466
- filerun $fd [list getallclines $fd]
6501
}
6502
6503
# Since most commits have 1 parent and 1 child, we group strings of
@@ -6482,10 +6516,10 @@ proc getallcommits {} {
6516
# coming from descendents, and "outgoing" means going towards ancestors.
6517
6518
proc getallclines {fd} {
6485
- global allids allparents allchildren idtags idheads nextarc nbmp
6519
+ global allparents allchildren idtags idheads nextarc
6520
global arcnos arcids arctags arcout arcend arcstart archeads growing
6487
- global seeds allcommits
6488
-
6521
+ global seeds allcommits cachedarcs allcupdate
6522
+
6523
set nid 0
6524
while {[incr nid] <= 1000 && [gets $fd line] >= 0} {
6525
set id [lindex $line 0]
@@ -6493,7 +6527,7 @@ proc getallclines {fd} {
6527
# seen it already
6528
continue
6529
}
6496
- lappend allids $id
6530
+ set cachedarcs 0
6531
set olds [lrange $line 1 end]
6532
set allparents($id) $olds
6533
if {![info exists allchildren($id)]} {
@@ -6524,7 +6558,6 @@ proc getallclines {fd} {
6558
continue
6559
}
6560
}
6527
- incr nbmp
6561
foreach a $arcnos($id) {
6562
lappend arcids($a) $id
6563
set arcend($a) $id
@@ -6564,9 +6597,28 @@ proc getallclines {fd} {
6597
if {![eof $fd]} {
6598
return [expr {$nid >= 1000? 2: 1}]
6599
}
6567
- close $fd
6600
+ set cacheok 1
6601
+ if {[catch {
6602
+ fconfigure $fd -blocking 1
6603
+ close $fd
6604
+ } err]} {
6605
+ # got an error reading the list of commits
6606
+ # if we were updating, try rereading the whole thing again
6607
+ if {$allcupdate} {
6608
+ incr allcommits -1
6609
+ dropcache $err
6610
+ return
6611
+ }
6612
+ error_popup "Error reading commit topology information;\
6613
+ branch and preceding/following tag information\
6614
+ will be incomplete.\n($err)"
6615
+ set cacheok 0
6616
+ }
6617
if {[incr allcommits -1] == 0} {
6618
notbusy allcommits
6619
+ if {$cacheok} {
6620
+ run savecache
6621
+ }
6622
}
6623
dispneartags 0
6624
return 0
@@ -6590,7 +6642,7 @@ proc recalcarc {a} {
6642
}
6643
6644
proc splitarc {p} {
6593
- global arcnos arcids nextarc nbmp arctags archeads idtags idheads
6645
+ global arcnos arcids nextarc arctags archeads idtags idheads
6646
global arcstart arcend arcout allparents growing
6647
6648
set a $arcnos($p)
@@ -6622,7 +6674,6 @@ proc splitarc {p} {
6674
set growing($na) 1
6675
unset growing($a)
6676
}
6625
- incr nbmp
6677
6678
foreach id $tail {
6679
if {[llength $arcnos($id)] == 1} {
@@ -6646,17 +6697,15 @@ proc splitarc {p} {
6697
# Update things for a new commit added that is a child of one
6698
# existing commit. Used when cherry-picking.
6699
proc addnewchild {id p} {
6649
- global allids allparents allchildren idtags nextarc nbmp
6700
+ global allparents allchildren idtags nextarc
6701
global arcnos arcids arctags arcout arcend arcstart archeads growing
6702
global seeds allcommits
6703
6704
if {![info exists allcommits]} return
6654
- lappend allids $id
6705
set allparents($id) [list $p]
6706
set allchildren($id) {}
6707
set arcnos($id) {}
6708
lappend seeds $id
6659
- incr nbmp
6709
lappend allchildren($p) $id
6710
set a [incr nextarc]
6711
set arcstart($a) $id
@@ -6671,6 +6720,172 @@ proc addnewchild {id p} {
6720
set arcout($id) [list $a]
6721
}
6722
6723
+# This implements a cache for the topology information.
6724
+# The cache saves, for each arc, the start and end of the arc,
6725
+# the ids on the arc, and the outgoing arcs from the end.
6726
+proc readcache {f} {
6727
+ global arcnos arcids arcout arcstart arcend arctags archeads nextarc
6728
+ global idtags idheads allparents cachedarcs possible_seeds seeds growing
6729
+ global allcwait
6730
+
6731
+ set a $nextarc
6732
+ set lim $cachedarcs
6733
+ if {$lim - $a > 500} {
6734
+ set lim [expr {$a + 500}]
6735
+ }
6736
+ if {[catch {
6737
+ if {$a == $lim} {
6738
+ # finish reading the cache and setting up arctags, etc.
6739
+ set line [gets $f]
6740
+ if {$line ne "1"} {error "bad final version"}
6741
+ close $f
6742
+ foreach id [array names idtags] {
6743
+ if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
6744
+ [llength $allparents($id)] == 1} {
6745
+ set a [lindex $arcnos($id) 0]
6746
+ if {$arctags($a) eq {}} {
6747
+ recalcarc $a
6748
+ }
6749
+ }
6750
+ }
6751
+ foreach id [array names idheads] {
6752
+ if {[info exists arcnos($id)] && [llength $arcnos($id)] == 1 &&
6753
+ [llength $allparents($id)] == 1} {
6754
+ set a [lindex $arcnos($id) 0]
6755
+ if {$archeads($a) eq {}} {
6756
+ recalcarc $a
6757
+ }
6758
+ }
6759
+ }
6760
+ foreach id [lsort -unique $possible_seeds] {
6761
+ if {$arcnos($id) eq {}} {
6762
+ lappend seeds $id
6763
+ }
6764
+ }
6765
+ set allcwait 0
6766
+ } else {
6767
+ while {[incr a] <= $lim} {
6768
+ set line [gets $f]
6769
+ if {[llength $line] != 3} {error "bad line"}
6770
+ set s [lindex $line 0]
6771
+ set arcstart($a) $s
6772
+ lappend arcout($s) $a
6773
+ if {![info exists arcnos($s)]} {
6774
+ lappend possible_seeds $s
6775
+ set arcnos($s) {}
6776
+ }
6777
+ set e [lindex $line 1]
6778
+ if {$e eq {}} {
6779
+ set growing($a) 1
6780
+ } else {
6781
+ set arcend($a) $e
6782
+ if {![info exists arcout($e)]} {
6783
+ set arcout($e) {}
6784
+ }
6785
+ }
6786
+ set arcids($a) [lindex $line 2]
6787
+ foreach id $arcids($a) {
6788
+ lappend allparents($s) $id
6789
+ set s $id
6790
+ lappend arcnos($id) $a
6791
+ }
6792
+ if {![info exists allparents($s)]} {
6793
+ set allparents($s) {}
6794
+ }
6795
+ set arctags($a) {}
6796
+ set archeads($a) {}
6797
+ }
6798
+ set nextarc [expr {$a - 1}]
6799
+ }
6800
+ } err]} {
6801
+ dropcache $err
6802
+ return 0
6803
+ }
6804
+ if {!$allcwait} {
6805
+ getallcommits
6806
+ }
6807
+ return $allcwait
6808
+}
6809
+
6810
+proc getcache {f} {
6811
+ global nextarc cachedarcs possible_seeds
6812
+
6813
+ if {[catch {
6814
+ set line [gets $f]
6815
+ if {[llength $line] != 2 || [lindex $line 0] ne "1"} {error "bad version"}
6816
+ # make sure it's an integer
6817
+ set cachedarcs [expr {int([lindex $line 1])}]
6818
+ if {$cachedarcs < 0} {error "bad number of arcs"}
6819
+ set nextarc 0
6820
+ set possible_seeds {}
6821
+ run readcache $f
6822
+ } err]} {
6823
+ dropcache $err
6824
+ }
6825
+ return 0
6826
+}
6827
+
6828
+proc dropcache {err} {
6829
+ global allcwait nextarc cachedarcs seeds
6830
+
6831
+ #puts "dropping cache ($err)"
6832
+ foreach v {arcnos arcout arcids arcstart arcend growing \
6833
+ arctags archeads allparents allchildren} {
6834
+ global $v
6835
+ catch {unset $v}
6836
+ }
6837
+ set allcwait 0
6838
+ set nextarc 0
6839
+ set cachedarcs 0
6840
+ set seeds {}
6841
+ getallcommits
6842
+}
6843
+
6844
+proc writecache {f} {
6845
+ global cachearc cachedarcs allccache
6846
+ global arcstart arcend arcnos arcids arcout
6847
+
6848
+ set a $cachearc
6849
+ set lim $cachedarcs
6850
+ if {$lim - $a > 1000} {
6851
+ set lim [expr {$a + 1000}]
6852
+ }
6853
+ if {[catch {
6854
+ while {[incr a] <= $lim} {
6855
+ if {[info exists arcend($a)]} {
6856
+ puts $f [list $arcstart($a) $arcend($a) $arcids($a)]
6857
+ } else {
6858
+ puts $f [list $arcstart($a) {} $arcids($a)]
6859
+ }
6860
+ }
6861
+ } err]} {
6862
+ catch {close $f}
6863
+ catch {file delete $allccache}
6864
+ #puts "writing cache failed ($err)"
6865
+ return 0
6866
+ }
6867
+ set cachearc [expr {$a - 1}]
6868
+ if {$a > $cachedarcs} {
6869
+ puts $f "1"
6870
+ close $f
6871
+ return 0
6872
+ }
6873
+ return 1
6874
+}
6875
+
6876
+proc savecache {} {
6877
+ global nextarc cachedarcs cachearc allccache
6878
+
6879
+ if {$nextarc == $cachedarcs} return
6880
+ set cachearc 0
6881
+ set cachedarcs $nextarc
6882
+ catch {
6883
+ set f [open $allccache w]
6884
+ puts $f [list 1 $cachedarcs]
6885
+ run writecache $f
6886
+ }
6887
+}
6888
+
6889
# Returns 1 if a is an ancestor of b, -1 if b is an ancestor of a,
6890
# or 0 if neither is true.
6891
proc anc_or_desc {a b} {