Skip to content

Conversation

@dkwingsmt
Copy link
Contributor

@dkwingsmt dkwingsmt commented Jan 22, 2020

Description

Since Element.dependencies is a HashSet, its order is not guaranteed, which can cause problem if someone relies on its exact string value. This is actually observed in #48453, where stepper_test.dart compares the exact value of an error that includes the string of Stepper, and the order isn't even consistent between desktop and web.

This PR fixes this issue by sorting dependencies by the widget name. I assume this should be sufficient in most cases, since the most common way of adding a dependency is dependOnInheritedWidgetOfExactType, in which case there should be no conflicted widget types.

Related Issues

Tests

I added the following tests:

  • Element diagnostics with multiple dependencies

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]). This will ensure a smooth and quick review process.

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I signed the CLA.
  • I read and followed the Flutter Style Guide, including Features we expect every widget to implement.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I updated/added relevant documentation (doc comments with ///).
  • All existing and new tests are passing.
  • The analyzer (flutter analyze --flutter-repo) does not report any problems on my PR.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

Did any tests fail when you ran them? Please read Handling breaking changes.

  • No, no existing tests failed, so this is not a breaking change.

@fluttergithubbot fluttergithubbot added the framework flutter/packages/flutter repository. See also f: labels. label Jan 22, 2020
final List<InheritedElement> sortedDependencies = _dependencies
.toList()
..sort((InheritedElement a, InheritedElement b) =>
a.widget.runtimeType.toString().compareTo(b.widget.runtimeType.toString()));
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 a helper method that @dnfield wrote (I think?) to avoid accessing widget.runtimeType.toString directly, which is slow.

Copy link
Contributor

Choose a reason for hiding this comment

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

it's in foundation - but I don't think it would help here, because you're actually trying to sort the string results here...

In theory this is only called from debug code.

I strongly suspect that if the tests care about order, this isn't helping anything. And if they don't care about order, we should probably just make them tolerate out of order items rather than putting this in the framework though.

Unless we just stop using a HashSet where we say we're using a Set :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the test that cares about order:

'dependencies: [TickerMode,\n'

The stringified Stepper widget displays its dependencies.
According to the test I added, it should help.

Copy link
Contributor

@dnfield dnfield left a comment

Choose a reason for hiding this comment

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

Why are we using a HashSet instead of the default LinkedHashSet, which would preserve order?

If we are using a HashSet, why are we advertising it as a Set, which by the spec says its ordered?

@jonahwilliams
Copy link
Contributor

A lot of time was spent optimizing Inherited elements. HashSets/Maps are notably faster than the default options.

@dnfield
Copy link
Contributor

dnfield commented Jan 22, 2020

Introduced in 5494323 - @Hixie?

@dnfield
Copy link
Contributor

dnfield commented Jan 22, 2020

@jonahwilliams - do we have a benchmark tracking that?

@jonahwilliams
Copy link
Contributor

Almost every benchmark will be inadvertently affected by InheritedElement lookup

@dnfield
Copy link
Contributor

dnfield commented Jan 22, 2020

For this specific instance, we only call contains in an assert - otherwise we add or iterate.

I'm not convinced that this specific one would be detrimental to performance to change to a regular Set. We could do that without changing all of them.

@dkwingsmt
Copy link
Contributor Author

dkwingsmt commented Jan 23, 2020

@dnfield If we only call contains in assert as you said, should it be even better a List?

@dnfield
Copy link
Contributor

dnfield commented Jan 23, 2020

A list would allow duplicates.

@dkwingsmt
Copy link
Contributor Author

dkwingsmt commented Jan 23, 2020

Still, changing debugFillProperties adds literally 0 overhead to release mode performance, while changing HashSet to LinkedHashSet adds something.

@Hixie
Copy link
Contributor

Hixie commented Jan 23, 2020

This would change an O(1) set of algorithms into O(N log N). The solution is to make the tests that depend on the order more resilient.

@dkwingsmt dkwingsmt closed this Jan 23, 2020
@dkwingsmt dkwingsmt deleted the sort-debug-dependencies branch January 23, 2020 21:24
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants