Skip to content

Commit 91c63bb

Browse files
committed
faster target phase
1 parent 958f449 commit 91c63bb

File tree

5 files changed

+19
-4
lines changed

5 files changed

+19
-4
lines changed

hypothesis-python/RELEASE.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ as well continue running the test!
88

99
Because we now finish running a few more examples for affected tests, this
1010
might be a slight slowdown - but correspondingly more likely to find a bug.
11+
12+
We've also applied similar tricks to the :ref:`target phase <phases>`, where
13+
they are a pure performance improvement for affected tests.

hypothesis-python/src/hypothesis/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ def _execute_once_for_engine(self, data: ConjectureData) -> None:
10561056
phase = "shrink"
10571057
elif runner := getattr(self, "_runner", None):
10581058
phase = runner._current_phase
1059-
else:
1059+
else: # pragma: no cover # in case of messing with internals
10601060
phase = "unknown"
10611061
tc = make_testcase(
10621062
start_timestamp=self._start_timestamp,

hypothesis-python/src/hypothesis/internal/conjecture/engine.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ def __init__(
129129

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

@@ -887,7 +888,8 @@ def optimise_targets(self):
887888
if any_improvements:
888889
continue
889890

890-
self.pareto_optimise()
891+
if self.best_observed_targets:
892+
self.pareto_optimise()
891893

892894
if prev_calls == self.call_count:
893895
break
@@ -1015,6 +1017,7 @@ def new_shrinker(self, example, predicate=None, allow_transition=None):
10151017
predicate,
10161018
allow_transition=allow_transition,
10171019
explain=Phase.explain in self.settings.phases,
1020+
in_target_phase=self._current_phase == "target",
10181021
)
10191022

10201023
def cached_test_function(self, buffer, *, error_on_discard=False, extend=0):

hypothesis-python/src/hypothesis/internal/conjecture/pareto.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ def allow_transition(source, destination):
317317
# If ``destination`` dominates ``source`` then ``source``
318318
# must be dominated in the front - either ``destination`` is in
319319
# the front, or it was not added to it because it was
320-
# dominated by something in it.,
320+
# dominated by something in it.
321321
try:
322322
self.front.front.remove(source)
323323
except ValueError:

hypothesis-python/src/hypothesis/internal/conjecture/shrinker.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def __init__(
271271
*,
272272
allow_transition: bool,
273273
explain: bool,
274+
in_target_phase: bool = False,
274275
):
275276
"""Create a shrinker for a particular engine, with a given starting
276277
point and predicate. When shrink() is called it will attempt to find an
@@ -309,6 +310,14 @@ def __init__(
309310
# testing and learning purposes.
310311
self.extra_dfas: Dict[str, ConcreteDFA] = {}
311312

313+
# Because the shrinker is also used to `pareto_optimise` in the target phase,
314+
# we sometimes want to allow extending buffers instead of aborting at the end.
315+
if in_target_phase:
316+
from hypothesis.internal.conjecture.engine import BUFFER_SIZE
317+
318+
self.__extend = BUFFER_SIZE
319+
else:
320+
self.__extend = 0
312321
self.should_explain = explain
313322

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

419428
buffer = bytes(buffer)
420-
result = self.engine.cached_test_function(buffer)
429+
result = self.engine.cached_test_function(buffer, extend=self.__extend)
421430
self.incorporate_test_data(result)
422431
if self.calls - self.calls_at_last_shrink >= self.max_stall:
423432
raise StopShrinking

0 commit comments

Comments
 (0)