From ec150889b5b73b4911f5be92aac044aaaa69d23b 2014-04-06 01:34:25 From: MinRK Date: 2014-04-06 01:34:25 Subject: [PATCH] fix n^2 performance issue in coalesce_streams preprocessor for n consecutive stream outputs, `\r` fix would be compiled n times, and applied to each output (n-i) times. - move pattern to module level - apply replacement after coalescing outputs --- diff --git a/IPython/nbconvert/preprocessors/coalescestreams.py b/IPython/nbconvert/preprocessors/coalescestreams.py index 37fddd1..7ccc5c6 100644 --- a/IPython/nbconvert/preprocessors/coalescestreams.py +++ b/IPython/nbconvert/preprocessors/coalescestreams.py @@ -40,6 +40,7 @@ def cell_preprocessor(function): return nb, resources return wrappedfunc +cr_pat = re.compile(r'.*\r(?=[^\n])') @cell_preprocessor def coalesce_streams(cell, resources, index): @@ -64,7 +65,6 @@ def coalesce_streams(cell, resources, index): last = outputs[0] new_outputs = [last] - for output in outputs[1:]: if (output.output_type == 'stream' and last.output_type == 'stream' and @@ -72,12 +72,14 @@ def coalesce_streams(cell, resources, index): ): last.text += output.text - # Respect \r characters. - cr_pat = re.compile(r'.*\r(?=[^\n])') - last.text = cr_pat.sub('', last.text) else: new_outputs.append(output) last = output + + # process \r characters + for output in new_outputs: + if output.output_type == 'stream': + output.text = cr_pat.sub('', output.text) cell.outputs = new_outputs return cell, resources