Area#add(Area) Optimization

Jeremy Wood mickleness at gmail.com
Fri Feb 11 23:51:02 UTC 2022


I’m working a lot with the Area class lately. Is there any interest in 
the following optimization?

Area#add(Area) currently resembles:

public void add(Area rhs) {
   curves = new AreaOp.AddOp().calculate(this.curves, rhs.curves);
   invalidateBounds();
}

I propose replacing it with:

public void add(Area rhs) {
   if (isEmpty()) {
     curves = rhs.curves;
   } else if (rhs.isEmpty()) {
     return;
   } else {
     curves = new AreaOp.AddOp().calculate(this.curves, rhs.curves);
   }
   invalidateBounds();
}

If either operand is empty then this is effectively a null op. But the 
current implementation may still take several seconds to return 
depending on the complexity of the non-empty operand.

For example the following two snippets of code may have significantly 
different execution times:

Area sum = new Area();
for(Shape shape : incomingShapes) {
   sum.add(new Area(shape));
}

vs

Area sum = new Area(shapes.get(0));
for(int a = 1; a < incomingShapes.size(); a++) {
   Shape shape = incomingShapes.get(a);
   sum.add(new Area(shape));
}

And similarly we could review the other operations 
(subtract/intersect/exclusiveOr/transform) for obvious potential null 
ops.

If I understand openjdk rules correctly: I'm not authorized to submit an 
openJDK ticket. Does this interest anyone else on this list enough to 
submit this as an openJDK ticket? (I'm happy to work on it if I can 
help.)

Regards,
  - Jeremy


More information about the client-libs-dev mailing list