aboutsummaryrefslogtreecommitdiff
path: root/commitquery/query_reduce.go
blob: b7ea5df161495e6e40e9cc7b64b00aa744a80178 (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package commitquery

import "slices"

// removeRedundant removes redundant merge-base candidates.
func removeRedundant(query *query, candidates []nodeIndex) ([]nodeIndex, error) {
	for _, idx := range candidates {
		if query.effectiveGeneration(idx) != generationInfinity {
			return removeRedundantWithGen(query, candidates), nil
		}
	}

	return removeRedundantNoGen(query, candidates)
}

// removeRedundantNoGen removes redundant candidates without generation data.
func removeRedundantNoGen(query *query, candidates []nodeIndex) ([]nodeIndex, error) {
	redundant := make([]bool, len(candidates))
	work := make([]nodeIndex, 0, len(candidates)-1)
	filledIndex := make([]int, 0, len(candidates)-1)

	for i, candidate := range candidates {
		if redundant[i] {
			continue
		}

		work = work[:0]
		filledIndex = filledIndex[:0]

		minGeneration := query.effectiveGeneration(candidate)

		for j, other := range candidates {
			if i == j || redundant[j] {
				continue
			}

			work = append(work, other)
			filledIndex = append(filledIndex, j)

			otherGeneration := query.effectiveGeneration(other)
			if otherGeneration < minGeneration {
				minGeneration = otherGeneration
			}
		}

		err := query.paintDownToCommon(candidate, work, minGeneration)
		if err != nil {
			return nil, err
		}

		if query.hasAnyMarks(candidate, markRight) {
			redundant[i] = true
		}

		for j, other := range work {
			if query.hasAnyMarks(other, markLeft) {
				redundant[filledIndex[j]] = true
			}
		}

		query.clearTouchedMarks(allMarks)
	}

	out := make([]nodeIndex, 0, len(candidates))
	for i, idx := range candidates {
		if !redundant[i] {
			out = append(out, idx)
		}
	}

	return out, nil
}

// removeRedundantWithGen removes redundant candidates using generation data.
func removeRedundantWithGen(query *query, candidates []nodeIndex) []nodeIndex {
	sorted := append([]nodeIndex(nil), candidates...)
	slices.SortFunc(sorted, query.compareByGeneration())

	minGeneration := query.effectiveGeneration(sorted[0])
	minGenPos := 0
	countStillIndependent := len(candidates)

	query.beginMarkPhase()

	walkStart := make([]nodeIndex, 0, len(candidates)*2)

	for _, idx := range candidates {
		query.setMarks(idx, markResult)

		for _, parent := range query.parents(idx) {
			if query.hasAnyMarks(parent, markStale) {
				continue
			}

			query.setMarks(parent, markStale)
			walkStart = append(walkStart, parent)
		}
	}

	slices.SortFunc(walkStart, query.compareByGeneration())

	for _, idx := range walkStart {
		query.clearMarks(idx, markStale)
	}

	for i := len(walkStart) - 1; i >= 0 && countStillIndependent > 1; i-- {
		stack := []nodeIndex{walkStart[i]}
		query.setMarks(walkStart[i], markStale)

		for len(stack) > 0 {
			top := stack[len(stack)-1]

			if query.hasAnyMarks(top, markResult) {
				query.clearMarks(top, markResult)

				countStillIndependent--
				if countStillIndependent <= 1 {
					break
				}

				if top == sorted[minGenPos] {
					for minGenPos < len(sorted)-1 && query.hasAnyMarks(sorted[minGenPos], markStale) {
						minGenPos++
					}

					minGeneration = query.effectiveGeneration(sorted[minGenPos])
				}
			}

			if query.effectiveGeneration(top) < minGeneration {
				stack = stack[:len(stack)-1]

				continue
			}

			pushed := false

			for _, parent := range query.parents(top) {
				if query.hasAnyMarks(parent, markStale) {
					continue
				}

				query.setMarks(parent, markStale)
				stack = append(stack, parent)
				pushed = true

				break
			}

			if !pushed {
				stack = stack[:len(stack)-1]
			}
		}
	}

	out := make([]nodeIndex, 0, len(candidates))
	for _, idx := range candidates {
		if !query.hasAnyMarks(idx, markStale) {
			out = append(out, idx)
		}
	}

	query.clearTouchedMarks(markStale | markResult)

	return out
}