Skip to content

[flutter_tools] default tree-shake-icons to enabled and improve performance - #54923

Merged
jonahwilliams merged 14 commits into
flutter:masterfrom
jonahwilliams:flip_tree_shake_icon_default
Apr 22, 2020
Merged

jonahwilliams merged 14 commits into
flutter:masterfrom
jonahwilliams:flip_tree_shake_icon_default

Conversation

@jonahwilliams

@jonahwilliams jonahwilliams commented Apr 16, 2020 •

Copy link
Copy Markdown
Contributor

Description

Enable tree-shake-icons by default in profile/release mode. Fix performance by ensuring that only a single instance of const finder runs, and only on ttf fonts

Fixes #55370

@fluttergithubbot fluttergithubbot added tool Affects the "flutter" command-line tool. See also t: labels. work in progress; do not review labels Apr 16, 2020
@jonahwilliams
jonahwilliams marked this pull request as ready for review April 16, 2020 21:44

@dnfield dnfield 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.

LGTM

@dnfield

dnfield commented Apr 17, 2020

Copy link
Copy Markdown
Contributor

(once tests pass)

@jonahwilliams

Copy link
Copy Markdown
Contributor Author

So the deploy gallery shard has failed twice in a row with OOM. I'm going to mark this a WIP, I need to investigate whether we're using drastically more memory with this on by default.

@jonahwilliams

Copy link
Copy Markdown
Contributor Author

Running this locally I'm able to repro a large spike in memory pressure and my computer also locked up for a few seconds while running this (it doesn't for normal builds).

@dnfield

dnfield commented Apr 17, 2020

Copy link
Copy Markdown
Contributor

Which command? Const_finder?

@jonahwilliams

Copy link
Copy Markdown
Contributor Author

just by running flutter build apk --tree-shake-icons

@Zazo032

Zazo032 commented Apr 18, 2020

Copy link
Copy Markdown
Contributor

I also experience this when using this flag. For a few seconds, my machine becomes unresponsive with 100% CPU usage and a spike in used memory. Both for APK and appbundle (didn't test iOS or web).

// Only tree shake MaterialIcons-Regular.ttf font.
final bool isMaterialIcons = globals.fs.path
.basename(content.file.path) == kMaterialIconsFontName;
if (!isMaterialIcons || !await iconTreeShaker.subsetFont(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@dnfield fyi

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.

But... Why? What about Cupertino icons or font awesome icins?

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.

The intention of this routine was to only do work if the font file was used by an IconData instance. Is that not happening?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

As written this was spinning up a const finder instance for every single asset. Does this actually work with arbitrary icon sets? If so we could use something like the mime package to limit it to font files (ttf only?)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The other change this would need is to limit the number of instances to 2 or 3, because each one seems to be quite heavy

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.

Ahh ok. Yes, this should work with any font that you refernece a glyph in using IconData .. or at least any TTF.

Limiting this to 2 or 3 instances should be fine. And it wasn't meant to run on non-ttf assets.

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.

The expectation is that this code

would return quickly, after the initial run of the const finder here:
_iconData ??= await _getIconData(_environment);

Why is the const finder running more than once? :\

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I figured it out, getIconData is fairly expensive, and we might be copying multiple assets at the same time. each one that hits this line before the first instance has finished will spawn a new const finder

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.

Ahh, yes. Ok. We should just trigger that to happen before the loop. I'm still a little confused about how multiple places are reaching this code before the first one finishes -are we missing an await somewhere?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because the out loop is a Future.wait, not a for loop

if (input.lengthSync() < 12) {
return false;
}
final String mimeType = mime.lookupMimeType(

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.

How confident are you that this won't cause new problems?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not confident about anything

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.

Maybe we can just move the mime chekc to a separate pr

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We might as well resolve this now if you have concerns :)

Are there non-ttf fonts that the icon_tree_shaker works with? Or does the hb subset utility have a list of supported formats?

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.

I guess this should be fairly safe. Mime sniffing can get funny, and we could get broken if the mime package changes the spelling of a type

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'll add some more tests around this logic

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

More tests added. If the spelling changes there was an existing test that fails, but I also added cases to verify we don't subset invalid fonts or non-ttf mime types

@jonahwilliams

Copy link
Copy Markdown
Contributor Author

I enabled this for web too, but it doesn't actually work since --cfe-only does not do any const evaluation. Instead we'll need to add a dart2js target to the frontend server and produce kernel that way.

@jonahwilliams jonahwilliams changed the title [flutter_tools] default tree-shake-icons to enabled for non-web [flutter_tools] default tree-shake-icons to enabled Apr 20, 2020
@jonahwilliams jonahwilliams changed the title [flutter_tools] default tree-shake-icons to enabled [flutter_tools] default tree-shake-icons to enabled and improve performance Apr 22, 2020

@dnfield dnfield 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.

LGTM

@jonahwilliams

Copy link
Copy Markdown
Contributor Author

One test failure, with a really nice error message too :D

@jonahwilliams
jonahwilliams merged commit 0b93a92 into flutter:master Apr 22, 2020
@jonahwilliams
jonahwilliams deleted the flip_tree_shake_icon_default branch April 22, 2020 20:17
jonahwilliams pushed a commit that referenced this pull request Apr 22, 2020
jonahwilliams pushed a commit that referenced this pull request Apr 22, 2020
@jonahwilliams
jonahwilliams restored the flip_tree_shake_icon_default branch April 22, 2020 23:08
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jul 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

tool Affects the "flutter" command-line tool. See also t: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

--shrink --tree-shake-icons causes huge system load, causing CI Runners to fail

5 participants