ewah: implement `ewah_bitmap_is_subset()`

In order to know whether a given pseudo-merge (comprised of a "parents" and "objects" bitmaps) is "satisfied" and can be OR'd into the bitmap result, we need to be able to quickly determine whether the "parents" bitmap is a subset of the current set of objects reachable on either side of a traversal. Implement a helper function to prepare for that, which determines whether an EWAH bitmap (the parents bitmap from the pseudo-merge) is a subset of a non-EWAH bitmap (in this case, the results bitmap from either side of the traversal). This function makes use of the EWAH iterator to avoid inflating any part of the EWAH bitmap after we determine it is not a subset of the non-EWAH bitmap. This "fail-fast" allows us to avoid a potentially large amount of wasted effort. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed May 23, 2024 at 17:26 UTC 10a96af8dc22ec890ffacafca52959bd61bd1a28
2 files changed +49
ewah/bitmap.c
+43
@@ -138,6 +138,49 @@ void bitmap_or(struct bitmap *self, const struct bitmap *other)
138 self->words[i] |= other->words[i];
139 }
140
141 +int ewah_bitmap_is_subset(struct ewah_bitmap *self, struct bitmap *other)
142 +{
143 + struct ewah_iterator it;
144 + eword_t word;
145 + size_t i;
146 +
147 + ewah_iterator_init(&it, self);
148 +
149 + for (i = 0; i < other->word_alloc; i++) {
150 + if (!ewah_iterator_next(&word, &it)) {
151 + /*
152 + * If we reached the end of `self`, and haven't
153 + * rejected `self` as a possible subset of
154 + * `other` yet, then we are done and `self` is
155 + * indeed a subset of `other`.
156 + */
157 + return 1;
158 + }
159 + if (word & ~other->words[i]) {
160 + /*
161 + * Otherwise, compare the next two pairs of
162 + * words. If the word from `self` has bit(s) not
163 + * in the word from `other`, `self` is not a
164 + * subset of `other`.
165 + */
166 + return 0;
167 + }
168 + }
169 +
170 + /*
171 + * If we got to this point, there may be zero or more words
172 + * remaining in `self`, with no remaining words left in `other`.
173 + * If there are any bits set in the remaining word(s) in `self`,
174 + * then `self` is not a subset of `other`.
175 + */
176 + while (ewah_iterator_next(&word, &it))
177 + if (word)
178 + return 0;
179 +
180 + /* `self` is definitely a subset of `other` */
181 + return 1;
182 +}
183 +
184 void bitmap_or_ewah(struct bitmap *self, struct ewah_bitmap *other)
185 {
186 size_t original_size = self->word_alloc;
ewah/ewok.h
+6
@@ -179,7 +179,13 @@ void bitmap_unset(struct bitmap *self, size_t pos);
179 int bitmap_get(struct bitmap *self, size_t pos);
180 void bitmap_free(struct bitmap *self);
181 int bitmap_equals(struct bitmap *self, struct bitmap *other);
182 +
183 +/*
184 + * Both `bitmap_is_subset()` and `ewah_bitmap_is_subset()` return 1 if the set
185 + * of bits in 'self' are a subset of the bits in 'other'. Returns 0 otherwise.
186 + */
187 int bitmap_is_subset(struct bitmap *self, struct bitmap *other);
188 +int ewah_bitmap_is_subset(struct ewah_bitmap *self, struct bitmap *other);
189
190 struct ewah_bitmap * bitmap_to_ewah(struct bitmap *bitmap);
191 struct bitmap *ewah_to_bitmap(struct ewah_bitmap *ewah);