Bug 562086 - Use copy-on-write data structures to improve performance

Over the lifecycle of a document, many more document events are
processed than modifications happen to the registered position updaters.
Therefore it is beneficial to avoid a defensive copy on each read but
use a CopyOnWriteArrayList instead to allow iteration while concurrently
modifying the position updaters.

The change does also contain a little bit of cleanup when working with
the instance field fPositionUpdaters within AbstractDocument.

Change-Id: Id09b72ef70c872530aab6dd01c5e64abc4fbfb6c
Signed-off-by: Sebastian Zarnekow <sebastian.zarnekow@gmail.com>
diff --git a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java
index b73f4b1..b94cc7e 100644
--- a/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java
+++ b/org.eclipse.text/src/org/eclipse/jface/text/AbstractDocument.java
@@ -22,6 +22,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import java.util.concurrent.CopyOnWriteArrayList;
 import java.util.regex.PatternSyntaxException;
 
 import org.eclipse.core.runtime.Assert;
@@ -293,7 +294,7 @@
 
 		fPositions= new HashMap<>();
 		fEndPositions= new HashMap<>();
-		fPositionUpdaters= new ArrayList<>();
+		fPositionUpdaters= new CopyOnWriteArrayList<>();
 		fDocumentListeners= new ListenerList<>(ListenerList.IDENTITY);
 		fPrenotifiedDocumentListeners= new ListenerList<>(ListenerList.IDENTITY);
 		fDocumentPartitioningListeners= new ListenerList<>(ListenerList.IDENTITY);
@@ -951,9 +952,7 @@
 
 	@Override
 	public IPositionUpdater[] getPositionUpdaters() {
-		IPositionUpdater[] updaters= new IPositionUpdater[fPositionUpdaters.size()];
-		fPositionUpdaters.toArray(updaters);
-		return updaters;
+		return fPositionUpdaters.toArray(new IPositionUpdater[fPositionUpdaters.size()]);
 	}
 
 	@Override
@@ -971,16 +970,12 @@
 
 	@Override
 	public void insertPositionUpdater(IPositionUpdater updater, int index) {
-
-		for (int i= fPositionUpdaters.size() - 1; i >= 0; i--) {
-			if (fPositionUpdaters.get(i) == updater)
+		for (IPositionUpdater u: fPositionUpdaters) {
+			if (u == updater) {
 				return;
+			}
 		}
-
-		if (index == fPositionUpdaters.size())
-			fPositionUpdaters.add(updater);
-		else
-			fPositionUpdaters.add(index, updater);
+		fPositionUpdaters.add(index, updater);
 	}
 
 	@Override
@@ -1154,10 +1149,7 @@
 	 *            the positions
 	 */
 	protected void updatePositions(DocumentEvent event) {
-		List<IPositionUpdater> list= new ArrayList<>(fPositionUpdaters);
-		Iterator<IPositionUpdater> e= list.iterator();
-		while (e.hasNext()) {
-			IPositionUpdater u= e.next();
+		for(IPositionUpdater u: fPositionUpdaters) {
 			u.update(event);
 		}
 	}