Skip to content

Avoid Coordinate allocations in Area and Length sequence computations#1211

Open
nishantmehta wants to merge 1 commit into
locationtech:masterfrom
nishantmehta:perf/area-length-noalloc
Open

Avoid Coordinate allocations in Area and Length sequence computations#1211
nishantmehta wants to merge 1 commit into
locationtech:masterfrom
nishantmehta:perf/area-length-noalloc

Conversation

@nishantmehta

Copy link
Copy Markdown

What

Area.ofRingSigned(CoordinateSequence) and Length.ofLine(CoordinateSequence) allocated Coordinate objects (three and one respectively) per call via CoordinateSequence.createCoordinate(), purely to read x/y values while iterating. These back Geometry.getArea() and getLength(), which are commonly called in hot paths.

This reads coordinates through the primitive CoordinateSequence.getX(int)/getY(int) accessors instead. The arithmetic is unchanged — Area still uses the Shoelace formula (caching the y values so each is read once), and Length still sums segment hypotenuses — and no Coordinate instances are created.

Benchmark

Measured with a ThreadMXBean allocation driver (200k warmed ops):

Geometry.getArea     28.5 B/op -> 0 B/op
Geometry.getLength   40   B/op -> 0 B/op

Testing

The full jts-core test suite (2294 tests) passes unchanged.

Area.ofRingSigned(CoordinateSequence) and Length.ofLine(CoordinateSequence)
allocated Coordinate objects (three and one respectively) per call via
CoordinateSequence.createCoordinate(), purely to read x/y values while
iterating. These back Geometry.getArea() and getLength(), which are commonly
called in hot paths.

Read coordinates through the primitive CoordinateSequence.getX(int)/getY(int)
accessors instead. The arithmetic is unchanged (Area still uses the Shoelace
formula, caching the y values so each is read once; Length still sums segment
hypotenuses), and no Coordinate instances are created.

Measured with a ThreadMXBean allocation driver (200k warmed ops):
Geometry.getArea 28.5 -> 0 B/op, Geometry.getLength 40 -> 0 B/op. The full
jts-core test suite (2294 tests) passes unchanged.

Signed-off-by: Nishant Mehta <nishantmehta.n@gmail.com>
@nishantmehta nishantmehta marked this pull request as ready for review June 28, 2026 21:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant