PERF: limit time spent diffing large blobs of text

REFACTOR: s/.length/.size/g
This commit is contained in:
Régis Hanol
2019-06-27 01:45:52 +02:00
parent 50e4ecc77e
commit 134a4c66b7
4 changed files with 34 additions and 24 deletions

View File

@ -21,7 +21,7 @@ class DiscourseDiff
def inline_html
i = 0
inline = []
while i < @block_by_block_diff.length
while i < @block_by_block_diff.size
op_code = @block_by_block_diff[i][1]
if op_code == :common then inline << @block_by_block_diff[i][0]
else
@ -37,7 +37,7 @@ class DiscourseDiff
second = i
end
if i + 1 < @block_by_block_diff.length && @block_by_block_diff[i + 1][1] == opposite_op_code
if i + 1 < @block_by_block_diff.size && @block_by_block_diff[i + 1][1] == opposite_op_code
diff = ONPDiff.new(tokenize_html(@block_by_block_diff[first][0]), tokenize_html(@block_by_block_diff[second][0])).diff
inline << generate_inline_html(diff)
i += 1
@ -54,7 +54,7 @@ class DiscourseDiff
def side_by_side_html
i = 0
left, right = [], []
while i < @block_by_block_diff.length
while i < @block_by_block_diff.size
op_code = @block_by_block_diff[i][1]
if op_code == :common
left << @block_by_block_diff[i][0]
@ -74,7 +74,7 @@ class DiscourseDiff
second = i
end
if i + 1 < @block_by_block_diff.length && @block_by_block_diff[i + 1][1] == opposite_op_code
if i + 1 < @block_by_block_diff.size && @block_by_block_diff[i + 1][1] == opposite_op_code
diff = ONPDiff.new(tokenize_html(@block_by_block_diff[first][0]), tokenize_html(@block_by_block_diff[second][0])).diff
deleted, inserted = generate_side_by_side_html(diff)
left << deleted
@ -93,7 +93,7 @@ class DiscourseDiff
def side_by_side_markdown
i = 0
table = ["<table class=\"markdown\">"]
while i < @line_by_line_diff.length
while i < @line_by_line_diff.size
table << "<tr>"
op_code = @line_by_line_diff[i][1]
if op_code == :common
@ -110,9 +110,9 @@ class DiscourseDiff
second = i
end
if i + 1 < @line_by_line_diff.length && @line_by_line_diff[i + 1][1] == opposite_op_code
if i + 1 < @line_by_line_diff.size && @line_by_line_diff[i + 1][1] == opposite_op_code
before_tokens, after_tokens = tokenize_markdown(@line_by_line_diff[first][0]), tokenize_markdown(@line_by_line_diff[second][0])
if (before_tokens.length - after_tokens.length).abs > MAX_DIFFERENCE
if (before_tokens.size - after_tokens.size).abs > MAX_DIFFERENCE
before_tokens, after_tokens = tokenize_line(@line_by_line_diff[first][0]), tokenize_line(@line_by_line_diff[second][0])
end
diff = ONPDiff.new(before_tokens, after_tokens).short_diff
@ -147,25 +147,25 @@ class DiscourseDiff
def tokenize_markdown(text)
t, tokens = [], []
i = 0
while i < text.length
while i < text.size
if text[i] =~ /\w/
t << text[i]
elsif text[i] =~ /[ \t]/ && t.join =~ /^\w+$/
begin
t << text[i]
i += 1
end while i < text.length && text[i] =~ /[ \t]/
end while i < text.size && text[i] =~ /[ \t]/
i -= 1
tokens << t.join
t = []
else
tokens << t.join if t.length > 0
tokens << t.join if t.size > 0
tokens << text[i]
t = []
end
i += 1
end
tokens << t.join if t.length > 0
tokens << t.join if t.size > 0
tokens
end
@ -180,7 +180,7 @@ class DiscourseDiff
def add_class_or_wrap_in_tags(html_or_text, klass)
result = html_or_text.dup
index_of_next_chevron = result.index(">")
if result.length > 0 && result[0] == '<' && index_of_next_chevron
if result.size > 0 && result[0] == '<' && index_of_next_chevron
index_of_class = result.index("class=")
if index_of_class.nil? || index_of_class > index_of_next_chevron
# we do not have a class for the current tag
@ -192,7 +192,7 @@ class DiscourseDiff
if classes.include?("diff-#{klass}")
result
else
result.insert(index_of_class + "class=".length + 1, "diff-#{klass} ")
result.insert(index_of_class + "class=".size + 1, "diff-#{klass} ")
end
end
else