Skip to content

Remove point-geometry methods and avoid unnecessary point allocations - #7085

Closed
mourner wants to merge 16 commits into
masterfrom
remove-point-methods
Closed

mourner wants to merge 16 commits into
masterfrom
remove-point-methods

Conversation

@mourner

@mourner mourner commented Aug 6, 2018 •

Copy link
Copy Markdown
Member

This PR starts our preparation for #5246. To open up a way to a different geometry storage, first we need to get rid of all uses of math methods from @mapbox/point-geometry when processing VT geometry, so that we're not tied to all points being Point objects from that module. This has additional benefits:

  • Those methods made it easy to make unnecessary allocations during vector calculations without realizing it. Operating on raw numbers directly makes all allocations explicit.
  • It encourages passing coordinates as numbers in arguments explicitly instead of creating intermediary point objects where possible, further saving on allocations.
  • It is less prone to bugs due to immutability — the current approach of using underscored methods for in-place calculations is fragile (e.g. I discovered at least one bug caused by it while refactoring).
  • We can optimize calculations like rotation of many points by the same angle (by calculating cos/sin only once).

In my test load of Mapbox Streets on DC z11, doing this in line_bucket.js reduces overall worker-side Point allocations from 1579279 to 415265, or by almost 4 times. This doesn't yet have a noticeable effect on the layout benchmark, but I'll try to capture perf more substantially after finishing the PR.

This change also makes the code a bit more verbose and less descriptive, but I think the future trade-off is worth it.

Note that I'm not adding equivalent util methods to replace the Point methods, preferring to inline calculations, to avoid intermediary objects in return values. Once the full elimination pass is complete, I'll see if I can reintroduce some util methods for cleaner code if it's possible to do without the overhead.

cc @ansis @jfirebaugh @ChrisLoer

To do

VT-related

  • line_bucket
  • fill_extrusion_bucket
  • line_style_layer
  • query_utils
  • intersection_tests
  • symbol_bucket
  • symbol_layout
  • clip_line
  • get_anchors
  • check_max_angle
  • projection
  • collision_feature
  • quads

Optional

  • transform
  • source_cache
  • camera
  • events
  • popup
  • marker
  • bind_handlers
  • box_zoom
  • drag_pan
  • drag_rotate
  • touch_zoom_rotate
  • smart_wrap
  • eliminate @mapbox/point-geometry dependency, replace with internal Point type

Launch Checklist

  • briefly describe the changes in this PR
  • write tests for all new functionality
  • document any changes to public APIs
  • post benchmark scores
  • manually test the debug page

@mourner
mourner force-pushed the remove-point-methods branch 2 times, most recently from 692b030 to b7fcaa9 Compare August 7, 2018 19:16
@mourner

mourner commented Aug 7, 2018

Copy link
Copy Markdown
Member Author

Note that I'm not adding dist(p1, p2) util in the last commit because it won't be ergonomic to use after switching to flat geometries anyway — we'll have to pass coordinate numbers instead of point objects to most methods.

@mourner mourner changed the title Remove point-geometry methods from buckets and avoid unnecessary point allocations Remove point-geometry methods and avoid unnecessary point allocations Aug 7, 2018
@mourner
mourner force-pushed the remove-point-methods branch from b7fcaa9 to a3c6ef3 Compare August 8, 2018 13:31
@mourner

mourner commented Aug 8, 2018

Copy link
Copy Markdown
Member Author

A simple console.time measurement of WorkerTile parse on a dense DC tile already shows a consistent 10% improvement vs master. Really looking forward to seeing how much we'll win on top of that after switching to flat geometries on the vector-tile-js side.

@mourner
mourner force-pushed the remove-point-methods branch from a3c6ef3 to a92ca39 Compare August 8, 2018 18:06

@jfirebaugh jfirebaugh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on continuing to push forward with this. The increase in verbosity doesn't look too bad to me -- would be worth a 10%+ performance increase.

ring.every(p => p.x > EXTENT) ||
ring.every(p => p.y < 0) ||
ring.every(p => p.y > EXTENT);
for (const p of ring) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't testing the same thing. For example, a ring with xs all < 0 should return true, but this logic will return false, because x < 0 ⇒ x <= EXTENT.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed.

@mourner
mourner force-pushed the remove-point-methods branch from a92ca39 to 1963443 Compare August 8, 2018 22:48
@mourner

mourner commented Aug 8, 2018 •

Copy link
Copy Markdown
Member Author

It is less prone to bugs due to immutability — the current approach of using underscored methods for in-place calculations is fragile (e.g. I discovered at least one bug caused by it while refactoring).

@ansis @jfirebaugh To illustrate this point, here's the nasty bug I found:

const aToB = i === 0 ? zero : b.sub(a)._unit()._perp();
const bToC = i === ring.length - 1 ? zero : c.sub(b)._unit()._perp();
const extrude = aToB._add(bToC)._unit();

Notice that on the third line, aToB is mutated (because it uses _ methods). But for the first point of the ring, aToB is simply a reference to zero, so by the time zero is referenced again on the last point, it is no longer zero.

Even more fun, this buggy behavior actually inadvertently produced correct results because bToC needs to equal aToB on the last point to be consistent with the rendering:

// Calculate the normal towards the next vertex in this line. In case
// there is no next vertex, pretend that the line is continuing straight,
// meaning that we are just using the previous normal.
nextNormal = nextVertex ? nextVertex.sub(currentVertex)._unit()._perp() : prevNormal;

So it took quite a while to figure out why a rewritten, flat version was failing query tests.

@mourner

mourner commented Aug 9, 2018

Copy link
Copy Markdown
Member Author

@jfirebaugh I completed the first stage — the map still works if I replace Point with {x, y} in vector-tile. The next stage is to try outputting flat geometries in VT, update the GL JS code accordingly and benchmark, although I'm not sure whether this should be done as a part of this PR rather than a follow-up — I think it can be merged as is.

@mourner

mourner commented Aug 9, 2018

Copy link
Copy Markdown
Member Author

Layout benchmark isn't affected and I'm not entirely sure why — manually measuring dense z11 tiles with console.time shows a definite improvement. Looking at profiler during a benchmark run, at least 30% of the benchmark is spent on Earcut (I guess because it includes a low-zoom tile with a particularly nasty water polygon), and actually adding features to buckets takes only ~10% of the benchmark, so I guess a 10% improvement is just eaten by natural variance.
image

@mourner
mourner requested a review from jfirebaugh August 9, 2018 13:38
@mourner
mourner requested a review from ansis August 9, 2018 13:38
@jfirebaugh

Copy link
Copy Markdown
Contributor

Can you make a benchmark that shows the improvement so far? It would be useful to have one that's not dominated by earcut anyway.

@mourner

mourner commented Jun 5, 2019

Copy link
Copy Markdown
Member Author

Closing as stale. I'm breaking some of these changes up into different PRs. The first and most important one (for line bucket) is #8303.

@mourner mourner closed this Jun 5, 2019
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.

2 participants