Skip to content
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

Provide faster implementation for whereNotNull #274

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mraleph
Copy link
Member

@mraleph mraleph commented Mar 21, 2023

Replace sync* with handwritten implementation for now. We can undo the change if sync* becomes as fast as handwritten code.

Closes #273

@mraleph mraleph requested a review from lrhn March 21, 2023 22:28
@devoncarew
Copy link
Member

devoncarew commented May 8, 2023

Ping @lrhn for a review.

@mraleph - can you add a changelog entry for this change (and rev the pubspec version if necessary)?

@override
bool moveNext() {
while (_source.moveNext()) {
final el = _source.current;
Copy link
Member

Choose a reason for hiding this comment

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

Use var, not final.

We don't generally use final for locals.

Copy link
Member

Choose a reason for hiding this comment

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

final locals is getting enforcement internally and is very likely the direction we'll go with Dash team packages as well.

Copy link
Member

Choose a reason for hiding this comment

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

Still, keep the style here consistent until we do make a change. Don't mix styles.

}

@override
T get current => _current!;
Copy link
Member

Choose a reason for hiding this comment

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

Use => _current ?? (throw StateError("no element")).

@@ -593,6 +594,42 @@ extension IterableExtension<T> on Iterable<T> {
}
}

/// An iterable which emits all non-`null` elements of the source
/// iterable.
class _NotNullIterable<T extends Object> extends IterableBase<T> {
Copy link
Member

Choose a reason for hiding this comment

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

extends Iterable<T>.

if (element != null) yield element;
}
}
Iterable<T> whereNotNull() => _NotNullIterable(this);
Copy link
Member

@lrhn lrhn Mar 22, 2023

Choose a reason for hiding this comment

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

Should we do

  var self = this;
  if (self is Iterable<T>) return self;
  return _NotNullIterable<T>(this);
}

Probably not worth it, but we don't have a good way to prevent you from using the method on Iterable<int>.
(We could put a Never whereNotNull() => throw "Not needed!"; as extension <T extends Object> on Iterable<T>, then you'd see if you get that instead.)

Copy link
Contributor

@gmpassos gmpassos May 19, 2023

Choose a reason for hiding this comment

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

This is a very important check, since it transform a new Iterable instance (_NotNullIterable) to just a casting.

Since the extension is resolved in the variable level, not in the instance:

void main() {
  
  List<int?> l1 = <int>[1,2];
  List<int> l2 = <int>[3,4];
  List<int?> l3 = <int?>[1,2];
  
  print(l1.isNullable);
  print(l2.isNullable);
  print(l3.isNullable);
  
}

extension _ItrExt<T extends Object> on Iterable<T> {
  String get isNullable => "NO<T>";
}

extension _ItrNullableExt<T extends Object> on Iterable<T?> {
  String get isNullable => this is Iterable<T> ? "NO<T?>" : "YES<T?>";
}

/////////////////
// OUTPUT:
/////////////////
// NO<T?>
// NO<T>
// YES<T?>

@lrhn
Copy link
Member

lrhn commented May 23, 2023

We now have .nonNulls getter in the platform libraries which does the same thing.

We should optimize that if necessary, and make collection just forward to the platform library version when we upgrade to requiring SDK 3.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

rewrite whereNotNull without sync*
5 participants