-
Notifications
You must be signed in to change notification settings - Fork 12
Add typed queue classes #21
Conversation
These make it easy to, for example, efficiently enqueue incoming chunked binary data for processing.
I'm not familiar with this package. @eernstg I think you are in the process of migrating dart:typed_data. Would you also want to review this addition to package:typed_data with your API changes in mind? |
Ping! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks very good!
lib/src/typed_queue.dart
Outdated
int get length => (_tail - _head) & (_table.length - 1); | ||
|
||
List<E> toList({bool growable = true}) { | ||
var list = growable ? (<E>[]..length = length) : _createList(length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might as well start to prepare for NNBD. The ..length = length
won't work there, you can't grow a non-nullable list.
(Usual workaround is List<E>.filled(length, ..someDefaultValue..)
, where the default value here is probably either 0 or 0.0/NaN depending on E
).
Maybe have a E get _defaultValue;
which you can override to return the value to use.
Since we are in package:typed_data
, we have access to growable typed data, so consider returning a Uint8Queue
or Uint8Buiffer
instead of a plain growable list. Both are valid lists.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
var delta = value - length; | ||
if (delta >= 0) { | ||
if (_table.length <= value) _growTo(value); | ||
_tail = (_tail + delta) & (_table.length - 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also fill the range with zeros (at least when you don't grow).
Since removeLast
doesn't clear the entry it removed, the data is still in the backing store. Increasing the length will make it accessible again, which seems like something that users could end up depending on, even if it's not actually promised. Better to zero out the range before making it available.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
lib/src/typed_queue.dart
Outdated
} | ||
|
||
E operator [](int index) { | ||
RangeError.checkValidIndex(index, this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use RangeError.checkValidIndex(index, this, null, this.length);
.
If you don't pass the length
, it is found through a dynamic lookup on this
. That's probably not good for performance any more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. That's probably a good note to add to the docs.
lib/src/typed_queue.dart
Outdated
|
||
L sublist(int start, [int end]) { | ||
var length = this.length; | ||
RangeError.checkValidRange(start, end, length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
end = RangeError.checkValidRange(start, end, length);
will assign end
to length or itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
|
||
// Create a new typed list. | ||
L _createList(int size); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire class seems to be indifferent to it being intended for typed data.
Consider making it publicly available as a general ListQueue
skeleton class, e.g., which takes the implementation of _createList
as a function argument to the constructor.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that could be a good addition to package:collection
(or dart:collection
, which implements QueueList
basically the same way), but unfortunately I don't have the bandwidth for it.
lib/src/typed_queue.dart
Outdated
import "package:collection/collection.dart"; | ||
|
||
/// The shared superclass of all the typed queue subclasses. | ||
abstract class _TypedQueue<E, L extends List<E>> extends Object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extends Object with ListMixin<E>
can now be written as just with ListMixin<E>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
lib/src/typed_queue.dart
Outdated
/// The underlying data buffer. | ||
/// | ||
/// This is always both a List<E> and a TypedData, which we don't have a type | ||
/// for that. For example, for a `Uint8Buffer`, this is a `Uint8List`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uint8Buffer -> Uint8Queue
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
L _createList(int size); | ||
} | ||
|
||
abstract class _IntQueue<L extends List<int>> extends _TypedQueue<int, L> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this class necessary?
It doesn't seem to be used for anything except writing
extends _IntQueue<Uint8List>
instead of
extends _TypedQueue<int, Uint8List>
or similar for each actual class.
(If you had a _defaultValue
getter, it would make sense to have it return 0 here).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have _defaultValue
now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Thanks!
What's the story here? |
Sorry, this dropped off my radar. I'll get it mergeable some time this week. |
A bunch of new lints were added since I opened this. I've fixed a few one-offs, but the remainders have many failures and no way to do automated fixes. Does someone else care enough to fix them, or can I just comment them out for now? |
Just pushed the fixes for you.
From |
pubspec.yaml
Outdated
@@ -9,6 +9,9 @@ homepage: https://github.com/dart-lang/typed_data | |||
environment: | |||
sdk: '>=2.4.0 <3.0.0' | |||
|
|||
dependencies: | |||
collection: ^1.1.0 | |||
|
|||
dev_dependencies: | |||
pedantic: ^1.0.0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could also just not depend on pedantic, or lock it to pedantic: 1.7.0 are thereabout.
The newest versions of pedantic have jumped the shark, and I do not want to support the lints they have chose to enable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
lib/src/typed_queue.dart
Outdated
@@ -27,17 +27,20 @@ abstract class _TypedQueue<E, L extends List<E>> with ListMixin<E> { | |||
|
|||
// Iterable interface. | |||
|
|||
@override |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't add @OverRide.
Please don't change quotes.
(So, in general, no to all these changes).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
This reverts commit 5b3b1f3.
Add typed queue classes
These make it easy to, for example, efficiently enqueue incoming
chunked binary data for processing.