Per discussion with kirilll, some checks that we desire to run at debug time only have been converted to asserts, and commented out pending an answer from the java libraries team on how best to handle debug time only checks.  One of these changes overlaps a change started by ikaushan in CL 25025671 -- hopefully he beats me to the punch.

S2EdgeIndex incorrectly copied the S2 Minimum Width metric, rather than referring to the metric in S2Projections.  This change should have no effect, except in the case that the projection in S2 is changed, in which case it may work fine now, but would certainly have been incredibly wrong before.

Vital performance fixes:

- Several classes and methods are now final, to avoid subclasses tampering with them and/or to allow the jvm to apply additional runtime optimizations.

- Fixed performance of S2PolygonBuilder#assembleLoop(), which was removing N vertices from the start of an ArrayList *one at a time*, by calling ArrayList.remove(0) N times.  Which of course takes N*list.size() steps, aka very quadratic performance in the common case of many loops in the builder. Instead we now call list.subList(n, list.size()), to just skip past the vertices we don't care about.

- Fixed S2Polygon.S2PolygonIndex creating a copy of the whole array of vertices in *every call* to edgeFromTo(), because S2Loop's copy constructor was being used, for no apparent reason, which clones the vertices. Since edgeFromTo() is called twice per vertex to create the polygon index, a polygon with N vertices was inadverdantly copying 2*N^2 vertices to build the polygon index.

- Fixed performance of S2Polygon#clipBoundary(), which was testing every vertex of a polygon b for containment in polygon a, only to emit a log message that essentially never occurs.  In C++, this check is a debug time only check, so the pattern has been followed of making this a commented-out assertion instead of doing this extremely expensive check in production code.  Also greatly simplified the addition of edges to the S2PolygonBuilder by relying on the checks already being done in the builder.

- Fixed performance of S2Loop#vertex(int), which was doing a check before indexing into the vertices array, which itself does a range check. Since we don't need or want want both checks, the array dereference exception is caught and rethrown so the external behavior of the method is unchanged, but it is now significantly faster.

- Fixed performance of S2Polygon.S2LoopSequenceIndex, which was suffering from using List<Integer>. By using int[] instead, we save extra method calls and a lot of boxing/unboxing in this performance critical area.

- S2EdgeIndex now stores cells and edges more efficiently in parallel arrays, and edge lookups have been made significantly faster by a local implementation of binary search.

- S2EdgeIndex methods getEdgesInParentCells and getEdgesInChildrenCells now take a Set instead of a List that is then filtered through a set. This saves an extra copy of all the edges, and keeps us from accumulating redundant copies of the same edge as the search progresses.
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=25397727
4 files changed
tree: c826d03218c02f8c5d499183b469f9116170811e
  1. lib/
  2. src/
  3. tests/
  4. .classpath
  5. .gitignore
  6. .project
  7. build.xml
  8. LICENSE