From d8899539a56ef26750340434c86e615364e86fba Mon Sep 17 00:00:00 2001 From: rikbellens Date: Mon, 22 Apr 2024 21:28:30 +0200 Subject: [PATCH] perf: addAll to empty SortedMap from FilteredMapView --- lib/src/sortedmap.dart | 8 ++++++-- lib/src/treeset.dart | 10 ++++++---- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/src/sortedmap.dart b/lib/src/sortedmap.dart index 5a5db24..36eac64 100644 --- a/lib/src/sortedmap.dart +++ b/lib/src/sortedmap.dart @@ -210,8 +210,12 @@ class _SortedMap extends MapBase return; } if (this is! FilteredMap) { - _sortedEntries - .addAll(other.entries.map((e) => ordering.mapEntry(e.key, e.value))); + var entries = other.entries; + if (entries is! Iterable<_MapEntryWithIndex>) { + entries = entries.map<_MapEntryWithIndex>( + (e) => ordering.mapEntry(e.key, e.value)); + } + _sortedEntries.addAll(entries); _map.addAll(other); return; } diff --git a/lib/src/treeset.dart b/lib/src/treeset.dart index 5aa44ba..fc0e2a9 100644 --- a/lib/src/treeset.dart +++ b/lib/src/treeset.dart @@ -393,10 +393,12 @@ class AvlTreeSet extends _BaseTreeSet { identical((items as dynamic).comparator, comparator)) { _root = items._root; return _root != null; - } - if (_root == null) { - // - var l = items.toList()..sort(comparator); + } else if (_root == null) { + var l = items.toList(); + if (items is! TreeSet || + !identical((items as dynamic).comparator, comparator)) { + l.sort(comparator); + } _root = AvlNode.fromOrderedList(l); return true; }