Index: /trunk/src/org/openstreetmap/josm/data/coor/QuadTiling.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/data/coor/QuadTiling.java	(revision 2421)
+++ /trunk/src/org/openstreetmap/josm/data/coor/QuadTiling.java	(revision 2422)
@@ -2,8 +2,4 @@
 package org.openstreetmap.josm.data.coor;
 
-import org.openstreetmap.josm.data.osm.Node;
-
-import org.openstreetmap.josm.Main;
-import org.openstreetmap.josm.data.projection.Projection;
 
 public class QuadTiling
@@ -41,8 +37,10 @@
             //    out("shift: " + shift + " bits: " + bits);
             // remember x is the MSB
-            if ((bits & 0x2) != 0)
+            if ((bits & 0x2) != 0) {
                 x += x_unit;
-            if ((bits & 0x1) != 0)
+            }
+            if ((bits & 0x1) != 0) {
                 y += y_unit;
+            }
             x_unit /= 2;
             y_unit /= 2;
@@ -55,8 +53,8 @@
     static long xy2tile(long x, long y)
     {
-       long tile = 0;
-       int i;
-       for (i = NR_LEVELS-1; i >= 0; i--)
-       {
+        long tile = 0;
+        int i;
+        for (i = NR_LEVELS-1; i >= 0; i--)
+        {
             long xbit = ((x >> i) & 1);
             long ybit = ((y >> i) & 1);
@@ -64,6 +62,6 @@
             // Note that x is the MSB
             tile |= (xbit<<1) | ybit;
-       }
-       return tile;
+        }
+        return tile;
     }
     static long coorToTile(LatLon coor)
@@ -73,22 +71,24 @@
     static long lon2x(double lon)
     {
-       //return Math.round((lon + 180.0) * QuadBuckets.WORLD_PARTS / 360.0)-1;
-       long ret = (long)Math.floor((lon + 180.0) * WORLD_PARTS / 360.0);
-       if (ret == WORLD_PARTS)
-           ret--;
-       return ret;
+        //return Math.round((lon + 180.0) * QuadBuckets.WORLD_PARTS / 360.0)-1;
+        long ret = (long)Math.floor((lon + 180.0) * WORLD_PARTS / 360.0);
+        if (ret == WORLD_PARTS) {
+            ret--;
+        }
+        return ret;
     }
     static long lat2y(double lat)
     {
-       //return Math.round((lat + 90.0) * QuadBuckets.WORLD_PARTS / 180.0)-1;
-       long ret = (long)Math.floor((lat + 90.0) * WORLD_PARTS / 180.0);
-       if (ret == WORLD_PARTS)
-           ret--;
-       return ret;
+        //return Math.round((lat + 90.0) * QuadBuckets.WORLD_PARTS / 180.0)-1;
+        long ret = (long)Math.floor((lat + 90.0) * WORLD_PARTS / 180.0);
+        if (ret == WORLD_PARTS) {
+            ret--;
+        }
+        return ret;
     }
     static public long quadTile(LatLon coor)
     {
         return xy2tile(lon2x(coor.lon()),
-                       lat2y(coor.lat()));
+                lat2y(coor.lat()));
     }
     static public int index(int level, long quad)
Index: /trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java
===================================================================
--- /trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java	(revision 2421)
+++ /trunk/src/org/openstreetmap/josm/gui/NavigatableComponent.java	(revision 2422)
@@ -25,4 +25,5 @@
 import org.openstreetmap.josm.data.osm.Way;
 import org.openstreetmap.josm.data.osm.WaySegment;
+import org.openstreetmap.josm.data.osm.QuadBuckets.BBox;
 import org.openstreetmap.josm.data.projection.Projection;
 import org.openstreetmap.josm.gui.help.Helpful;
@@ -36,5 +37,6 @@
 public class NavigatableComponent extends JComponent implements Helpful {
 
-    public static final int snapDistance = sqr(Main.pref.getInteger("node.snap-distance", 10));
+    public static final int snapDistance = Main.pref.getInteger("node.snap-distance", 10);
+    public static final int snapDistanceSq = sqr(snapDistance);
 
     private static int sqr(int a) { return a*a;}
@@ -296,4 +298,9 @@
     }
 
