Skip to content

Commit

Permalink
faster target phase
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jan 30, 2024
1 parent 958f449 commit 91c63bb
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 4 deletions.
3 changes: 3 additions & 0 deletions hypothesis-python/RELEASE.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ as well continue running the test!

Because we now finish running a few more examples for affected tests, this
might be a slight slowdown - but correspondingly more likely to find a bug.

We've also applied similar tricks to the :ref:`target phase <phases>`, where
they are a pure performance improvement for affected tests.
2 changes: 1 addition & 1 deletion hypothesis-python/src/hypothesis/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,7 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None:
phase = "shrink"
elif runner := getattr(self, "_runner", None):
phase = runner._current_phase
else:
else: # pragma: no cover # in case of messing with internals
phase = "unknown"
tc = make_testcase(
start_timestamp=self._start_timestamp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def __init__(

# Global dict of per-phase statistics, and a list of per-call stats
# which transfer to the global dict at the end of each phase.
self._current_phase = "(not a phase)"
self.statistics = {}
self.stats_per_test_case = []

Expand Down Expand Up @@ -887,7 +888,8 @@ def optimise_targets(self):
if any_improvements:
continue

self.pareto_optimise()
if self.best_observed_targets:
self.pareto_optimise()

if prev_calls == self.call_count:
break
Expand Down Expand Up @@ -1015,6 +1017,7 @@ def new_shrinker(self, example, predicate=None, allow_transition=None):
predicate,
allow_transition=allow_transition,
explain=Phase.explain in self.settings.phases,
in_target_phase=self._current_phase == "target",
)

def cached_test_function(self, buffer, *, error_on_discard=False, extend=0):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ def allow_transition(source, destination):
# If ``destination`` dominates ``source`` then ``source``
# must be dominated in the front - either ``destination`` is in
# the front, or it was not added to it because it was
# dominated by something in it.,
# dominated by something in it.
try:
self.front.front.remove(source)
except ValueError:
Expand Down
11 changes: 10 additions & 1 deletion hypothesis-python/src/hypothesis/internal/conjecture/shrinker.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ def __init__(
*,
allow_transition: bool,
explain: bool,
in_target_phase: bool = False,
):
"""Create a shrinker for a particular engine, with a given starting
point and predicate. When shrink() is called it will attempt to find an
Expand Down Expand Up @@ -309,6 +310,14 @@ def __init__(
# testing and learning purposes.
self.extra_dfas: Dict[str, ConcreteDFA] = {}

# Because the shrinker is also used to `pareto_optimise` in the target phase,
# we sometimes want to allow extending buffers instead of aborting at the end.
if in_target_phase:
from hypothesis.internal.conjecture.engine import BUFFER_SIZE

self.__extend = BUFFER_SIZE
else:
self.__extend = 0
self.should_explain = explain

@derived_value # type: ignore
Expand Down Expand Up @@ -417,7 +426,7 @@ def cached_test_function(self, buffer):
with status >= INVALID that would result from running this buffer."""

buffer = bytes(buffer)
result = self.engine.cached_test_function(buffer)
result = self.engine.cached_test_function(buffer, extend=self.__extend)
self.incorporate_test_data(result)
if self.calls - self.calls_at_last_shrink >= self.max_stall:
raise StopShrinking
Expand Down

0 comments on commit 91c63bb

Please sign in to comment.