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

Updated MGLScaleBar to use rendered UIImages instead of MGLScaleBarLabel #11921

Merged
merged 3 commits into from
May 24, 2018

Conversation

julianrex
Copy link
Contributor

@julianrex julianrex commented May 16, 2018

  • Added a new "random world tour" to iOS for testing/profiling purposes
  • Render MGLScaleBarLabel on demand caching the resulting UIImages for use as scale bar labels

Edit: No changelog entry yet

Edit 2: Updated with screenshots of Instruments running the randomWorldTour example on an iPhone X, specifically looking at CPU and memory usage (not FPS).

CPU / MGLScaleBarLabel

Before: In the following image, I'm filtering symbols for drawTextInRect which was only used by MGLScaleBarLabel. You can see, in this test, taking up a total of 14% of the CPU - significant for such a small on-screen item. You can also see that allocations take up a good chunk of this too (and if we filter on malloc - that actually goes up further).

screen shot 2018-05-15 at 09 48 49

After: Filtering on drawTextInRect and then MGLScaleBar, the effect is essentially non-existent:

screen shot 2018-05-15 at 11 45 51

screen shot 2018-05-15 at 11 45 59

Memory impact

Before: The following includes transient allocations i.e. allocations that have come and gone. The highlighted line, 33%, will include will include all allocations from CA::Transaction::commit(), but notice on the right that the "Heaviest Stack Trace" is our MGLScaleBarLabel.

screen shot 2018-05-15 at 10 35 49

After: Hunting for the same CA::Transaction::commit() as above:
screen shot 2018-05-15 at 11 50 19

Transient allocations

Before: Looking at all allocations, sorted by transient, we see:

screen shot 2018-05-15 at 10 25 31

After: In these traces, the comparisons aren't exact, but it's worth noting how the top 3 small allocations compare with the next largest chunk, mbgl::VectorTileFeature - this reduced much more than I was expecting (and why I included these traces).

screen shot 2018-05-15 at 11 54 25

@julianrex julianrex added iOS Mapbox Maps SDK for iOS performance Speed, stability, CPU usage, memory usage, or power usage labels May 16, 2018
@julianrex
Copy link
Contributor Author

Rebasing after release-boba merge...

@julianrex julianrex self-assigned this May 18, 2018
Copy link
Contributor

@fabian-guerra fabian-guerra left a comment

Choose a reason for hiding this comment

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

Thank you for improving the performance. I made some comments. Please add the screenshots for the pre/after performance tests. Also add the blurb in the changelog and reference this PR.


// Now create a world coord
CLLocationDegrees heading = drand48()*360.0;
CLLocationDistance dist = drand48()*radius;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: distance?

for (NSInteger i = 0; i<numAnnotations; i++) {

CLLocationDegrees heading = drand48()*360.0;
CLLocationDistance dist = drand48()*radius;
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: distance?

[self _randomWorldTourInternal];
}

- (void)_randomWorldTourInternal {
Copy link
Contributor

Choose a reason for hiding this comment

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

There is no need for an underscored private method.

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've noticed that we've used _ as a prefix for private methods in the SDK. Do we have a rule of thumb?

Copy link
Contributor

Choose a reason for hiding this comment

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

We have some but mostly on SDK classes. This is part of the demo app. I'm OK if you want to leave it as it is.


[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(currentLocaleDidChange:)
name:NSCurrentLocaleDidChangeNotification
Copy link
Contributor

Choose a reason for hiding this comment

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

I may be wrong but I think this notification is not triggered on iOS since changing the locale language requires an app restart.

Copy link
Contributor

Choose a reason for hiding this comment

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

@fabian-guerra is correct — NSCurrentLocaleDidChangeNotification isn’t relevant on iOS, as the system does a soft-reboot whenever the language/locale/region is changed and apps reload all of their views.

(This notification is sent on macOS, but this code doesn’t support that platform.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will remove.

}

- (void)currentLocaleDidChange:(NSNotification*)notification {
// Clear the cache, so labels should be rebuilt.
[self.labelImageCache removeAllObjects];
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be necessary freeing the cache when the app triggers a memory warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IIRC - no, since the code uses NSCache rather than a dictionary. It has behaviour to evict objects in low memory situations (though seems unpredictable).

Copy link
Contributor

Choose a reason for hiding this comment

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

Right and it may not free anything if it receives a memory warning. If you consider is not necessary then I'm OK.

Copy link
Contributor

@fabian-guerra fabian-guerra left a comment

Choose a reason for hiding this comment

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

👍🏼

@julianrex julianrex merged commit 9e4a16d into master May 24, 2018
@julianrex julianrex deleted the jrex-scalebar branch May 24, 2018 14:30
@julianrex julianrex added this to the ios-v4.1.0 milestone Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
iOS Mapbox Maps SDK for iOS performance Speed, stability, CPU usage, memory usage, or power usage
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants