File tree Expand file tree Collapse file tree 1 file changed +13
-0
lines changed
Expand file tree Collapse file tree 1 file changed +13
-0
lines changed Original file line number Diff line number Diff 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+
145148def 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
You can’t perform that action at this time.
0 commit comments