Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix IndexedPointInAreaLocator to reduce calls to synchronized method #574

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ public IndexedPointInAreaLocator(Geometry g)
*/
public int locate(Coordinate p)
{
createIndex();
// avoid calling synchronized method improves performance
if (index == null) createIndex();

RayCrossingCounter rcc = new RayCrossingCounter(p);

SegmentVisitor visitor = new SegmentVisitor(rcc);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test.jts.perf.algorithm;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.locationtech.jts.algorithm.locate.IndexedPointInAreaLocator;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Location;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.util.GeometricShapeFactory;

import test.jts.perf.PerformanceTestCase;
import test.jts.perf.PerformanceTestRunner;

public class IndexedPointInAreaPerfTest extends PerformanceTestCase {
public static void main(String args[]) {
PerformanceTestRunner.run(IndexedPointInAreaPerfTest.class);
}

public IndexedPointInAreaPerfTest(String name)
{
super(name);
setRunSize(new int[] { 100_000 });
setRunIterations(1);
}

List<Coordinate> coords;
Polygon polygon;

public void startRun(int num)
{
System.out.println("Running with size " + num);
GeometricShapeFactory factory = new GeometricShapeFactory();
factory.setSize(100);
polygon = factory.createCircle();

coords = new ArrayList<>();
Random rand = new Random(1324);
for (int i = 0; i < num; i++) {
coords.add(new Coordinate(rand.nextDouble()*100, rand.nextDouble()*100));
}
}

public void runParallel() {
for (int i = 0; i < 1000; i++) {
IndexedPointInAreaLocator locator = new IndexedPointInAreaLocator(polygon);
coords.parallelStream().forEach(c -> isInside(locator, c));
}
}

private boolean isInside(IndexedPointInAreaLocator locator, Coordinate coord) {
return locator.locate(coord) == Location.INTERIOR;
}
}