Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Ignore flipped glyphs when placing labels for perf - #257

Merged
mourner merged 2 commits into
masterfrom
optimize-placement
May 28, 2014
Merged

mourner merged 2 commits into
masterfrom
optimize-placement

Conversation

@mourner

@mourner mourner commented May 27, 2014

Copy link
Copy Markdown
Member

Should make placement faster due to much less objects put into rtree. In llmr-js, it made about 25% parsing improvement for Outdoors (mapbox/mapbox-gl-js#385). My first ever C++ pull, please let me know if there any stylistic problems. :)

@springmeyer

Copy link
Copy Markdown
Contributor

Nice work @mourner! Looks great. One issue I see is that there will likely be an extra copy of the glyphs and boxes members of the GlyphsAndBoxes class now right here. It's possible the compiler might optimize away this copy, but an alternative approach would be to do this (which would ensure no extra copies):

diff --git a/src/text/placement.cpp b/src/text/placement.cpp
index dbf7286..c0fe81e 100644
--- a/src/text/placement.cpp
+++ b/src/text/placement.cpp
@@ -72,14 +72,6 @@ struct GlyphInstance {
     const float angle = 0.0f;
 };

-struct GlyphsAndBoxes {
-    explicit GlyphsAndBoxes(const PlacedGlyphs &glyphs, const GlyphBoxes &boxes)
-        : glyphs(glyphs), boxes(boxes) {}
-
-    const PlacedGlyphs glyphs;
-    const GlyphBoxes boxes;
-};
-
 typedef std::vector<GlyphInstance> GlyphInstances;

 void getSegmentGlyphs(std::back_insert_iterator<GlyphInstances> glyphs,
@@ -153,10 +145,12 @@ void getSegmentGlyphs(std::back_insert_iterator<GlyphInstances> glyphs,
     }
 }

-GlyphsAndBoxes getGlyphs(Anchor &anchor, float advance, const Shaping &shaping,
-                       const IndexedFaces &faces, float fontScale,
-                       bool horizontal, const std::vector<Coordinate> &line,
-                       float maxAngleDelta, float rotate) {
+void getGlyphs( PlacedGlyphs &glyphs,
+                GlyphBoxes &boxes,
+                Anchor &anchor, float advance, const Shaping &shaping,
+                const IndexedFaces &faces, float fontScale,
+                bool horizontal, const std::vector<Coordinate> &line,
+                float maxAngleDelta, float rotate) {
     // The total text advance is the width of this label.

     // TODO: figure out correct ascender height.
@@ -170,9 +164,6 @@ GlyphsAndBoxes getGlyphs(Anchor &anchor, float advance, const Shaping &shaping,
     //     origin.x -= advance;
     // }

-    PlacedGlyphs glyphs;
-    GlyphBoxes boxes;
-
     const uint32_t buffer = 3;

     for (const GlyphPlacement &placed : shaping) {
@@ -264,8 +255,6 @@ GlyphsAndBoxes getGlyphs(Anchor &anchor, float advance, const Shaping &shaping,
             }
         }
     }
-
-    return GlyphsAndBoxes{glyphs, boxes};
 }

 void Placement::addFeature(TextBucket& bucket,
@@ -303,14 +292,15 @@ void Placement::addFeature(TextBucket& bucket,
     }

     for (Anchor anchor : anchors) {
-        GlyphsAndBoxes glyphsAndBoxes =
-            getGlyphs(anchor, advance, shaping, faces, fontScale, horizontal,
+        PlacedGlyphs glyphs;
+        GlyphBoxes boxes;
+        getGlyphs(glyphs, boxes, anchor, advance, shaping, faces, fontScale, horizontal,
                       line, maxAngleDelta, rotate);
         PlacementProperty place =
-            collision.place(glyphsAndBoxes.boxes, anchor, anchor.scale, maxPlacementScale,
+            collision.place(boxes, anchor, anchor.scale, maxPlacementScale,
                             padding, horizontal, info.alwaysVisible);
         if (place) {
-            bucket.addGlyphs(glyphsAndBoxes.glyphs, place.zoom, place.rotationRange,
+            bucket.addGlyphs(glyphs, place.zoom, place.rotationRange,
                              zoom - zOffset);
         }
     }

@mourner

mourner commented May 27, 2014

Copy link
Copy Markdown
Member Author

@springmeyer wow, thanks for a really helpful comment Dane — agreed, will do!

@mourner

mourner commented May 27, 2014

Copy link
Copy Markdown
Member Author

Done!

@kkaefer

kkaefer commented May 28, 2014

Copy link
Copy Markdown
Member

Looks good, please merge.

@springmeyer I've mostly been counting on return value optimization being done, so there's a lot of code that returns "copies", which should get optimized away. Do you think it's worth it to change the code to the other style?

mourner added a commit that referenced this pull request May 28, 2014
Ignore flipped glyphs when placing labels for perf
@mourner
mourner merged commit 7dc706b into master May 28, 2014
@mourner
mourner deleted the optimize-placement branch May 28, 2014 14:17
@springmeyer

Copy link
Copy Markdown
Contributor

@springmeyer I've mostly been counting on return value optimization being done, so there's a lot of code that returns "copies", which should get optimized away. Do you think it's worth it to change the code to the other style?

I noticed the heavy reliance on RVO. I think it should be fine, but I worried about this case since I presume the members of the class will trigger copies even with RVO. Would be worth testing this assumption when things quiet down. /cc @artemp in case he knows more on this.

@artemp
artemp restored the optimize-placement branch May 29, 2014 13:16
@artemp
artemp deleted the optimize-placement branch May 29, 2014 13:17
@yhahn yhahn mentioned this pull request May 29, 2014
5 of 6 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants