commit 6b7338bc63da6a15f434740632e8dcfabcffcd10
parent 60108ad6b5b9257406ad35040cf0b47c5dd8a06d
Author: Tero Tilus <tero@tilus.net>
Date: Thu, 28 Oct 2010 14:01:12 +0300
Message#text_to_chunks: avoid O(n^2) behavior on sequences of blank lines
Signed-off-by: Tero Tilus
Diffstat:
1 file changed, 12 insertions(+), 1 deletion(-)
diff --git a/lib/sup/message.rb b/lib/sup/message.rb
@@ -590,9 +590,20 @@ private
state = :text # one of :text, :quote, or :sig
chunks = []
chunk_lines = []
+ nextline_index = -1
lines.each_with_index do |line, i|
- nextline = lines[(i + 1) ... lines.length].find { |l| l !~ /^\s*$/ } # skip blank lines
+ if i >= nextline_index
+ # look for next nonblank line only when needed to avoid O(n²)
+ # behavior on sequences of blank lines
+ if nextline_index = lines[(i+1)..-1].index { |l| l !~ /^\s*$/ } # skip blank lines
+ nextline_index += i + 1
+ nextline = lines[nextline_index]
+ else
+ nextline_index = lines.length
+ nextline = nil
+ end
+ end
case state
when :text