Skip to content

Commit

Permalink
dont cache interesting buffer-based datas
Browse files Browse the repository at this point in the history
  • Loading branch information
tybug committed May 27, 2024
1 parent 41b15e3 commit 866256e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions hypothesis-python/src/hypothesis/internal/conjecture/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,8 +357,25 @@ def _cache(self, data: Union[ConjectureData, ConjectureResult]) -> None:
# write to the buffer cache here as we move more things to the ir cache.
if data.invalid_at is None:
self.__data_cache[data.buffer] = result
key = self._cache_key_ir(data=data)
self.__data_cache_ir[key] = result

# interesting buffer-based data can mislead the shrinker if we cache them.
#
# @given(st.integers())
# def f(n):
# assert n < 100
#
# may generate two counterexamples, n=101 and n=m > 101, in that order,
# where the buffer corresponding to n is large due to eg failed probes.
# We shrink m and eventually try n=101, but it is cached to a large buffer
# and so the best we can do is n=102, a non-ideal shrink.
#
# We can cache ir-based buffers fine, which always correspond to the
# smallest buffer via forced=. The overhead here is small because almost
# all interesting data are ir-based via the shrinker (and that overhead
# will tend towards zero as we move generation to the ir).
if data.ir_tree_nodes is not None or data.status < Status.INTERESTING:
key = self._cache_key_ir(data=data)
self.__data_cache_ir[key] = result

def cached_test_function_ir(
self, nodes: List[IRNode]
Expand Down Expand Up @@ -1218,7 +1235,7 @@ def shrink_interesting_examples(self) -> None:
self.interesting_examples.values(), key=lambda d: sort_key(d.buffer)
):
assert prev_data.status == Status.INTERESTING
data = self.new_conjecture_data_for_buffer(prev_data.buffer)
data = self.new_conjecture_data_ir(prev_data.examples.ir_tree_nodes)
self.test_function(data)
if data.status != Status.INTERESTING:
self.exit_with(ExitReason.flaky)
Expand Down

0 comments on commit 866256e

Please sign in to comment.