From 8010a537616ebc963f2e1ea3b4a60f8c0f2e05f3 Mon Sep 17 00:00:00 2001 From: Jonah Williams Date: Tue, 15 Mar 2022 14:25:22 -0700 Subject: [PATCH] Revert "Do not eagerly allocate inherited widget caches when initializing element tree" (#100152) --- .../flutter/lib/src/widgets/framework.dart | 66 +++------------- .../widgets/inherited_tree_cache_test.dart | 79 ------------------- 2 files changed, 11 insertions(+), 134 deletions(-) delete mode 100644 packages/flutter/test/widgets/inherited_tree_cache_test.dart diff --git a/packages/flutter/lib/src/widgets/framework.dart b/packages/flutter/lib/src/widgets/framework.dart index bc9f973d64..e716c5803e 100644 --- a/packages/flutter/lib/src/widgets/framework.dart +++ b/packages/flutter/lib/src/widgets/framework.dart @@ -394,53 +394,6 @@ abstract class Widget extends DiagnosticableTree { } } -/// A cache of inherited elements with an optional parent cache. -/// -/// When looking up an inherited element, this will look through parent -/// caches until the element is found or the end is reached. When an element -/// is found, it is cached at the first element where the search began. -/// -/// The intention of this cache is to speed up the initial build of widget -/// trees that contain a significant number of inherited widgets by deferring -/// expensive map allocations until they are needed, and by only allocating -/// in the "closest" hash map. -/// -/// This will not cache `null` results if an inherited element is not found. -@visibleForTesting -class InheritedTreeCache { - /// Create a new [InheritedTreeCache] with an optional parent. - InheritedTreeCache([this._parent]); - - final InheritedTreeCache? _parent; - final HashMap _current = HashMap(); - - /// Place the [element] in the cache under [type]. - void operator []=(Type type, InheritedElement element) { - _current[type] = element; - } - - /// Find the nearest [InheritedElement] of type [type], or `null` if it - /// cannot be found. - /// - /// This operation will also cache the inherited element to improve the - /// speed of future lookups. - InheritedElement? operator[](Type type) { - InheritedElement? potential = _current[type]; - if (potential != null) { - return potential; - } - potential = _parent?._lookupWithoutCaching(type); - if (potential != null) { - _current[type] = potential; - } - return potential; - } - - InheritedElement? _lookupWithoutCaching(Type type) { - return _current[type] ?? _parent?._lookupWithoutCaching(type); - } -} - /// A widget that does not require mutable state. /// /// A stateless widget is a widget that describes part of the user interface by @@ -4030,7 +3983,7 @@ abstract class Element extends DiagnosticableTree implements BuildContext { // implementation to decide whether to rebuild based on whether we had // dependencies here. } - _inheritedLookup = null; + _inheritedWidgets = null; _lifecycleState = _ElementLifecycle.inactive; } @@ -4222,8 +4175,7 @@ abstract class Element extends DiagnosticableTree implements BuildContext { return null; } - InheritedTreeCache? _inheritedLookup; - + Map? _inheritedWidgets; Set? _dependencies; bool _hadUnsatisfiedDependencies = false; @@ -4260,7 +4212,7 @@ abstract class Element extends DiagnosticableTree implements BuildContext { @override T? dependOnInheritedWidgetOfExactType({Object? aspect}) { assert(_debugCheckStateIsActiveForAncestorLookup()); - final InheritedElement? ancestor = _inheritedLookup?[T]; + final InheritedElement? ancestor = _inheritedWidgets == null ? null : _inheritedWidgets![T]; if (ancestor != null) { return dependOnInheritedElement(ancestor, aspect: aspect) as T; } @@ -4271,7 +4223,7 @@ abstract class Element extends DiagnosticableTree implements BuildContext { @override InheritedElement? getElementForInheritedWidgetOfExactType() { assert(_debugCheckStateIsActiveForAncestorLookup()); - final InheritedElement? ancestor = _inheritedLookup?[T]; + final InheritedElement? ancestor = _inheritedWidgets == null ? null : _inheritedWidgets![T]; return ancestor; } @@ -4291,7 +4243,7 @@ abstract class Element extends DiagnosticableTree implements BuildContext { void _updateInheritance() { assert(_lifecycleState == _ElementLifecycle.active); - _inheritedLookup = _parent?._inheritedLookup; + _inheritedWidgets = _parent?._inheritedWidgets; } @override @@ -5292,8 +5244,12 @@ class InheritedElement extends ProxyElement { @override void _updateInheritance() { assert(_lifecycleState == _ElementLifecycle.active); - _inheritedLookup = InheritedTreeCache(_parent?._inheritedLookup) - ..[widget.runtimeType] = this; + final Map? incomingWidgets = _parent?._inheritedWidgets; + if (incomingWidgets != null) + _inheritedWidgets = HashMap.of(incomingWidgets); + else + _inheritedWidgets = HashMap(); + _inheritedWidgets![widget.runtimeType] = this; } @override diff --git a/packages/flutter/test/widgets/inherited_tree_cache_test.dart b/packages/flutter/test/widgets/inherited_tree_cache_test.dart deleted file mode 100644 index 693d8477c3..0000000000 --- a/packages/flutter/test/widgets/inherited_tree_cache_test.dart +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2014 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -import 'package:flutter/material.dart'; -import 'package:flutter/src/widgets/framework.dart'; -import 'package:flutter_test/flutter_test.dart'; - -void main() { - testWidgets('InheritedTreeCache returns null if element is not found', (WidgetTester tester) async { - final InheritedTreeCache parent = InheritedTreeCache(); - - expect(parent[InheritedElementA], isNull); - }); - - testWidgets('InheritedTreeCache can look up element from parent', (WidgetTester tester) async { - final InheritedTreeCache parent = InheritedTreeCache(); - final InheritedTreeCache child = InheritedTreeCache(parent); - final InheritedElementA elementA = InheritedElementA(const InheritedWidgetA()); - - parent[InheritedElementA] = elementA; - - expect(child[InheritedElementA], elementA); - }); - - testWidgets('InheritedTreeCache can look up multiple elements from parent', (WidgetTester tester) async { - final InheritedTreeCache parent = InheritedTreeCache(); - final InheritedTreeCache child = InheritedTreeCache(parent); - final InheritedElementA elementA = InheritedElementA(const InheritedWidgetA()); - final InheritedElementA elementB = InheritedElementA(const InheritedWidgetB()); - - parent[InheritedElementA] = elementA; - parent[InheritedElementB] = elementB; - - expect(child[InheritedElementA], elementA); - expect(child[InheritedElementB], elementB); - }); - - testWidgets('InheritedTreeCache does not cache nulls', (WidgetTester tester) async { - final InheritedTreeCache parent = InheritedTreeCache(); - final InheritedTreeCache child = InheritedTreeCache(parent); - final InheritedElementA elementA = InheritedElementA(const InheritedWidgetA()); - - // First look up element that has not been cached. - expect(child[InheritedElementA], null); - - // Then manually add element to parent. - parent[InheritedElementA] = elementA; - - // Then the child should be able to find it. - expect(child[InheritedElementA], elementA); - }); -} - -class InheritedElementA extends InheritedElement { - InheritedElementA(InheritedWidget widget) : super(widget); -} - -class InheritedWidgetA extends InheritedWidget { - const InheritedWidgetA({ Key? key }) : super(child: const SizedBox(), key: key); - - @override - bool updateShouldNotify(covariant InheritedWidget oldWidget) { - return true; - } -} - -class InheritedElementB extends InheritedElement { - InheritedElementB(InheritedWidget widget) : super(widget); -} - -class InheritedWidgetB extends InheritedWidget { - const InheritedWidgetB({ Key? key }) : super(child: const SizedBox(), key: key); - - @override - bool updateShouldNotify(covariant InheritedWidget oldWidget) { - return true; - } -}