Skip to content

const BuiltList with Expando to cache hashCode #283

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/src/iterable/built_iterable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ abstract class BuiltIterable<E> implements Iterable<E> {

/// Converts to a [BuiltSet].
BuiltSet<E> toBuiltSet();

const BuiltIterable();
}
12 changes: 7 additions & 5 deletions lib/src/list/built_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

part of '../list.dart';

final _hashCodeExpando = Expando<int>();

/// The Built Collection [List].
///
/// It implements [Iterable] and the non-mutating part of the [List] interface.
Expand All @@ -15,7 +17,6 @@ part of '../list.dart';
/// for the general properties of Built Collections.
abstract class BuiltList<E> implements Iterable<E>, BuiltIterable<E> {
final List<E> _list;
int? _hashCode;

/// Instantiates with elements from an [Iterable].
factory BuiltList([Iterable iterable = const []]) {
Expand All @@ -26,6 +27,8 @@ abstract class BuiltList<E> implements Iterable<E>, BuiltIterable<E> {
}
}

const factory BuiltList.fromList([List<E> list]) = _BuiltList<E>.withSafeList;

/// Instantiates with elements from an [Iterable<E>].
///
/// `E` must not be `dynamic`.
Expand Down Expand Up @@ -62,8 +65,7 @@ abstract class BuiltList<E> implements Iterable<E>, BuiltIterable<E> {
/// the same order. Then, the `hashCode` is guaranteed to be the same.
@override
int get hashCode {
_hashCode ??= hashObjects(_list);
return _hashCode!;
return _hashCodeExpando[this] ??= hashObjects(_list);
}

/// Deep equality.
Expand Down Expand Up @@ -234,12 +236,12 @@ abstract class BuiltList<E> implements Iterable<E>, BuiltIterable<E> {

// Internal.

BuiltList._(this._list);
const BuiltList._(this._list);
}

/// Default implementation of the public [BuiltList] interface.
class _BuiltList<E> extends BuiltList<E> {
_BuiltList.withSafeList(List<E> list) : super._(list);
const _BuiltList.withSafeList([List<E> list = const []]) : super._(list);

_BuiltList.from([Iterable iterable = const []])
: super._(List<E>.from(iterable, growable: false)) {
Expand Down
6 changes: 3 additions & 3 deletions lib/src/list/list_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ part of '../list.dart';
/// for the general properties of Built Collections.
class ListBuilder<E> {
late List<E> _list;
_BuiltList<E>? _listOwner;
BuiltList<E>? _listOwner;

/// Instantiates with elements from an [Iterable].
factory ListBuilder([Iterable iterable = const []]) {
Expand All @@ -38,7 +38,7 @@ class ListBuilder<E> {

/// Replaces all elements with elements from an [Iterable].
void replace(Iterable iterable) {
if (iterable is _BuiltList<E>) {
if (iterable is BuiltList<E>) {
_setOwner(iterable);
} else {
_setSafeList(List<E>.from(iterable));
Expand Down Expand Up @@ -258,7 +258,7 @@ class ListBuilder<E> {

ListBuilder._uninitialized();

void _setOwner(_BuiltList<E> listOwner) {
void _setOwner(BuiltList<E> listOwner) {
_list = listOwner._list;
_listOwner = listOwner;
}
Expand Down