Skip to content

Commit ffc40ff

Browse files
author
taylor.smock
committedFeb 20, 2024·
Fix #23472: Decrease cost of Geometry#polygonIntersectionResult (specifically for geometry.mapcss)
When profiling the validator with an overpass download of Mesa County, Colorado, ~50% of the total CPU time are taken up by `Geometry#polygonIntersectionResult`. Specifically (inside the function), the improvements for this patch are as follows: * CPU cycles: -95.7% * Memory allocations: -97.9% When taken as a whole, the total time taken by the validator was reduced by ~75%. git-svn-id: https://josm.openstreetmap.de/svn/trunk@18990 0c6e7542-c601-0410-84e7-c038aed88b3b
1 parent 80ff7f1 commit ffc40ff

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed
 

‎src/org/openstreetmap/josm/tools/Geometry.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -693,14 +693,22 @@ public static PolygonIntersection polygonIntersection(Area a1, Area a2, double e
693693
* @since 15938
694694
*/
695695
public static Pair<PolygonIntersection, Area> polygonIntersectionResult(Area a1, Area a2, double eps) {
696+
// Simple intersect check (if their bounds don't intersect, don't bother going further; there will be no intersection)
697+
// This avoids the more expensive Area#intersect call some of the time (decreases CPU and memory allocation by ~95%)
698+
// in Mesa County, CO geometry validator test runs.
699+
final Rectangle2D a12d = a1.getBounds2D();
700+
final Rectangle2D a22d = a2.getBounds2D();
701+
if (!a12d.intersects(a22d) || !a1.intersects(a22d) || !a2.intersects(a12d)) {
702+
return new Pair<>(PolygonIntersection.OUTSIDE, new Area());
703+
}
696704
Area inter = new Area(a1);
697705
inter.intersect(a2);
698706

699707
if (inter.isEmpty() || !checkIntersection(inter, eps)) {
700708
return new Pair<>(PolygonIntersection.OUTSIDE, inter);
701-
} else if (a2.getBounds2D().contains(a1.getBounds2D()) && inter.equals(a1)) {
709+
} else if (a22d.contains(a12d) && inter.equals(a1)) {
702710
return new Pair<>(PolygonIntersection.FIRST_INSIDE_SECOND, inter);
703-
} else if (a1.getBounds2D().contains(a2.getBounds2D()) && inter.equals(a2)) {
711+
} else if (a12d.contains(a22d) && inter.equals(a2)) {
704712
return new Pair<>(PolygonIntersection.SECOND_INSIDE_FIRST, inter);
705713
} else {
706714
return new Pair<>(PolygonIntersection.CROSSING, inter);

0 commit comments

Comments
 (0)
Please sign in to comment.