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

[flutter_tools] fix performance of tree-shake-icons #55417

Merged

Conversation

jonahwilliams
Copy link
Member

@jonahwilliams jonahwilliams commented Apr 22, 2020

Description

Fixes the performance issue with tree-shake-icons and filters to ttf mime type. Does not change default or error behavior.

Fixes #53240
Fixes #55370

@fluttergithubbot fluttergithubbot added team Infra upgrades, team productivity, code health, technical debt. See also team: labels. tool Affects the "flutter" command-line tool. See also t: labels. labels Apr 22, 2020
@jonahwilliams
Copy link
Member Author

@dnfield could we possibly loosen some of the error cases to make this non-breaking? Specifically, we could allow (with a warning) the case where the kernel still contains references to icons that don't exist as assets

@jonahwilliams jonahwilliams changed the title [flutter_tools] fix performance of tree-shake-icons without adjusting default [flutter_tools] fix performance of tree-shake-icons and allow missing fonts Apr 23, 2020
'in the assets section of your pubspec.yaml, are missing '
'the package that would include them, or are missing '
'"uses-material-design: true".');
_logger.printError(
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if this breaks any invariants. It might also need to be a trace event

@dnfield
Copy link
Contributor

dnfield commented Apr 23, 2020

If a kernel has references to a font that isn't present, that's a bug. The fact that we're failing earlier should not be considered a breaking change IMO - if you try to use an icon from a non-existant font, you'll get undefined behavior at runtime. You'll end up with either an arbitrary character from a fallback font, or (more likely), a replacement character or some garbage in your application.

@jonahwilliams
Copy link
Member Author

For now we'll need to disable for web then.

On the other side, I disagree that it is a bug. There are plenty of code patterns that would lead to icons being retained but not actually used. For example, a pattern like the following will not tree shake:

Widget build(BuildContext context) {
  switch (data) {
    case 'material':
       return WidgetWithMaterialIcon();
     case 'cupertino':
       return WidgetWithCupertinoIcon();
  }
}

If data comes from an API endpoint, the compiler will never be able to remove the cupertino path. Despite that, the author may know with certainty that their endpoint will always return material. In fact, this code could be present in a third party dependency of that authors code in such a way that is not obvious.

Is tree-shake-icons a way to reduce bundle size, or an icon usage linter? Its going to be very difficult to roll out both of these at once. If you really want the later behavior I would strongly consider staging that as a warning first and doing a gradual breaking change.

@jonahwilliams
Copy link
Member Author

And I'm also concerned that we'd be really depending on the specific implementation of tree shaking, despite the fact that the Dart team has provided no formal guidance or guarantees on how it applies: dart-lang/sdk#33920

@dnfield
Copy link
Contributor

dnfield commented Apr 23, 2020

Someone who wants to use that pattern cannot tree shake thier icons. They would have to opt out. Perhaps we could make this just print a warning and opt them out, and the warning could be suppressed if they explicitly opted out.

As far as implementation details go, I'm not sure what else to do there. The only concern would be if they changed the way apt/tfa const evaluation worked. That seems pretty safe to me.

@jonahwilliams
Copy link
Member Author

How would a user know that they have un-treeshakeable code if there is no guidance or documentation on how tree shaking works?

@dnfield
Copy link
Contributor

dnfield commented Apr 23, 2020

I think we should just let them know that their icon fonts cannot be shaken given the state of their code. We could create a wiki or doc page explaining that for Icons to be shakeable, they have to be const, and that any reachable code has to include the referenced font.

@jonahwilliams
Copy link
Member Author

FYI @zanderso

@jonahwilliams jonahwilliams changed the title [flutter_tools] fix performance of tree-shake-icons and allow missing fonts [flutter_tools] fix performance of tree-shake-icons Apr 25, 2020
@jonahwilliams jonahwilliams removed the team Infra upgrades, team productivity, code health, technical debt. See also team: labels. label Apr 25, 2020
@jonahwilliams
Copy link
Member Author

Updated to not adjust the error behavior for now

Copy link
Member

@zanderso zanderso left a comment

Choose a reason for hiding this comment

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

lgtm

So, tree shaking fonts is still off by default, and when it is turned on by default, it will still be off for Web, correct? Is there an issue tracking what the story is for web?

@jonahwilliams
Copy link
Member Author

Yes, its still off by default. Whether or not we can turn it on for web depends on the outcome of a discussion involving @dnfield when he gets back

@jonahwilliams jonahwilliams merged commit c55b322 into flutter:master Apr 29, 2020
@jonahwilliams jonahwilliams deleted the flip_tree_shake_icon_default branch April 29, 2020 18:43
@liyuqian liyuqian added the c: performance Relates to speed or footprint issues (see "perf:" labels) label May 13, 2020
@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
c: performance Relates to speed or footprint issues (see "perf:" labels) tool Affects the "flutter" command-line tool. See also t: labels.
Projects
None yet
6 participants