|
21 | 21 | #include <cassert> |
22 | 22 | #include <vector> |
23 | 23 |
|
| 24 | +#include "support/coroutine.h" |
| 25 | + |
24 | 26 | namespace wasm { |
25 | 27 |
|
26 | | -// Use the delta debugging algorithm (Zeller 1999, |
27 | | -// https://dl.acm.org/doi/10.1109/32.988498) to find the minimal set of |
28 | | -// items necessary to preserve some property. Returns that minimal set of |
29 | | -// items, preserving their input order. `tryPartition` should have this |
30 | | -// signature: |
31 | | -// |
32 | | -// bool tryPartition(size_t partitionIndex, |
33 | | -// size_t numPartitions, |
34 | | -// const std::vector<T>& partition) |
35 | | -// |
36 | | -// It should return true iff the property is preserved while keeping only |
37 | | -// `partition` items. |
38 | | -template<typename T, typename F> |
39 | | -std::vector<T> deltaDebugging(std::vector<T> items, const F& tryPartition) { |
40 | | - if (items.empty()) { |
41 | | - return items; |
42 | | - } |
43 | | - // First try removing everything. |
44 | | - if (tryPartition(0, 1, {})) { |
45 | | - return {}; |
| 28 | +// Use the delta debugging algorithm (Zeller 2002, |
| 29 | +// https://dl.acm.org/doi/10.1109/32.988498) to find the minimal set of items |
| 30 | +// necessary to preserve some property. `working` is the minimal set of items |
| 31 | +// found so far and `test` is the smaller set of items that should be tested |
| 32 | +// next. After testing, call `accept()`, `reject()`, or `resolve(bool accepted)` |
| 33 | +// to update the working and test sets appropriately. |
| 34 | +template<typename T> struct DeltaDebugger { |
| 35 | + DeltaDebugger(std::vector<T> items) : task(run(std::move(items))) { |
| 36 | + task.handle.resume(); |
46 | 37 | } |
47 | | - size_t numPartitions = 2; |
48 | | - while (numPartitions <= items.size()) { |
49 | | - // Partition the items. |
50 | | - std::vector<std::vector<T>> partitions; |
51 | | - size_t size = items.size(); |
52 | | - size_t basePartitionSize = size / numPartitions; |
53 | | - size_t rem = size % numPartitions; |
54 | | - size_t idx = 0; |
55 | | - for (size_t i = 0; i < numPartitions; ++i) { |
56 | | - size_t partitionSize = basePartitionSize + (i < rem ? 1 : 0); |
57 | | - if (partitionSize > 0) { |
58 | | - std::vector<T> partition; |
59 | | - partition.reserve(partitionSize); |
60 | | - for (size_t j = 0; j < partitionSize; ++j) { |
61 | | - partition.push_back(items[idx++]); |
62 | | - } |
63 | | - partitions.emplace_back(std::move(partition)); |
64 | | - } |
| 38 | + |
| 39 | + bool finished() const { return task.get()->finished; } |
| 40 | + |
| 41 | + const std::vector<T>& working() const { return task.get()->working; } |
| 42 | + std::vector<T>& test() { return task.get()->test; } |
| 43 | + |
| 44 | + size_t partitionCount() const { return task.get()->numPartitions; } |
| 45 | + size_t partitionIndex() const { return task.get()->currPartition; } |
| 46 | + |
| 47 | + void resolve(bool success) { |
| 48 | + if (finished()) { |
| 49 | + return; |
65 | 50 | } |
66 | | - assert(numPartitions == partitions.size()); |
| 51 | + task.resume(success); |
| 52 | + } |
67 | 53 |
|
68 | | - bool reduced = false; |
| 54 | + void accept() { resolve(true); } |
| 55 | + void reject() { resolve(false); } |
69 | 56 |
|
70 | | - // Try keeping only one partition. Try each partition in turn. |
71 | | - for (size_t i = 0; i < numPartitions; ++i) { |
72 | | - if (tryPartition(i, numPartitions, partitions[i])) { |
73 | | - items = std::move(partitions[i]); |
74 | | - numPartitions = 2; |
75 | | - reduced = true; |
76 | | - break; |
77 | | - } |
| 57 | +private: |
| 58 | + struct State { |
| 59 | + std::vector<T> working; |
| 60 | + std::vector<T> test; |
| 61 | + size_t numPartitions = 1; |
| 62 | + size_t currPartition = 0; |
| 63 | + bool finished = false; |
| 64 | + }; |
| 65 | + |
| 66 | + Generator<State*, bool> task; |
| 67 | + |
| 68 | + static Generator<State*, bool> run(std::vector<T> items) { |
| 69 | + State state; |
| 70 | + auto& [working, test, numPartitions, currPartition, finished] = state; |
| 71 | + |
| 72 | + working = std::move(items); |
| 73 | + |
| 74 | + if (working.empty()) { |
| 75 | + finished = true; |
| 76 | + co_yield &state; |
| 77 | + co_return; |
78 | 78 | } |
79 | | - if (reduced) { |
80 | | - continue; |
| 79 | + |
| 80 | + // First try removing everything. |
| 81 | + if (co_yield &state) { |
| 82 | + working = {}; |
| 83 | + finished = true; |
| 84 | + co_yield &state; |
| 85 | + co_return; |
81 | 86 | } |
82 | 87 |
|
83 | | - // Otherwise, try keeping the complement of a partition. Do not do this with |
84 | | - // only two partitions because that would be no different from what we |
85 | | - // already tried. |
86 | | - if (numPartitions > 2) { |
| 88 | + numPartitions = 2; |
| 89 | + while (numPartitions <= working.size()) { |
| 90 | + // Partition the items. |
| 91 | + std::vector<std::vector<T>> partitions; |
| 92 | + size_t size = working.size(); |
| 93 | + size_t basePartitionSize = size / numPartitions; |
| 94 | + size_t rem = size % numPartitions; |
| 95 | + size_t idx = 0; |
87 | 96 | for (size_t i = 0; i < numPartitions; ++i) { |
88 | | - std::vector<T> complement; |
89 | | - complement.reserve(items.size() - partitions[i].size()); |
90 | | - for (size_t j = 0; j < numPartitions; ++j) { |
91 | | - if (j != i) { |
92 | | - complement.insert( |
93 | | - complement.end(), partitions[j].begin(), partitions[j].end()); |
| 97 | + size_t partitionSize = basePartitionSize + (i < rem ? 1 : 0); |
| 98 | + if (partitionSize > 0) { |
| 99 | + std::vector<T> partition; |
| 100 | + partition.reserve(partitionSize); |
| 101 | + for (size_t j = 0; j < partitionSize; ++j) { |
| 102 | + partition.push_back(working[idx++]); |
94 | 103 | } |
| 104 | + partitions.emplace_back(std::move(partition)); |
95 | 105 | } |
96 | | - if (tryPartition(i, numPartitions, complement)) { |
97 | | - items = std::move(complement); |
98 | | - numPartitions = std::max(numPartitions - 1, size_t(2)); |
| 106 | + } |
| 107 | + assert(numPartitions == partitions.size()); |
| 108 | + |
| 109 | + bool reduced = false; |
| 110 | + |
| 111 | + // Try keeping only one partition. Try each partition in turn. |
| 112 | + for (currPartition = 0; currPartition < numPartitions; ++currPartition) { |
| 113 | + test = std::move(partitions[currPartition]); |
| 114 | + if (co_yield &state) { |
| 115 | + working = std::move(test); |
| 116 | + numPartitions = 2; |
99 | 117 | reduced = true; |
100 | 118 | break; |
| 119 | + } else { |
| 120 | + // Restore the partition since we failed and might need it for |
| 121 | + // complement testing. |
| 122 | + partitions[currPartition] = std::move(test); |
101 | 123 | } |
102 | 124 | } |
103 | 125 | if (reduced) { |
104 | 126 | continue; |
105 | 127 | } |
106 | | - } |
107 | 128 |
|
108 | | - if (numPartitions == items.size()) { |
109 | | - // Cannot further refine the partitions. We're done. |
110 | | - break; |
| 129 | + // Otherwise, try keeping the complement of a partition. Do not do this |
| 130 | + // with only two partitions because that would be no different from what |
| 131 | + // we already tried. |
| 132 | + if (numPartitions > 2) { |
| 133 | + for (currPartition = 0; currPartition < numPartitions; |
| 134 | + ++currPartition) { |
| 135 | + test.clear(); |
| 136 | + test.reserve(working.size() - partitions[currPartition].size()); |
| 137 | + for (size_t i = 0; i < numPartitions; ++i) { |
| 138 | + if (i != currPartition) { |
| 139 | + test.insert( |
| 140 | + test.end(), partitions[i].begin(), partitions[i].end()); |
| 141 | + } |
| 142 | + } |
| 143 | + if (co_yield &state) { |
| 144 | + working = std::move(test); |
| 145 | + numPartitions = std::max(numPartitions - 1, size_t(2)); |
| 146 | + reduced = true; |
| 147 | + break; |
| 148 | + } |
| 149 | + } |
| 150 | + if (reduced) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + if (numPartitions == working.size()) { |
| 156 | + // Cannot further refine the partitions. We're done. |
| 157 | + break; |
| 158 | + } |
| 159 | + |
| 160 | + // Otherwise, make the partitions finer grained. |
| 161 | + numPartitions = std::min(working.size(), 2 * numPartitions); |
111 | 162 | } |
112 | 163 |
|
113 | | - // Otherwise, make the partitions finer grained. |
114 | | - numPartitions = std::min(items.size(), 2 * numPartitions); |
| 164 | + // Yield final state |
| 165 | + test = {}; |
| 166 | + finished = true; |
| 167 | + co_yield &state; |
115 | 168 | } |
116 | | - return items; |
117 | | -} |
| 169 | +}; |
118 | 170 |
|
119 | 171 | } // namespace wasm |
120 | 172 |
|
|
0 commit comments