From 37b41835429822913107e58024c6118820df5b9e Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Sun, 22 Feb 2015 13:02:08 +0100 Subject: [PATCH 1/2] :racehorse: Improve mergeIntersectingSelections performance Order selections first and intersect only with the last one. This brings the cost of mergeIntersectingSelections down to O(n log n). --- src/text-editor.coffee | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index b86b97d17..19334a813 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -2206,11 +2206,18 @@ class TextEditor extends Model result = fn() @suppressSelectionMerging = false + findIntersectingSelection = (elements, selection) -> + return if elements.length == 0 + + otherSelection = _.last(elements) + exclusive = not selection.isEmpty() and not otherSelection.isEmpty() + intersects = otherSelection.intersectsWith(selection, exclusive) + + return otherSelection if intersects + + reducer = (disjointSelections, selection) -> - intersectingSelection = _.find disjointSelections, (otherSelection) -> - exclusive = not selection.isEmpty() and not otherSelection.isEmpty() - intersects = otherSelection.intersectsWith(selection, exclusive) - intersects + intersectingSelection = findIntersectingSelection(disjointSelections, selection) if intersectingSelection? intersectingSelection.merge(selection, options) @@ -2218,7 +2225,7 @@ class TextEditor extends Model else disjointSelections.concat([selection]) - _.reduce(@getSelections(), reducer, []) + _.reduce(@getSelectionsOrderedByBufferPosition(), reducer, []) # Add a {Selection} based on the given {Marker}. # From 7fe313552636cb657aaad0ddf98c8cb88c6086fc Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Sun, 22 Feb 2015 15:04:37 +0100 Subject: [PATCH 2/2] :art: Give code a better structure --- src/text-editor.coffee | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/text-editor.coffee b/src/text-editor.coffee index 19334a813..bd3f76190 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -2206,26 +2206,19 @@ class TextEditor extends Model result = fn() @suppressSelectionMerging = false - findIntersectingSelection = (elements, selection) -> - return if elements.length == 0 - - otherSelection = _.last(elements) - exclusive = not selection.isEmpty() and not otherSelection.isEmpty() - intersects = otherSelection.intersectsWith(selection, exclusive) - - return otherSelection if intersects - - reducer = (disjointSelections, selection) -> - intersectingSelection = findIntersectingSelection(disjointSelections, selection) + adjacentSelection = _.last(disjointSelections) + exclusive = not selection.isEmpty() and not adjacentSelection.isEmpty() + intersects = adjacentSelection.intersectsWith(selection, exclusive) - if intersectingSelection? - intersectingSelection.merge(selection, options) + if intersects + adjacentSelection.merge(selection, options) disjointSelections else disjointSelections.concat([selection]) - _.reduce(@getSelectionsOrderedByBufferPosition(), reducer, []) + [head, tail...] = @getSelectionsOrderedByBufferPosition() + _.reduce(tail, reducer, [head]) # Add a {Selection} based on the given {Marker}. #