+    private BBox getSnapDistanceBBox(Point p) {
+        return new BBox(getLatLon(p.x - snapDistance / 2, p.y - snapDistance / 2),
+                getLatLon(p.x + snapDistance / 2, p.y + snapDistance / 2));
+    }
+
     /**
      * Return the nearest point to the screen point given.
@@ -301,10 +308,11 @@
      */
     public final Node getNearestNode(Point p) {
-        double minDistanceSq = snapDistance;
-        Node minPrimitive = null;
         DataSet ds = getCurrentDataSet();
         if (ds == null)
             return null;
-        for (Node n : ds.getNodes()) {
+
+        double minDistanceSq = snapDistanceSq;
+        Node minPrimitive = null;
+        for (Node n : ds.searchNodes(getSnapDistanceBBox(p))) {
             if (!n.isUsable()) {
                 continue;
@@ -319,5 +327,5 @@
             else if (dist == minDistanceSq && minPrimitive != null
                     && ((n.isNew() && ds.isSelected(n))
-                    || (!ds.isSelected(minPrimitive) && (ds.isSelected(n) || n.isNew())))) {
+                            || (!ds.isSelected(minPrimitive) && (ds.isSelected(n) || n.isNew())))) {
                 minPrimitive = n;
             }
@@ -337,5 +345,6 @@
         if (ds == null)
             return null;
-        for (Way w : ds.getWays()) {
+
+        for (Way w : ds.searchWays(getSnapDistanceBBox(p))) {
             if (!w.isUsable()) {
                 continue;
@@ -359,5 +368,5 @@
                 double b = p.distanceSq(A);
                 double perDist = a - (a - b + c) * (a - b + c) / 4 / c; // perpendicular distance squared
-                if (perDist < snapDistance && a < c + snapDistance && b < c + snapDistance) {
+                if (perDist < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq) {
                     if (ds.isSelected(w)) {
                         perDist -= 0.00001;
@@ -450,5 +459,5 @@
     /**
      * @return A list of all objects that are nearest to
-     * the mouse.  Does a simple sequential scan on all the data.
+     * the mouse.
      *
      * @return A collection of all items or <code>null</code>
@@ -461,5 +470,5 @@
         if (ds == null)
             return null;
-        for (Way w : ds.getWays()) {
+        for (Way w : ds.searchWays(getSnapDistanceBBox(p))) {
             if (!w.isUsable()) {
                 continue;
@@ -480,5 +489,5 @@
                 double b = p.distanceSq(A);
                 double perDist = a - (a - b + c) * (a - b + c) / 4 / c; // perpendicular distance squared
-                if (perDist < snapDistance && a < c + snapDistance && b < c + snapDistance) {
+                if (perDist < snapDistanceSq && a < c + snapDistanceSq && b < c + snapDistanceSq) {
                     nearest.add(w);
                     break;
@@ -487,7 +496,7 @@
             }
         }
-        for (Node n : ds.getNodes()) {
+        for (Node n : ds.searchNodes(getSnapDistanceBBox(p))) {
             if (n.isUsable()
-                    && getPoint(n).distanceSq(p) < snapDistance) {
+                    && getPoint(n).distanceSq(p) < snapDistanceSq) {
                 nearest.add(n);
             }
@@ -498,5 +507,5 @@
     /**
      * @return A list of all nodes that are nearest to
-     * the mouse.  Does a simple sequential scan on all the data.
+     * the mouse.
      *
      * @return A collection of all nodes or <code>null</code>
@@ -509,7 +518,8 @@
         if (ds == null)
             return null;
-        for (Node n : ds.getNodes()) {
+
+        for (Node n : ds.searchNodes(getSnapDistanceBBox(p))) {
             if (n.isUsable()
-                    && getPoint(n).distanceSq(p) < snapDistance) {
+                    && getPoint(n).distanceSq(p) < snapDistanceSq) {
                 nearest.add(n);
             }
