Index: /applications/editors/josm/plugins/wayselector/build.xml
===================================================================
--- /applications/editors/josm/plugins/wayselector/build.xml	(revision 23102)
+++ /applications/editors/josm/plugins/wayselector/build.xml	(revision 23103)
@@ -31,5 +31,5 @@
 
 	<!-- enter the SVN commit message -->
-	<property name="commit.message" value="Do not mutate the collection of selected ways. Convert TAB to spaces." />
+	<property name="commit.message" value="Optimize WaySelection.findWay()." />
 	<!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
 	<property name="plugin.main.version" value="3095" />
Index: /applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelection.java
===================================================================
--- /applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelection.java	(revision 23102)
+++ /applications/editors/josm/plugins/wayselector/src/org/openstreetmap/josm/plugins/wayselector/WaySelection.java	(revision 23103)
@@ -69,14 +69,26 @@
      @param node perimeter node from which to extend the selection
      @return a way by which to extend the selection, or null */
-    private Way findWay(Collection<OsmPrimitive> selection, Node node) {
-        TreeSet<Way> foundWays = new TreeSet<Way>();
+    private static Way findWay(Collection<OsmPrimitive> selection, Node node) {
+        Way foundWay = null;
 
         for (Way way : OsmPrimitive.getFilteredList(node.getReferrers(),
-                                                    Way.class))
-            if (way.getNodesCount() >= 2 && !selection.contains(way) &&
-                way.isFirstLastNode(node))
-                foundWays.add(way);
+                                                    Way.class)) {
+            if (way.getNodesCount() < 2 || !way.isFirstLastNode(node)
+                || selection.contains(way))
+                continue;
 
-        return foundWays.size() == 1 ? foundWays.first() : null;
+            /* A previously unselected way was found that is connected
+            to the node. */
+            if (foundWay != null)
+                /* This is not the only qualifying way. There is a
+                branch at the node, and we cannot extend the selection. */
+                return null;
+
+            /* Remember the first found qualifying way. */
+            foundWay = way;
+        }
+
+        /* Return the only way found, or null if none was found. */
+        return foundWay;
     }
 
