Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of Wayname calculation #2635

Merged
merged 3 commits into from
Sep 21, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion MapboxNavigation/RouteMapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,15 @@ extension RouteMapViewController: NavigationViewDelegate {
streetLabelLayer.lineOpacity = NSExpression(forConstantValue: 1)
streetLabelLayer.lineWidth = NSExpression(forConstantValue: 20)
streetLabelLayer.lineColor = NSExpression(forConstantValue: UIColor.white)

if ![DirectionsProfileIdentifier.walking, DirectionsProfileIdentifier.cycling].contains( router.routeProgress.routeOptions.profileIdentifier) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Btw, I think it’s possible to shorten this line based on the type of profileIdentifier:

Suggested change
if ![DirectionsProfileIdentifier.walking, DirectionsProfileIdentifier.cycling].contains( router.routeProgress.routeOptions.profileIdentifier) {
if ![.walking, .cycling].contains(router.routeProgress.routeOptions.profileIdentifier) {

// filter out to road classes valid for motor transport
let roadPredicates = ["motorway", "motorway_link", "trunk", "trunk_link", "primary", "primary_link", "secondary", "secondary_link", "tertiary", "tertiary_link", "street", "street_limited", "roundabout", "mini_roundabout"].compactMap { roadClass -> NSPredicate? in
Copy link
Contributor

Choose a reason for hiding this comment

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

I’m not sure about mini_roundabout being necessary, since it’s tagged as a node in OpenStreetMap and would thus be a point feature in the Streets source. We’re only querying for polyline features.

return NSPredicate(format: "%K == %@", "class", roadClass)
}
let compoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: roadPredicates)
Copy link
Contributor

Choose a reason for hiding this comment

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

Something along these lines should work and would probably perform slightly better:

Suggested change
let roadPredicates = ["motorway", "motorway_link", "trunk", "trunk_link", "primary", "primary_link", "secondary", "secondary_link", "tertiary", "tertiary_link", "street", "street_limited", "roundabout", "mini_roundabout"].compactMap { roadClass -> NSPredicate? in
return NSPredicate(format: "%K == %@", "class", roadClass)
}
let compoundPredicate = NSCompoundPredicate(orPredicateWithSubpredicates: roadPredicates)
let roadPredicates = ["motorway", "motorway_link", "trunk", "trunk_link", "primary", "primary_link", "secondary", "secondary_link", "tertiary", "tertiary_link", "street", "street_limited", "roundabout", "mini_roundabout"]
let compoundPredicate = NSPredicate(format: "class IN %@", roadPredicates)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'll adopt these changes. Worth getting the best performance.

streetLabelLayer.predicate = compoundPredicate
}
style.insertLayer(streetLabelLayer, at: 0)
}

Expand All @@ -692,6 +701,7 @@ extension RouteMapViewController: NavigationViewDelegate {
var smallestLabelDistance = Double.infinity
var currentName: String?
var currentShieldName: NSAttributedString?
let slicedLine = stepShape.sliced(from: closestCoordinate)!

for feature in features {
var allLines: [MGLPolyline] = []
Expand All @@ -706,7 +716,6 @@ extension RouteMapViewController: NavigationViewDelegate {
guard line.pointCount > 0 else { continue }
let featureCoordinates = Array(UnsafeBufferPointer(start: line.coordinates, count: Int(line.pointCount)))
let featurePolyline = LineString(featureCoordinates)
let slicedLine = stepShape.sliced(from: closestCoordinate)!
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, moving the slicing out of the loop should have a measurable impact on areas where there are lots of overlapping lines.


let lookAheadDistance: CLLocationDistance = 10
guard let pointAheadFeature = featurePolyline.sliced(from: closestCoordinate)!.coordinateFromStart(distance: lookAheadDistance) else { continue }
Expand Down