Better internal implementation for 'filtered choice' #1885
Labels
enhancement
it's not broken, but we want it to be better
test-case-reduction
about efficiently finding smaller failing examples
As discussed in the shrinking guide, we have an internal trick that can make sampling from a filtered list much more efficient. It would be great to translate
sampled_from(...).filter(...)into this form internally!On the implementation side, the most elegant way to do this is probably to extend
FilteredStrategy.do_drawwith special cases for strategies that are choosing from a fixed number of elements, i.e.:sampled_fromof reasonable size (e.g. <1k). This could extend tobooleans()andjust()in futurekinto the filtered list. We don't know how many there are, so just assume that it's unfiltered-1 (as we've failed a random draw).kth element. This is up to twice as fast on average as creating the full list - less if very few elements are allowed, but probably more when we're shrinking.kallowed elements, pick a newkwhich is a valid index. If there are no valid elements, fall out of this condition - it'll be noted and marked invalid as if rejection sampling failed and we didn't try this special case.Where this gets a bit hairy is in the interaction with
LaxyStrategyand delayed validation, so we'll probably need to fiddle with the delegation of thedo_draworfiltermethods a bit to have it work. That's why we have this write-up of the discussion from #1862.The text was updated successfully, but these errors were encountered: