Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a901d6a

Browse files
committed
Improve the caching strategy employed in utils.py
1 parent 3e99049 commit a901d6a

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

sphinxlint/sphinxlint.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from dataclasses import dataclass
33
from os.path import splitext
44

5-
from sphinxlint.utils import hide_non_rst_blocks, po2rst
5+
from sphinxlint.utils import PER_FILE_CACHES, hide_non_rst_blocks, po2rst
66

77

88
@dataclass(frozen=True)
@@ -50,16 +50,20 @@ def check_text(filename, text, checkers, options=None):
5050

5151

5252
def check_file(filename, checkers, options: CheckersOptions = None):
53-
ext = splitext(filename)[1]
54-
if not any(ext in checker.suffixes for checker in checkers):
55-
return Counter()
5653
try:
57-
with open(filename, encoding="utf-8") as f:
58-
text = f.read()
59-
if filename.endswith(".po"):
60-
text = po2rst(text)
61-
except OSError as err:
62-
return [f"{filename}: cannot open: {err}"]
63-
except UnicodeDecodeError as err:
64-
return [f"{filename}: cannot decode as UTF-8: {err}"]
65-
return check_text(filename, text, checkers, options)
54+
ext = splitext(filename)[1]
55+
if not any(ext in checker.suffixes for checker in checkers):
56+
return Counter()
57+
try:
58+
with open(filename, encoding="utf-8") as f:
59+
text = f.read()
60+
if filename.endswith(".po"):
61+
text = po2rst(text)
62+
except OSError as err:
63+
return [f"{filename}: cannot open: {err}"]
64+
except UnicodeDecodeError as err:
65+
return [f"{filename}: cannot decode as UTF-8: {err}"]
66+
return check_text(filename, text, checkers, options)
67+
finally:
68+
for memoized_function in PER_FILE_CACHES:
69+
memoized_function.cache_clear()

sphinxlint/utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
from sphinxlint import rst
88

99

10+
PER_FILE_CACHES = []
11+
12+
13+
def _per_file_cache(func):
14+
memoized_func = lru_cache(maxsize=None)(func)
15+
PER_FILE_CACHES.append(memoized_func)
16+
return memoized_func
17+
18+
1019
def match_size(re_match):
1120
return re_match.end() - re_match.start()
1221

@@ -29,7 +38,7 @@ def _clean_heuristic(paragraph, regex):
2938
paragraph = paragraph[: candidate.start()] + paragraph[candidate.end() :]
3039

3140

32-
@lru_cache()
41+
@_per_file_cache
3342
def clean_paragraph(paragraph):
3443
"""Removes all good constructs, so detectors can focus on bad ones.
3544
@@ -45,7 +54,7 @@ def clean_paragraph(paragraph):
4554
return paragraph.replace("\x00", "\\")
4655

4756

48-
@lru_cache()
57+
@_per_file_cache
4958
def escape2null(text):
5059
r"""Return a string with escape-backslashes converted to nulls.
5160
@@ -79,7 +88,7 @@ def escape2null(text):
7988
start = found + 2 # skip character after escape
8089

8190

82-
@lru_cache()
91+
@_per_file_cache
8392
def paragraphs(lines):
8493
"""Yield (paragraph_line_no, paragraph_text) pairs describing
8594
paragraphs of the given lines.
@@ -207,7 +216,7 @@ def hide_non_rst_blocks(lines, hidden_block_cb=None):
207216
_starts_with_substitution_definition = re.compile(r"\.\. \|[^\|]*\| ").match
208217

209218

210-
@lru_cache()
219+
@_per_file_cache
211220
def type_of_explicit_markup(line):
212221
"""Tell apart various explicit markup blocks."""
213222
line = line.lstrip()

0 commit comments

Comments
 (0)