Skip to content

Commit

Permalink
TEMP charmap cache workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
Zac-HD committed Jul 12, 2024
1 parent 82f6b60 commit 1d2345d
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions hypothesis-python/src/hypothesis/internal/charmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from functools import lru_cache
from typing import Dict, Tuple

from hypothesis.control import _current_build_context
from hypothesis.configuration import storage_directory
from hypothesis.errors import InvalidArgument
from hypothesis.internal.intervalsets import IntervalSet
Expand Down Expand Up @@ -186,6 +187,14 @@ def as_general_categories(cats, name="cats"):
return tuple(c for c in cs if c in out)


def _should_cache():
# FIXME: this is going to be AWFULLY slow, we need to cache for the
# duration of the example and then evict instead.
# (but let's see how many things this lets us triage)
context = _current_build_context.value
return context is None or not context.data.provider.avoid_realization


category_index_cache = {(): ()}


Expand Down Expand Up @@ -226,7 +235,8 @@ def _query_for_key(key):
IntervalSet(charmap()[key[-1]])
)
assert isinstance(result, IntervalSet)
category_index_cache[key] = result.intervals
if _should_cache():
category_index_cache[key] = result.intervals
return result.intervals


Expand Down Expand Up @@ -278,5 +288,6 @@ def query(
if v >= min_codepoint and u <= max_codepoint:
result.append((max(u, min_codepoint), min(v, max_codepoint)))
result = (IntervalSet(result) | character_intervals) - exclude_intervals
limited_category_index_cache[qkey] = result
if _should_cache():
limited_category_index_cache[qkey] = result
return result

0 comments on commit 1d2345d

Please sign in to comment.