Skip to content

feat: provide and use lightweight tokens for optimized treeshaking#19736

Merged
jelbourn merged 24 commits intomasterfrom
lightweight-token-optimizations
Jun 29, 2020
Merged

feat: provide and use lightweight tokens for optimized treeshaking#19736
jelbourn merged 24 commits intomasterfrom
lightweight-token-optimizations

Conversation

@devversion
Copy link
Member

@devversion devversion commented Jun 23, 2020

Merges the lightweight-token-optimizations feature branch into master.

Provides and uses lightweight tokens for optimized treeshaking in components that were causing
unnecessary class retention (resulting in bundle size increases; especially with Ivy).

More details in #19576

The Angular Material `MatRadioButton` component currently
optionally injects the `MatRadioGroup`. This causes the
`MatRadioGroup` implementation to be always retained in
application bundles.

This is problematic as the use of radio groups is not
always required. i.e. it's valid to use a standalone
radio button. In those cases, we do not want to retain
the radio group implementation unnecessarily.

This has always been an issue, but the issue became
more significant in Ivy where component factory and definitions
are attached directly to the `MatRadioGroup` class (resulting
in a more significant size increase).

We fix this by using a lightweight token for injecting the parent
radio group optionally. This solves the retention issue if a standalone
radio button is used.

Related to: #19576.
Angular Material supports lazy content for menus. It does this
by querying for the `MatMenuContent` directive in the `MatMenu`
component. This is problematic though as it always causes
`MatMenuContent` to be retained as its used as actual query predicate
token.

To solve this, we use a lightweight injection token for querying
the lazy content (if present). This optimizes applications using
the Angular Material menu without lazy content.

Related to: #19576.
The date range picker could be used outside the `MatFormField`
component in practice. Right now though, the date-picker input
always has a hard-reference on the `MatFormField` component, causing
it to be retained. This would be the same in View Engine, but with
Ivy this signifies a larger size issue as factories/definitions are
directly attached to the component.

To allow for better tree-shaking / optimizations, we use the form-field
light-weight injection token, so that the date range picker can be used
outside of a form-field with minimal cost.

Related to: #19576.
The `MatButtonToggleGroup` component is not always used in applications. e.g.
it's an optional part, and standalone button toggles are totally valid.

Currently though, we always reference the `MatButtonToggleGroup` component
directly as a value, and it will be retained in applications regardless
of the usage.

We can improve this by providing a light-weight injection token for
the button-toggle group that we can then use DI injection to avoid
full retention of the class.

This obviously was already an issue in View Engine too, but it became
significantly worse in Ivy as factories are now directly attached to
the component class.

Related to: #19576.
The `MatOptgroup` component is not always used in applications. e.g.
it's an optional part of the autocomplete or select. Currently though,
we always reference the `MatOpgroup` component directly as a value,
and it will be retained in applications regardless of the usage.

We can improve this by providing a light-weight injection token for
the optgroup that we can then use in queries or optional DI injection.

This obviously was already an issue in View Engine too, but it became
significantly worse in Ivy as factories are now directly attached to
the component class.

Related to: #19576.
Currently the `CdkDropList` directive always retains the
`CdkDropListGroup` directive. This is not desired as the
drop list group is not necessarily always used in combination
with a drop list.

Additionally, the `CdkDrag` directive always retains the
`CdkDragHandle`, `CdkDragPlaceholder` and `CdkDragPreview`
directives. This is neither desired because a handle, placeholder
or preview can be consumed optionally. We should only bring those
directives into applications when actually needed.

Size measurements show that this saves around ~1.3kb if only
a drop list and `cdkDrag` is used. The downside is that we add
up ~200b if _all_ directives of the drag-drop module are used.

Given the drag-drop has a size of around 45kb, the 1.3kb size
improvement in the common case should be good (~3% reduction).

Related to #19576.
The `CdkAccordionItem` currently always brings in the `CdkAccordion`
class. This is not desired as an accordion item could be used
outside of a `<cdk-accordion>`. In those cases, we do not want
to retain the accordion class w/ metadata unnecessarily.

This also affects the `MatExpansionPanel` as it extends
from the `CdkAccordionItem` and therefore always brings in
the `CdkAccordion` list unnecessarily right now.

Related to #19576.
The size tests automatically will include a bootstrap module
call. The boostrap calls in those test files are noop and
will be removed by rollup as build optimizer considers it as
a side-effect free call that is unused.
Sorts the integration size-test golden lexicographically. This
should make the golden more readable.
…icon (#19693)

* build: add size test for basic chip-list with chip

* refactor: use lightweight token for chip avatar, remove and trailing icon

Currently `MatChip` always causes `MatChipAvatar`, `MatChipRemove` and
`MatChipTrailingIcon` to be retained. These are optional parts of a
chip and shouldn't be retained always. The retention of these classes
wasn't noticeable in View Engine as no factories were generated for
directives. With Ivy though, factories and definitions are generated
for these directives and a size increase is noticeable.

We should use lightweight tokens for querying of those directives.
That will improve tree-shaking for the case where not all of these
parts are used. If all parts are used, the app will slightly increase
due to the provided injection tokens, but that is of average 150b, while
the savings are of ~800b in the common case.

Related to: #19576
Currently the `MatFormField` always retains `MatHint`, `MatError`,
`MatPrefix` and `MatSuffix`. These are optional parts of a form-field so
we ideally would not retain those always.

We can achieve this by using lightweight injection tokens for querying
of these directives, instead of using the actual implementations as
query predicate token. The retention was already an issue in View Engine
too, but the issue became more noticable in Ivy where factories and
definitions are directly attached to the classes.
The Angular Material tab group currently always retains `MatTabContent`
and `MatTabLabel`. These are optional features of the tabs module and
should only be retained when needed.

To achieve this, we use lightweight tokens instead of the actual
implementations when querying for a lazy content or label.

This signifies an improvement of ~500b in the common case, and
an increase (due to the lightweight tokens being provided) in
the advanced case of ~140b.

Related to #19576.
The select size test has slightly shifted already due to the recent
Angular update. We are updating the golden here so that we can see
the lightweight token effect in the followed commit for select.
The Angular Material always retains the `MatSelectTrigger`
class. This is not ideal as this is an optional part
of the select. The class itself is not large enough
to contribute to any noticeable size impact, even with
Ivy where factories/definitions are directly attached, but
for the sake of completeness, and because custom triggers
are assumed to be uncommon, a lightweight token is used for
a small micro-optimization in the common case.

Related to #19576.
@devversion devversion requested a review from a team June 23, 2020 20:35
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jun 23, 2020
@devversion devversion added P2 The issue is important to a large percentage of users, with a workaround target: patch This PR is targeted for the next patch release labels Jun 23, 2020
@jelbourn jelbourn added target: minor This PR is targeted for the next minor release merge: preserve commits When the PR is merged, a rebase and merge should be performed and removed target: patch This PR is targeted for the next patch release labels Jun 23, 2020
Copy link
Contributor

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

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

LGTM

@jelbourn jelbourn added lgtm action: merge The PR is ready for merge by the caretaker labels Jun 23, 2020
@jelbourn jelbourn merged commit 756ec58 into master Jun 29, 2020
@devversion devversion deleted the lightweight-token-optimizations branch July 15, 2020 19:56
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Aug 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement merge: preserve commits When the PR is merged, a rebase and merge should be performed P2 The issue is important to a large percentage of users, with a workaround target: minor This PR is targeted for the next minor release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Use light-weight injection pattern for optimized tree-shaking/bundle size

4 participants