Skip to content

Commit 7d63ef3

Browse files
committed
A more complex cache for hide_non_rst_blocks()
1 parent a7ae4ea commit 7d63ef3

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

sphinxlint/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,22 @@ def is_multiline_non_rst_block(line):
142142
return False
143143

144144

145+
_NON_RST_BLOCKS_CACHE = {}
146+
147+
145148
def hide_non_rst_blocks(lines, hidden_block_cb=None):
146149
"""Filters out literal, comments, code blocks, ...
147150
148151
The filter actually replace "removed" lines by empty lines, so the
149152
line numbering still make sense.
153+
154+
This function is quite hot, so we cache the returned value where possible.
155+
The function is only "pure" when hidden_block_cb is None, however,
156+
so we can only safely cache the output when hidden_block_cb=None.
150157
"""
158+
lines = tuple(lines)
159+
if hidden_block_cb is None and lines in _NON_RST_BLOCKS_CACHE:
160+
return _NON_RST_BLOCKS_CACHE[lines]
151161
in_literal = None
152162
excluded_lines = []
153163
block_line_start = None
@@ -175,6 +185,9 @@ def hide_non_rst_blocks(lines, hidden_block_cb=None):
175185
output.append(line)
176186
if excluded_lines and hidden_block_cb:
177187
hidden_block_cb(block_line_start, "".join(excluded_lines))
188+
output = tuple(output)
189+
if hidden_block_cb is None:
190+
_NON_RST_BLOCKS_CACHE[lines] = output
178191
return output
179192

180193

0 commit comments

Comments
 (0)